Repository navigation
fix(mcu): mark a command pending before its bytes are written - #871
Open
hongquanli wants to merge 1 commit into
Open
hongquanli wants to merge 1 commit into
hongquanli wants to merge 1 commit into
Conversation
send_command set mcu_cmd_execution_in_progress (and last_command, the send time, the retry count and the cleared abort error) only after the serial write returned. A reply parsed before that - SimSerial answers inside write(), and a fast link can too - found the flag still clear, and the flag was then set with nothing left to clear it until another packet acked the same id: in simulation the next command's reply, at worst the next heartbeat. A CMD_EXECUTION_ERROR reply in that window was wiped the same way, so the wait timed out instead of raising CommandAborted. Everything the reader matches a reply against is now set before the write, the in-progress flag last. A write that raises puts that state back as it found it, flag first, so a failed write leaves no pending command for a wait or an ack-timeout resend. The send time is taken again after the write, so the ack timeout still counts from when the bytes went out. Tests: a SimSerial whose write() returns only after the reader has handled the reply (completion and execution error, both failed before), and a failed write that leaves no command pending. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What fails
Microcontroller.send_commandsetmcu_cmd_execution_in_progress = Trueafterself._serial.write(...)returned. It setlast_command, the send time, the retry count and the cleared abort error after the write too. The reader thread clears the flag when it parses the status packet that acks the command. When that reply is parsed beforesend_commandsets the flag, this happens:send_commandthen sets the flag to True.SimSerialanswers only insidewrite(), so in simulation the next such packet is the reply to the next command. For a wait that sends nothing else, that is the heartbeat, up to 2.5 s later. ACMD_EXECUTION_ERRORreply in the same window is lost the same way:send_commandclears the abort that the reader just recorded, and the wait times out instead of raisingCommandAborted.How it was found:
test_HighContentScreeningGui.py::test_home_stage_proceeds_with_only_the_host_busy_flag_setwas slow in CI, and under load it hit its 10 swaitUntil. Each command in the homing sequence whose reply landed in the window made that step'swait_till_operation_is_completedlast until the next heartbeat.How often: rarely, per command. The reader has to be scheduled in the few bytecodes between the write returning and the flag being set. In a bare process with
SimSerial:The GUI process runs many threads, and under CI load it hits the window more often.
On hardware: the window is the time between the write returning and the flag being set. That is microseconds, but a fast USB reply can land in it. The controller sends its status every 10 ms with the same id and status, so on hardware a hit should cost about one status period, not a stuck wait.
Fix
last_command, the send time, the retry count, the cleared abort error and the packet count at send. The in-progress flag is set last, so a reader that sees it turn on finds the rest already set for this command.There is no change to the protocol, the firmware, the timeouts, the retries or the locking.
_cmd_lockstill serialises senders, and the reader still does not take it to match a reply.Tests
The new tests are in
tests/control/test_microcontroller.py. They use aSimSerialsubclass whosewrite()returns only after the reader thread has handled the reply. It waits on the packet callback, which runs after the reader's matching branches. That makes the race happen every time:test_a_reply_handled_before_write_returns_completes_the_commandfails on master:test_an_execution_error_handled_before_write_returns_fails_the_commandfails on master. The wait times out instead of raisingCommandAborted:test_a_failed_write_leaves_no_command_pendingpasses on master, where the state was only set after the write. It guards the rollback: with the state moved before the write but no rollback, it fails.Results, under both PyQt5 and PyQt6:
test_HighContentScreeningGui.py, run whole in one process with CI's config: 53 passed.Still unverified
CephlaStage.home()waits on it. That wait varies by up to one heartbeat interval (2.5 s) from run to run. The fix removes only the extra heartbeat wait for each command the race hits, which is what pushed the test past 10 s under CI load.🤖 Generated with Claude Code
Summary by CodeRabbit