Skip to content

Cluster-state docs and comments assert locking invariants the code does not maintain #628

Closed

Description

@nockawa

Found while analysing #582. These are wrong today, independent of whether #582 lands, and one of them is why the hazard went unnoticed through a dedicated review.

Design docs / rules

Doc Claim Reality
claude/design/Runtime/10-fence-parallelisation.md:121 ActiveClusterIds/ActiveClusterCount "mutated under _finalizeLock … Already solved." Wrong three ways: the destroy path (ReleaseSlot:2544/:2582) takes no lock; the non-spatial ClaimSlot spawn path takes no lock; and no reader anywhere takes _finalizeLock, so even a fully-locked writer wouldn't make the walk safe. This is the cell that let the fence-parallelisation review conclude the hazard was handled.
claude/rules/spatial.md:292-294 (MD-02) The drain path "enters per-archetype _finalizeLock exclusive section before CellClusterPool.RemoveCluster, RemoveFromActiveList, ClusterSegment.FreeChunk" Describes code that does not exist. Every _finalizeLock acquisition is at ArchetypeClusterState.cs:141, 213, 321, 1205, 1291, 2336 — none on the drain-finalize path. Scope also omits the caller (Transaction.ECS.cs).
claude/design/Runtime/09-tick-pipelining.md:28 (N8), :281 "D's core is a parameter flip plus a drain call, not new machinery"; "Destroy just needs the flag flipped" Refuted — see #582. N8's sub-claim that spawn's AddToActiveList is under _finalizeLock is true only for the spatial path.

Code comments (all in src/Typhon.Engine/Ecs/internals/ArchetypeClusterState.cs)

Site Claim Reality
:2485-2488 "Caller owns the writer mutex … when deferFinalize = false" No caller does. FlushPendingDestroys takes only EpochGuard.Enter, which gates page eviction, not chunk-id recycling
:2471-2472 "Safe because no concurrent claimer can CAS a slot back in" The premise is #582
:46-51 PaddedFinalizeLock banner — the draining worker "enters the finalize section (FinaliseEmptyClusterCellState + RemoveFromActiveList + segment FreeChunk)" The lock is never taken on that path. Almost certainly the origin of MD-02's stale text — fix both together
:2689-2690 FinaliseEmptyClusterCellState "called inside the per-archetype _finalizeLock" Neither caller holds it
:2710-2711 ClearSlotMetadata's "last-bit-wins signal … decide which worker enters the finalize section under _finalizeLock" No worker enters any such section; it records a drain instead
:310 vs :352-354 "re-checks occupancy under the finalize lock" vs "the per-archetype lock is unnecessary here" Direct self-contradiction. The code takes no lock — keep one, delete the other
:37 ActiveClusterIds is "clusters with occupancy > 0" Already false during the migrate drain window; ClusterRef.cs:504-508 documents the opposite
:68-69 Capacity "pre-sized by PreSizeMigrationBuffers" Bounds migration-sourced drains only

Why this is worth its own issue

Six of these comments describe a _finalizeLock-protected finalize section that does not exist anywhere in the code. They corroborate each other, which is what made the wrong model survive: a reader checking one comment against another gets consistent confirmation of a fiction. MD-02 then encoded it as a correctness rule, and 10-fence-parallelisation.md marked the hazard closed on that basis.

Fix these regardless of #582's outcome — a stale comment that asserts a lock is worse than no comment.

Activity

  1. added
    documentation
    on Jul 31, 2026
  2. added theissue type on Jul 31, 2026
  3. added a commit that references this issue on Jul 31, 2026
  4. moved this to Todo in Typhonon Jul 31, 2026
  5. nockawa commented on Aug 11, 2026

    @nockawa
    CollaboratorAuthor

    Re-verified against main @ 640e1a2b (2026-08-11) — still valid. #582 is still open.

    The load-bearing finding is intact verbatim. ArchetypeClusterState.cs still contradicts itself about the same lock, in two comments ~40 lines apart:

    • RecordClusterDrain's summary: "DrainPendingClusterFinalizations re-checks occupancy under the finalize lock and skips non-empty entries"
    • DrainPendingClusterFinalizations' own remarks: "The occupancy re-read is therefore single-threaded and the per-archetype lock is unnecessary here" — and the body takes no lock

    The PaddedFinalizeLock banner (:46-51) still asserts the draining worker "enters the finalize section (FinaliseEmptyClusterCellState + RemoveFromActiveList + segment FreeChunk)", which is the section the method above says it does not enter.

    rules/spatial.md MD-02 still encodes it as a rule — "cluster-fully-drains path … enters per-archetype _finalizeLock exclusive section before CellClusterPool.RemoveCluster, RemoveClusterFromPerCellIndex, RemoveFromActiveList, ClusterSegment.FreeChunk" — and claude/design/Runtime/10-fence-parallelisation.md still marks the ActiveClusterIds/ActiveClusterCount row "Already solved."

    Path correction for the fixer: the rule now lives at rules/spatial.md in the Typhon repo, not claude/rules/spatial.md — rules moved in #747 and are CI-checked there (scripts/check-rule-scopes.py), so an MD-02 edit belongs in the same commit as the code/comment fix.

    Note the code has moved since this was filed and the issue's line numbers are stale — _finalizeLock is now acquired at :141, :213, :321, :1410, :1496, :2541, and :2905 documents a genuine "called inside the per-archetype _finalizeLock" path. So the per-site table needs re-walking line by line before editing; what is confirmed today is that the drain/finalize path the comments and MD-02 describe still does not take the lock.

    PR #701 did not address this — it is doc/-only and touched neither claude/, rules/, nor src/.

  6. added a commit that references this issue on Aug 11, 2026
  7. moved this from Todo to Done in Typhonon Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentation

    Type

    Fields

    Priority

    None yet

    Estimate

    None yet

    Area

    None yet

    Product

    None yet

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions