Fix AssertionError when a queued engine command is cancelled before starting - #1206
Open
Mukller wants to merge 2 commits into
Open
Fix AssertionError when a queued engine command is cancelled before starting#1206Mukller wants to merge 2 commits into
Mukller wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
In
Protocol.communicate(), a still-pendingnext_commandis replaced by cancelling its futures and callingset_finished()on it. If the awaiting task was cancelled before that queued command ever becameACTIVE(two position updates arriving in quick succession, exactly as in #1116), the command is still in stateCommandState.NEWand the assertion inset_finished()raises: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
BaseCommand.set_finished()now acceptsCommandState.NEW(queued-but-never-started commands) alongsideACTIVE/CANCELLING. A pending result still gets the usualEngineError("engine command finished before returning result"); an already-cancelled result stays cancelled.finished.set_result()with a done-check:communicate()cancels thefinishedfuture immediately before callingset_finished(), so on this path the future can already be cancelled — the previous unconditionalset_result(None)would have raisedInvalidStateErrornext.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