Skip to content

Ignore engine lines received after the command is finished - #1207

Open
Mukller wants to merge 3 commits into
niklasf:masterfrom
Mukller:fix/ignore-lines-after-command-done
Open

Ignore engine lines received after the command is finished#1207
Mukller wants to merge 3 commits into
niklasf:masterfrom
Mukller:fix/ignore-lines-after-command-done

Conversation

@Mukller

@Mukller Mukller commented Aug 22, 2026

Copy link
Copy Markdown

Root cause

Engines commonly emit output around the finishing line — e.g. info lines sent just before or after bestmove. Depending on pipe timing, such a line can arrive after the command already processed bestmove and transitioned to DONE. At that point Protocol._line_received() still forwards the line to the current command, and the assertion in BaseCommand._line_received() raises:

AssertionError

(#1161, reproducible with ordinary UCI engines; the reporter saw it regularly.)

Changes

  1. BaseCommand._line_received() returns early when the command is already DONE: there is no handler left to feed the line into, so trailing engine output is discarded instead of tripping an internal invariant. The assertion for genuinely unexpected states (NEW) stays in place.
  2. Regression test: a finished command receives a late info line without raising.

Testing

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

Refs #1161

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().
Engines may emit trailing output around the finishing line (e.g. info
lines sent just before or after bestmove). When such a line arrives
after the command already transitioned to DONE, BaseCommand._line_received
raised AssertionError instead of discarding it (niklasf#1161).
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.

1 participant