Repository navigation
#628 The finalize lock never guarded the finalize section - #782
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.csand 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, and10-fence-parallelisation.mdclosed the hazard on that basis.The two that actually contradicted each other sat ~40 lines apart, on the same lock:
RecordClusterDrain: "DrainPendingClusterFinalizationsre-checks occupancy under the finalize lock"DrainPendingClusterFinalizations: "the per-archetype lock is unnecessary here" — and the body takes noneWhat the code actually does: defer, not exclude
A Migrate-phase worker that clears a cluster's last bit records the chunkId via
RecordClusterDrainand returns. Finalize-and-free runs later inDrainPendingClusterFinalizations— once per archetype, fromFinalizeArchetypeFence, after the Migrate and AabbRefresh phase barriers. It takes no lock and must not need one: the barriers exclude every concurrentClaimSlotInCellandReleaseSlot, 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
_finalizeLockreally guardsGrowth and append. The banner now enumerates all six acquisitions instead of describing a section:
ApplyDirtyBitDeltasGrowFenceDirtyBitsForChunkIdArray.ResizeRecordClusterDrainTryClaimSlotInCluster×2AddToActiveList, pool add, back-pointer)EnqueueMigrationsBulkPendingMigrationsIt also records the asymmetry left behind:
AddToActiveListruns under it,RemoveFromActiveListnever does, no reader takes it at all.Two corrections beyond the comments
ActiveClusterIdswas 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_violationnamed 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, andscope:gainsFinalizeArchetypeFence,RecordClusterDrain,DrainPendingClusterFinalizations.Method, and what I did not do
Verified by walking every
_finalizeLockacquisition and every caller ofAddToActiveList,RemoveFromActiveList,FinaliseEmptyClusterCellStateandAllocateNewCluster— 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.PreSizeMigrationBuffersbounding migration-sourced drains only is correct and already handled — that is exactly whatRecordClusterDrain'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.pyoverArchetypeClusterState.cs→ 172 passed, 0 failed across 15 fixtures.