Skip to content

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

Description

@nockawa

Shutdown() stops new ticks. It does not stop the tick already running, and it never claimed to in code — only in its own XML doc, which promised CurrentTickNumber is stable once this returns. That promise is false, and the gap it hides is what this issue is really about.

Originally filed as the leftover half of the June 2026 Test host process crashed investigation. That framing is stale — see What changed below. The remaining defect is a contract that lies, not a thread that needs joining.

The mechanism

  • Shutdown() (DagScheduler.cs:709-720) signals _workerShutdown, bumps _tickGeneration, joins the workers. Nothing else.
  • The only thing that stops the timer thread is StopTimerThread() (HighResolutionTimerServiceBase.cs:201-205, a bounded 2 s Join), reachable solely through Dispose(bool).
  • The _workerShutdown gate in ExecuteCallbacks (:396) is at tick entry. A tick already past it runs to completion.
  • _currentTickNumber++ is the last statement of ComputeAndRecordTelemetry (DagScheduler.Telemetry.cs:156), itself the last call of both tick paths (:824 multi-worker, :868 single-worker), on the timer thread.

So CurrentTickNumber can advance by one after Shutdown() returns — and by no more, since _workerShutdown is published before the join.

Why the obvious fix is wrong

OnTickAborted fires at TyphonRuntime.cs:2105, inside OnTickEndInternal — which is the TickEndCallback invoked at DagScheduler.cs:860, six lines before ComputeAndRecordTelemetry. The documented response to that event is FatalStop(), which reaches Shutdown().

So Shutdown() routinely runs on the timer thread, inside the tick it is stopping. On that path the one-tick advance is guaranteed, not merely possible.

The original action item — "have Shutdown() stop + join the timer thread" — would therefore make the timer thread join itself: a guaranteed 2 s 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. This issue anticipated a deadlock risk abstractly; the concrete shipped caller was identified in this report.

What changed since filing

