Skip to content

Fix AssertionError when a queued engine command is cancelled before starting - #1206

Open
Mukller wants to merge 2 commits into
niklasf:masterfrom
Mukller:fix/cancel-queued-command
Open

Fix AssertionError when a queued engine command is cancelled before starting#1206
Mukller wants to merge 2 commits into
niklasf:masterfrom
Mukller:fix/cancel-queued-command

Conversation

@Mukller

@Mukller Mukller commented Aug 22, 2026

Copy link
Copy Markdown

Root cause

In Protocol.communicate(), a still-pending next_command is replaced by cancelling its futures and calling set_finished() on it. If the awaiting task was cancelled before that queued command ever became ACTIVE (two position updates arriving in quick succession, exactly as in #1116), the command is still in state CommandState.NEW and the assertion in set_finished() raises:

AssertionError: CommandState.NEW

Nothing has been sent to the engine while a command sits in the queue, so there is no active phase to unwind — finishing it cleanly is safe.

Changes

  1. BaseCommand.set_finished() now accepts CommandState.NEW (queued-but-never-started commands) alongside ACTIVE/CANCELLING. A pending result still gets the usual EngineError("engine command finished before returning result"); an already-cancelled result stays cancelled.
  2. Guard finished.set_result() with a done-check: communicate() cancels the finished future immediately before calling set_finished(), so on this path the future can already be cancelled — the previous unconditional set_result(None) would have raised InvalidStateError next.
  3. Regression tests in EngineTestCase: cancelled-while-queued (the AssertionError is thrown if async analysis is cancelled too soon #1116 scenario) and replaced-while-pending (EngineError surfaced on result).

Fixes #1116

Testing

  • python -m pytest test.py::EngineTestCase → 27 passed, 11 skipped (binary-gated), including the two new regression tests

Protocol.communicate() replaces a still-pending next_command by calling
set_finished() on it. If that command had never left the NEW state (its
awaiting task was cancelled before the previous command finished), the
assertion in set_finished() raised AssertionError: CommandState.NEW,
crashing the engine task (niklasf#1116).

Allow CommandState.NEW in set_finished(): nothing was sent to the engine
while the command was queued, so there is no active phase to unwind -
surface the usual EngineError on its result instead. Also guard
finished.set_result(), since communicate() cancels the finished future
right before calling set_finished().
Python 3.14 removed implicit event-loop creation, so constructing
BaseCommand outside a running loop raised RuntimeError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssertionError is thrown if async analysis is cancelled too soon

1 participant