Skip to content

#628 The finalize lock never guarded the finalize section - #782

Merged
nockawa merged 1 commit into
mainfrom
fix/628-cluster-lock-comments
Aug 11, 2026
Merged

nockawa merged 1 commit into
mainfrom
fix/628-cluster-lock-comments

Conversation

@nockawa

@nockawa nockawa commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Fixes #628

Comments and one rule. No behaviour change — this describes what the code has been doing.

Docs half (the two claude/design/Runtime/ docs): Log2n-io/Typhon-Claude#83.

What was wrong

Six comments in ArchetypeClusterState.cs and rule MD-02 all described a _finalizeLock-protected finalize section. They corroborated each other, which is what let the wrong model survive a dedicated review: checking one comment against another returned consistent confirmation of a fiction. MD-02 then encoded it as a correctness rule, and 10-fence-parallelisation.md closed the hazard on that basis.

The two that actually contradicted each other sat ~40 lines apart, on the same lock:

  • RecordClusterDrain: "DrainPendingClusterFinalizations re-checks occupancy under the finalize lock"
  • DrainPendingClusterFinalizations: "the per-archetype lock is unnecessary here" — and the body takes none

What the code actually does: defer, not exclude

A Migrate-phase worker that clears a cluster's last bit records the chunkId via RecordClusterDrain and returns. Finalize-and-free runs later in DrainPendingClusterFinalizations — once per archetype, from FinalizeArchetypeFence, after the Migrate and AabbRefresh phase barriers. It takes no lock and must not need one: the barriers exclude every concurrent ClaimSlotInCell and ReleaseSlot, and a lock would not substitute, because finalizing while a claimer holds a CAS-won slot frees a live chunk no matter who holds what.

The other drain path (deferFinalize: false) finalizes inline and is safe only because its caller is single-threaded — which the old "caller owns the writer mutex" wording dressed up as a lock discipline that does not exist. It now says so, and says a concurrent caller there would be a defect no lock currently prevents.

What _finalizeLock really guards

Growth and append. The banner now enumerates all six acquisitions instead of describing a section:

Site Guards
ApplyDirtyBitDeltas batch-apply worker-local dirty-bit deltas
GrowFenceDirtyBitsForChunkId Array.Resize
RecordClusterDrain fallback grow only — the normal append is lock-free
TryClaimSlotInCluster ×2 new-cluster slow path (lockstep dual-segment allocate, AddToActiveList, pool add, back-pointer)
EnqueueMigrationsBulk bulk append to PendingMigrations

It also records the asymmetry left behind: AddToActiveList runs under it, RemoveFromActiveList never does, no reader takes it at all.

Two corrections beyond the comments

ActiveClusterIds was documented as "clusters with occupancy > 0". Deferral makes that structurally false — a drained cluster stays listed until Finalize. Readers needing occupancy must check the occupancy word, not membership.

MD-02's on_violation named a lock-shaped failure it never had. It now names the one deferral actually prevents (a worker finalizing inline frees a chunk a concurrent claim already won), plus the new way to break it: moving the finalize pass before the barriers. The invariant itself is rewritten around deferral + barrier, and scope: gains FinalizeArchetypeFence, RecordClusterDrain, DrainPendingClusterFinalizations.

Method, and what I did not do

Verified by walking every _finalizeLock acquisition and every caller of AddToActiveList, RemoveFromActiveList, FinaliseEmptyClusterCellState and AllocateNewCluster — not by trusting the issue's table, whose line numbers had all drifted.

Two of #628's sub-claims did not survive that walk as stated, and I have not "fixed" them into the opposite error:

  • ClearSlotMetadata's last-bit-wins signal is real and load-bearing; only the clause about entering a lock on the strength of it was wrong.
  • PreSizeMigrationBuffers bounding migration-sourced drains only is correct and already handled — that is exactly what RecordClusterDrain's fallback grow is for.

#582 is untouched and still open. Nothing here changes the concurrent-walk hazard; it stops the comments from claiming it is handled.

Verification

check-rule-scopes ✅ (192 scope lines) · check-doc-links ✅ · audit-rule-coverage ✅ (0 blocking) · engine build ✅ 0 warnings · test-affected.py over ArchetypeClusterState.cs → 172 passed, 0 failed across 15 fixtures.

Six comments in ArchetypeClusterState and rule MD-02 all described a _finalizeLock-protected finalize section, and they corroborated each other, which is what let the wrong model survive a dedicated review: checking one comment against another returned consistent confirmation of a fiction. MD-02 then encoded it as a correctness rule and 10-fence-parallelisation.md closed the hazard on that basis.

What the code actually does is defer, not exclude. A Migrate-phase worker that clears a cluster's last bit records the chunkId via RecordClusterDrain and returns; the finalize-and-free runs later in DrainPendingClusterFinalizations, once per archetype, after the Migrate and AabbRefresh phase barriers. It takes no lock and must not need one — its safety is that the barriers exclude every concurrent claim and release, and a lock would not substitute, since finalizing while a claimer holds a CAS-won slot frees a live chunk no matter who holds what. The other drain path finalizes inline and is safe only because its caller is single-threaded, which the old "caller owns the writer mutex" wording dressed up as a lock discipline that does not exist.

_finalizeLock does guard something, just not that: growth and append. The banner now enumerates all six acquisitions rather than describing a section, and records the asymmetry it leaves behind — AddToActiveList runs under it, RemoveFromActiveList never does, and no reader takes it at all.

Two corrections beyond the comments. ActiveClusterIds was documented as clusters with occupancy > 0, which deferral makes structurally false: a drained cluster stays listed until Finalize. And MD-02's on_violation now names the failure the deferral prevents (a worker finalizing inline frees a chunk a concurrent claim already won) instead of the lock-shaped one it never had.

The code is unchanged; this is what it has been doing. Verified by walking every _finalizeLock acquisition and every caller of AddToActiveList, RemoveFromActiveList, FinaliseEmptyClusterCellState and AllocateNewCluster. #582 is untouched and still open.
@nockawa
nockawa merged commit fa0acc5 into main Aug 11, 2026
16 checks passed
@nockawa
nockawa deleted the fix/628-cluster-lock-comments branch August 11, 2026 14:03
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.

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

1 participant