The original body claimed a tick racing view disposal → ViewBase.Contains on a freed HashMap → NullReferenceException. Two parts of that no longer hold:

  • The crash is gone. Its amplifier — ExecuteCallbacks having no outer try/catch on a raw thread — was fixed in Committed storage discipline (#392) + cluster crash durability (#395) #401. What remains is a logged dropped tick, not a host abort. Regression-locked by DagSchedulerTests.TickThread_UnhandledOrchestrationException_SurfacedNotHostCrash.
  • The ordering is already correct. TyphonRuntime.StopInternal disposes nothing (:363-394), and Dispose() joins the timer thread before disposing the per-thread accessors (:397-407 → Dispose(bool) → StopTimerThread()). The disposal that raced in June was NUnit fixture teardown, not the runtime's.

The "nothing is running before teardown" property is therefore already held — by Dispose(), one call later. Adding a second quiescence point in Shutdown() would duplicate it, make every existing caller block for up to a tick, and put a thread-affinity branch in a lifecycle method. Declined.

Resolution — state the guarantee honestly

  • Shutdown() doc drops "stable once this returns"; states the at-most-one advance, names Dispose() as the sole quiescence point, and records why the timer thread must not be joined here
  • FatalStop doc: neither it nor Shutdown is a quiescence point; it typically returns into the tick it is stopping
  • OnTickAborted doc: "after the tick has fully drained" means every system ran or was skipped, not that the tick is over — the handler runs inside the tick and must not block
  • DagSchedulerTests.Shutdown_Clean asserts the advance is in [0, 1] after Shutdown() and 0 after Dispose(), replacing the Thread.Sleep(50) that hid this exact advance behind a settle-then-baseline workaround (added in Fix five timing-dependent test defects that were failing the arm64 nightly #689 after Expected: 346 But was: 347)
  • New StrictTickAbortRuntimeTests.FatalStopFromAbortHandler_ReturnsWithoutSelfJoin_AndCostsExactlyOneMoreTick — calls FatalStop() from inside OnTickAborted at WorkerCount=1, asserts the counter advances exactly once afterwards, and bounds FatalStop() at 1 s so a future self-join reddens instead of silently costing 2 s
  • Full engine suite green (5446 passed / 0 failed); SchedulerLifecycle_RacingShutdownAgainstTick_LeavesNoRunningThread (Stop DagScheduler.Shutdown stranding worker and timer threads that then spin a core forever #690's Sensitive guard) unaffected

Work sits on branch doc/update-2026-08-15, not yet merged. This issue closes on merge.

Deliberately not done

No lifecycle change. If Shutdown() should ever gain an opt-in quiescence wait, the deliverable is a throw when called from the tick thread — turning the self-join footgun into a diagnosable error — not a join. That would be a new issue with its own justification, not a revival of this one.

Activity

  1. self-assigned this
    on Jun 26, 2026
  2. moved this to Backlog in Typhon devon Jun 26, 2026
  3. added theissue type on Jun 29, 2026
  4. updated Estimate S, Priority P2-Medium and Area Runtime on Jun 29, 2026
  5. changed the issue type fromtoon Jun 29, 2026
  6. removed a parent issue on Jul 1, 2026
  7. moved this to Todo in Typhonon Aug 6, 2026
  8. yhcedpn commented on Aug 14, 2026

    @yhcedpn
    Contributor

    Additional evidence for this issue: a documentation precision gap in DagScheduler.cs:706.

    The Shutdown() doc comment says:

    Does NOT stop the timer thread — Dispose(bool) does, via the base class. The thread stays alive but stops producing ticks, so CurrentTickNumber is stable once this returns.

    That last claim ("stable once this returns") is not accurate: the in-flight tick keeps running on the timer thread after Shutdown() returns, and its telemetry finalizer (ComputeAndRecordTelemetry -> _currentTickNumber++) can land after the return. Shutdown() only joins the worker threads; nothing joins the timer thread until Dispose().

    Concrete observation from a downstream demo (TyphonRuntime, WorkerCount=1, BaseTickRate=1000, SystemExceptionPolicy=AbortTickAndStop): after OnTickAborted fires and FatalStop() (-> Shutdown()) returns, CurrentTickNumber intermittently advanced from 0 to 1 — the final bookkeeping of the aborted tick, not a following tick. Isolated runs passed consistently; parallel full-suite runs hit it intermittently. We treat it as a test-side sampling assumption error in our demo, not an engine dispatch/fence bug — consistent with the fix in #689.

    Suggested cleanup: when Shutdown() is updated to stop+join the timer thread (per the action above), also amend the doc comment so it no longer promises CurrentTickNumber is stable immediately upon return — or, short of that, reword it to reflect that the current in-flight tick accounting may still land.

  9. nockawa commented on Aug 15, 2026

    @nockawa
    CollaboratorAuthor

    Confirmed, in full — thanks, this is a good catch and the mechanism you describe is exactly right. Verified against the implementation and the tests:

    • Shutdown() (DagScheduler.cs:709-720) signals _workerShutdown, bumps _tickGeneration, and calls JoinWorkers(). Nothing else.
    • The only thing that stops the timer thread is StopTimerThread() (HighResolutionTimerServiceBase.cs:201-205, a bounded 2 s Join), reachable solely via Dispose(bool).
    • _currentTickNumber++ is the last statement of ComputeAndRecordTelemetry (DagScheduler.Telemetry.cs:156), which is the last call of both tick paths (:824 multi-worker, :868 single-worker), on the timer thread.
    • The _workerShutdown gate in ExecuteCallbacks (:396) is at tick entry, so a tick already past it runs to completion.

    So the doc's CurrentTickNumber is stable once this returns is false, and the other half of the sentence (stops producing ticks) is true.

    One correction, in your favour: on your path it isn't a race — it's re-entrancy, and it's deterministic.

    OnTickAborted fires at TyphonRuntime.cs:2105, inside OnTickEndInternal, which is the TickEndCallback invoked at DagScheduler.cs:860 — six lines before ComputeAndRecordTelemetry. So a handler calling FatalStop() runs Shutdown() on the timer thread, inside the tick it is stopping, and Shutdown() returns straight back into that tick. The 0→1 advance doesn't merely can happen there; it always does. What was intermittent is your demo's observation racing the timer thread's remaining few lines — so your "test-side sampling assumption error" read is right, and the engine-side statement is stronger than the one you wrote.

    This also means the action item on this issue can't be implemented as written. "Have Shutdown() stop + join the timer thread" would, on that path, make the timer thread join itself — a guaranteed 2 s stall that returns false and changes nothing, after which the tick completes and increments anyway. The issue anticipated a deadlock risk abstractly; your report supplies the concrete shipped caller.

    What is being done

    Re-scoping this issue to the contract rather than the join, because the crash it was opened for is gone and the ordering it wanted is already correct:

    • The original NRE (a tick touching a view whose backing store had just been freed) had its amplifier fixed in Committed storage discipline (#392) + cluster crash durability (#395) #401 — an unhandled exception on a raw thread was what turned it into a host crash. What raced view disposal was NUnit fixture teardown, not the runtime's.
    • TyphonRuntime.StopInternal disposes nothing (:363-394), and Dispose() joins the timer thread before disposing the per-thread accessors (:397-407 → Dispose(bool) → StopTimerThread()). The "nothing is running before teardown" property is already held — by Dispose(), one call later.

    Adding a second quiescence point in Shutdown() would duplicate that, make every existing Shutdown() caller block for up to a tick, and require a thread-affinity branch in a lifecycle method. Not worth it. Instead, the guarantee gets stated honestly:

    • Shutdown() / FatalStop() stop new ticks; neither is a quiescence point. Dispose() is.
    • CurrentTickNumber may advance by at most one after Shutdown() returns — exactly one on the OnTickAborted path — and by zero after Dispose().
    • OnTickAborted runs inside the tick; "after the tick has fully drained" meant every system had run or been skipped, not that the tick was over. Handlers must not block.
    • A note against joining the timer thread from Shutdown(), so the self-join isn't rediscovered later.

    Two tests pin it, both green:

    • DagSchedulerTests.Shutdown_Clean — its Thread.Sleep(50) was hiding this exact advance behind a settle-then-baseline workaround (added in Fix five timing-dependent test defects that were failing the arm64 nightly #689 after Expected: 346 But was: 347). It now asserts the advance is in [0, 1] instead of sleeping past it.
    • StrictTickAbortRuntimeTests.FatalStopFromAbortHandler_ReturnsWithoutSelfJoin_AndCostsExactlyOneMoreTick — new, and the one your report earned: calls FatalStop() from inside OnTickAborted at WorkerCount=1, asserts the counter advances exactly once afterwards, and bounds FatalStop() at 1 s so a future self-join reddens instead of silently costing 2 s.

    Full engine suite green (5446 passed / 0 failed).

    Your suggested cleanup is therefore taken as the doc half, and declined as the join half — with the reasoning above recorded here so it doesn't get re-proposed.

  10. changed the title [-]DagScheduler.Shutdown should stop+join the timer thread before view dispose (teardown race)[/-] [+]DagScheduler.Shutdown is not a quiescence point — fix the contract, do not join the timer thread[/+] on Aug 15, 2026
  11. moved this from Todo to In Progress in Typhonon Aug 15, 2026
  12. added a commit that references this issue on Aug 15, 2026
  13. moved this from In Progress to Done in Typhonon Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Fields

Priority

P2-Medium

Estimate

S

Area

Runtime

Start date

None yet

Target date

None yet

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions