Skip to content

#404 Shutdown() promised a stable tick number while returning into the tick that increments it - #814

Merged
nockawa merged 1 commit into
mainfrom
doc/update-2026-08-15
Aug 15, 2026
Merged

nockawa merged 1 commit into
mainfrom
doc/update-2026-08-15

Conversation

@nockawa

@nockawa nockawa commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Fixes #404

DagScheduler.Shutdown's XML doc claimed CurrentTickNumber was stable once it returned. It is not — and the mechanism was already documented in a test comment 700 lines away, which is how the contradiction survived.

Reported externally on #404 (comment); the report is correct, and understates itself.

The defect

Shutdown() signals _workerShutdown, bumps the generation, joins the workers. The _workerShutdown gate in ExecuteCallbacks (:396) is at tick entry, so a tick already past it runs to completion on the timer thread — and _currentTickNumber++ is the last statement of that tick's telemetry finalizer (DagScheduler.Telemetry.cs:156). The counter can advance by one after Shutdown() returns, and by no more, since _workerShutdown is published before the join.

On the OnTickAborted path it is not a race but a certainty. That event fires at TyphonRuntime.cs:2105, inside OnTickEndInternal — which is the TickEndCallback invoked at DagScheduler.cs:860, six lines before ComputeAndRecordTelemetry — and its documented response is FatalStop(). So Shutdown() routinely runs on the timer thread, inside the tick it is stopping, and returns straight back into it.

Why the issue's original action item was dropped

"Have Shutdown() stop + join the timer thread" is a self-join on that path: StopTimerThread's 2 s bound turns it into a stall that returns false and changes nothing, after which the tick completes and increments anyway. The doc fix would not even be delivered by the code fix.

The property the issue wanted is already held: TyphonRuntime.StopInternal disposes nothing, and Dispose() joins the timer thread before disposing the per-thread accessors. #404 has been rewritten to record this.

What changed

Docs state the guarantee instead of joining for it — Shutdown/FatalStop stop new ticks and are not quiescence points, Dispose() is; OnTickAborted's "after the tick has fully drained" means every system ran or was skipped, not that the tick is over, so the handler is documented as running inside the tick and told not to block.

Tests:

  • DagSchedulerTests.Shutdown_Clean — its Thread.Sleep(50) was added by Fix five timing-dependent test defects that were failing the arm64 nightly #689 (after Expected: 346 But was: 347) to hide this exact advance behind a settle-then-baseline workaround. The sleep is now the assertion: at most one advance after Shutdown(), none after Dispose().
  • StrictTickAbortRuntimeTests.FatalStopFromAbortHandler_ReturnsWithoutSelfJoin_AndCostsExactlyOneMoreTick — new, covering the reported path: FatalStop() from inside OnTickAborted at WorkerCount=1, asserting the counter advances exactly once, and bounding FatalStop() at 1 s so a future self-join reddens instead of silently costing 2 s.

The false line came from #690, which correctly replaced "waits for the current tick to finish" and introduced "stable once this returns" on the way past.

Verification

bash scripts/pre-push.sh — all gate-equivalent checks passed: policy scripts, engine suite Release (5445 passed / 0 failed), Workbench suite Release (662 passed / 0 failed). SchedulerLifecycle_RacingShutdownAgainstTick_LeavesNoRunningThread (#690's Sensitive guard) unaffected.

Not covered locally: shard contention and the serial Sensitive pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TmLHw4QbBCE6Cp97xu2w9Y

…e tick that increments it

DagScheduler.Shutdown's XML doc claimed CurrentTickNumber was stable once it returned. It is not. Shutdown signals _workerShutdown, bumps the generation and joins the workers; the _workerShutdown gate in ExecuteCallbacks is at tick ENTRY, so a tick already past it runs to completion on the timer thread, and _currentTickNumber++ is the last statement of that tick's telemetry finalizer. The counter can therefore advance by one after Shutdown returns, and by no more, since _workerShutdown is published before the join.

On the OnTickAborted path the advance is not a race but a certainty. That event fires inside OnTickEndInternal, which is the TickEndCallback invoked six lines before ComputeAndRecordTelemetry, and its documented response is FatalStop. So Shutdown routinely runs on the timer thread, inside the tick it is stopping, and returns straight back into it. This is also why the issue's original action item cannot be implemented as written: joining the timer thread from there is a self-join, which StopTimerThread's two-second bound turns into a stall that returns false and changes nothing.

The guarantee is now stated rather than joined for. Shutdown and FatalStop stop new ticks and are not quiescence points; Dispose is, because it joins the timer thread before anything downstream disposes. OnTickAborted's "after the tick has fully drained" meant every system had run or been skipped, not that the tick was over, so the handler is documented as running inside the tick and told not to block.

The knowledge already existed in the repository and was sitting in the wrong place. DagSchedulerTests.Shutdown_Clean carried a comment describing this exact mechanism, added by #689 after the assertion failed with "Expected: 346 But was: 347", and then slept fifty milliseconds to hide the advance instead of asserting it. That sleep is now the assertion: at most one advance after Shutdown, none after Dispose. The false doc line came later, from #690, which correctly replaced "waits for the current tick to finish" and introduced "stable once this returns" on the way past.

FatalStopFromAbortHandler_ReturnsWithoutSelfJoin_AndCostsExactlyOneMoreTick is new and covers the path the report was filed against: FatalStop called from inside OnTickAborted at WorkerCount 1, asserting the counter advances exactly once afterwards and bounding FatalStop at one second so a future self-join reddens instead of silently costing two.
@nockawa
nockawa merged commit 2bb3e4d into main Aug 15, 2026
17 checks passed
@nockawa
nockawa deleted the doc/update-2026-08-15 branch August 15, 2026 11:11
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.

DagScheduler.Shutdown is not a quiescence point — fix the contract, do not join the timer thread

1 participant