Skip to content

Immediately cancelling after prompt is sent hangs agent forever #442

@emil-e

Description

@emil-e

This is in agent-shell, if I:

  • Sent prompt
  • Immediately cancel with C-c C-c

Then it hangs forever with the following message:

Session ba806dac-1f1f-46ae-9968-13e7e2bd7160: consuming background task result

According to Claude, this is the root cause:

In `acp-agent.ts`, the `prompt()` method uses a `promptReplayed` flag to distinguish results belonging to the current prompt from results of a previous background task. The flag is set to `true` when the CLI replays the user message (matched by UUID) back through the generator.

When cancel arrives before the user message replay, this is the sequence:

1. `prompt()` enters the `while(true)` loop with `promptReplayed = false`
2. `cancel()` sets `session.cancelled = true` and calls `query.interrupt()`
3. The CLI emits a `"result"` message for the interrupted prompt
4. The `"result"` handler checks `!promptReplayed` first — it's still `false`
5. The result is consumed as a background task result (`break` out of switch, continue loop)
6. The `session.cancelled` check on the next line is never reached
7. `session.query.next()` blocks forever — no more messages will arrive

Relevant code:

case "result": {
    // ... usage accounting ...
    if (!promptReplayed) {
        this.logger.log(`Session ${id}: consuming background task result`);
        break;  // <-- continues the while loop, skipping the cancelled check below
    }
    if (session.cancelled) {
        return { stopReason: "cancelled" };  // <-- unreachable in this scenario
    }
    // ...
}

Possibly relevant PR:
#423

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions