Repository navigation
fix: a cancelled caller's frame never reaches the wire; the shared Stop always runs (bug-076 S1/N4/N5/N6) - #11
Conversation
…g one (bug-076) At a station, 73 STATION STOP presses ran as 73 concurrent StopAsync calls on the carriage. Each wrote its own [Stop, CommandSeq]; the PLC acknowledges only the sequence it last saw, so 72 callers failed NotAcknowledged while the axis stopped. - A pending Stop is one shared task: the first caller writes the edge at once, later callers join it and get its result (success or failure); a Stop after it finished is a new Stop. It runs to its end regardless of one caller's cancellation. - While a Stop is pending no other command word is written: a new verb is refused (G2, Busy), a command already past its guards waits (a pre-empted verb ends with OperationCanceledException), the disconnect's Enable 0 goes after the Stop. - A command pre-empted at its ack deadline no longer clears its edge with CancellationToken.None after the Stop (that stale CommandSeq made the PLC drop the Stop's ack). - protocol.md § Command semantics: "One command in flight". Tests: StopStormTests (8): 6 red on master (73 Stop writes, 72 of 73 callers NotAcknowledged), 8 green here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…op always runs (bug-076 S1/N6) S1: PriorityGate grants a waiter it already claimed even when the waiter's token was cancelled meanwhile (the cancellation callback loses to the claim). ModbusChannel.ExecuteAsync now re-checks the token right after it takes the slot, releases the slot and throws, so a verb a Stop pre-empted can never write [Move, k] after [Stop, k+1]. A frame that already started is not cancelled; it holds the slot, so the Stop comes after it. N6: the Stop's runner is started in a finally, after the running verb is cancelled; a throwing callback on the verb's token is logged and the Stop goes out regardless. RunStopAsync clears _stop and publishes the result in a finally. N4: protocol.md "One command in flight" names the two deferred exceptions (a disconnect's Enable 0, a re-attach's edge clear); neither carries a motion edge. N5: protocol.pdf regenerated from this protocol.md (markdown-it-py -> HTML -> headless Chrome, as the previous PDF's metadata shows). Tests (StopStormTests +3): S1 and N6-cancel red before the fix, green after; N6-run-throws pins the failure path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Independent review: bug-076 follow-up (S1/N4/N5/N6), MERGE. Binds to
|
Mutant (StopStormTests, project rebuilt) |
Result |
|---|---|
try/catch/finally around Cancel removed (old order) |
1/11 fail: StopAsync_CancellingTheVerbThrows_StopStillRunsAndIsNotLeftPending |
RunStopAsync's finally body emptied |
10/11 fail, including both N6 tests |
RunStopAsync put back as it was at 15088be |
11/11 pass |
The last row shows the RunStopAsync rewrite behaves exactly like 15088be, which already caught every exception. The new StopAsync_RunThrows_… test guards behaviour that already existed; that is fine.
FakePlcChannel.OnToken is test-only and harmless. It lives in src/ModelingEvolution.GenericAxis.Tests/Support, defaults to null, and is invoked before the fake's own ThrowIfCancellationRequested. Product code is untouched.
N4: the protocol sentence is accurate. Closed.
docs/protocol.md:76-79 matches the code:
- Disconnect:
DisableForDetachAsyncwrites the Enable level only. - Re-attach:
AttachAsyncwrites[Enable level, CommandAck], which carries no edge bit.
Both can land after the Stop's word, and neither carries a motion edge. With S1 closed, these are the only two exceptions left.
N5: the regenerated protocol.pdf is complete and legible. Keep it.
I compared it with the old PDF (git show 15088be:docs/protocol.pdf), extracting text with pypdf and rendering pages with PyMuPDF.
- Complete. All 17 headings and all 87 register-table rows of
protocol.mdare present.- In a word-count comparison with the markdown, 7 words came up short in the new PDF; those look like hyphenation splits (
--allow-motion,dead-commander). In the old PDF, 165 came up short. - The old PDF is stale: it predates PR fix: one command in flight per axis; concurrent Stops join the pending one (bug-076) #10 and lacks the whole "One command in flight" paragraph. A PLC programmer would be reading an out-of-date contract.
- In a word-count comparison with the markdown, 7 words came up short in the new PDF; those look like hyphenation splits (
- Legible.
- Register tables have ruled rows. A table split across a page repeats its header row (Command block, pages 1–2), and no row is cut.
- No text span reaches past the right margin (checked on every page). The long example messages wrap inside their grey block.
- Differences a reader may notice; none is worse in a way that matters.
- The paper size is now A4, not Letter.
- The code font in tables and message blocks is smaller than before, but still readable at 100 %.
- 13 pages instead of 18.
- The render is not reproducible from the repo. No script; markdown-it-py plus headless Chrome by hand. Committing the stylesheet and command next time would let anyone regenerate it. NOTE, not blocking.
Q5: invented constraints and conventions
None.
- No new option, timeout or setting.
- No new lock.
- The catch-all around
Cancellogs at Error and is sourced by N6. - B.0, identifiers and JSON: no id, value type, event or command record touched.
- No Moq.
Run evidence (Debug, ~/.dotnet/dotnet, -nodeReuse:false, foreground)
53c890e:ModelingEvolution.GenericAxis.Tests223/223,ModelingEvolution.GenericAxis.Plugin.Tests69/69.- Mutants as above, each run on
StopStormTestswith the project rebuilt. Checkout restored and clean at53c890e.
Not verified
- No real PLC and no simulator run.
TestApp.Testsnot run.- PDF checked on screen renders, not printed.
🤖 Generated with Claude Code
Follow-up to #10 (bug-076), from its review: S1, N4, N5, N6. The branch is cut from #10's head
15088be, so until #10 is merged this diff also shows #10.S1: a cancelled caller's frame could still reach the PLC after the Stop (safety)
Verified in the code:
PriorityGate.ReleaseOne→TakeNextclaims a waiter (TryClaim0→1) and grants it.Waiter.WaitAsync) cancels only an unclaimed waiter (CAS 0→2). So a waiter claimed before its token is cancelled is still granted, and the same holds when the token is cancelled while the waiter is enqueued but not yet registered.ModbusChannel.ExecuteAsyncdid not check the token again once it held the slot.[Move, k]after[Stop, k+1], and the PLC would take it as a new command.Fix:
ExecuteAsyncchecks the token right after it takes the slot; if it is cancelled, it releases the slot and throws before the frame starts. After that check, ordering holds:Test (
Channel_SlotGrantedAfterTokenCancelled_NoFrameWrittenSlotReleased), deterministic:Callbacks run newest first, so the gate claims the waiter before the waiter's own callback can cancel it; the token is already cancelled when the frame would start. The test asserts
OperationCanceledException, that no TCP connection reached the test listener (so no frame), and that the slot was released.N6: the shared Stop can never be left dead
StopCoreAsync, the runner now starts in afinallyafterrunning.Cancel(). If a token callback makesCancel()throw, the error is logged and the Stop still goes out. Before,_stopstayed published with no runner, and every laterStopAsyncjoined a task that never completes.RunStopAsync,_stopis cleared and the result published in afinally.Tests:
StopAsync_CancellingTheVerbThrows_StopStillRunsAndIsNotLeftPending: red before, because the firstStopAsyncthrewAggregateExceptionand no runner started. Green after: the Stop runs, and the nextStopAsyncwrites a new Stop. It needs one test-only hook,FakePlcChannel.OnToken, to put a throwing callback on the verb's token.StopAsync_RunThrows_CallersGetTheFailure_NextStopWritesANewOne: its caller gets the Stop's own failure, and the nextStopAsyncwrites a new Stop. It passes both before and after; it pins the failure path.N4:
docs/protocol.md"One command in flight" now names the two known exceptions, deferred until observed:
Enable 0that took itsCommandSeqjust before the Stop;Either can land after the Stop's word; neither carries a motion edge.
N5:
protocol.pdfregenerated in this PR8f1f19f,bf59723,bd9a5b7,6f62ada) only say "regenerated". The old PDF's metadata shows how:Creator HeadlessChrome/154,Producer Skia/PDF, title "generic-axis protocol".markdown-it-py(CommonMark + tables) → HTML with title "generic-axis protocol" and a plain print stylesheet.google-chrome --headless=new --no-pdf-header-footer --print-to-pdf.docs/protocol.mdat this commit.Evidence (Debug, native
~/.dotnet/dotnet, foreground)StopStormTestson this branch's tests against the code at15088be: 2 red (S1, N6-cancel), 9 green.StopStormTestsplus the channel and gate classes (PriorityGateTests,ChannelBlockingTests,ErrorMessageTests,ConnectMessageTests): 25/25.ModelingEvolution.GenericAxis.Tests: 223/223 (220 + 3 new).ModelingEvolution.GenericAxis.Plugin.Tests: 69/69.Not verified
TestApp.Testsnot run locally.Busyfor a jog re-pressed while a Stop is still pending. The rw2 lead and the tester should know.🤖 Generated with Claude Code