Control mode begin/end command interleaving behavior

Medium
Unlisted
For control mode, begin/end are sent around all commands. Are these guaranteed to be ended before a new begin starts? Can they be interleaved? Show source code for evidence.

Yes, they are guaranteed to be non-interleaved. Here's the evidence:

Looking at cmd-queue.c:

  1. Commands are processed sequentially in cmdq_next() (line 730-807). The queue processes one item at a time in a for(;;) loop.

  2. Begin/end are synchronous within cmdq_fire_command() (lines 596-681):

    • Line 619: cmdq_guard(item, "begin", flags); is called immediately before execution
    • Line 677-679: Either "error" or "end" is called at the function's exit via the out: label
  3. Commands that wait (return CMD_RETURN_WAIT) don't interleave because:

    • The wait happens after the command has already completed and emitted its %end (line 679)
    • The CMDQ_WAITING flag (line 761-762, 793-794) just pauses the queue—no new command can fire
  4. Each client has its own queue (via cmdq_get(c)), so cross-client interleaving doesn't apply to a single control client's stream.

The pattern guarantees: %begin → output → %end (or %error) for each command, never interleaved.