Skip to content

#817 DrainPendingClusterFinalizations never disposed its accessors, so the checkpoint gate never opened - #825

Merged
nockawa merged 1 commit into
mainfrom
fix/checkpoint-counter-leaks
Aug 15, 2026
Merged

nockawa merged 1 commit into
mainfrom
fix/checkpoint-counter-leaks

Conversation

@nockawa

@nockawa nockawa commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Fixes #817. Found while hardening with the SpaceBattle demo.

The bug

ArchetypeClusterState.DrainPendingClusterFinalizations opened two ChunkAccessors, read each drained chunk via GetChunkAddress(chunkId, dirty: true), and returned without disposing either. That dirty flag registers an ActiveChunkWriter on the chunk's page, and the registration is released only by CommitChanges/Dispose — so every drained cluster left one behind permanently.

The consequence lands three layers away, in a subsystem that never mentions clusters:

migration leaks ACW → page permanently ACW≠0 → CP-11 skips it → CK-03 gates every cycle
                    → CheckpointLSN never advances → CK-04 recycles no WAL segment
                    → log grows at the full write rate → writer stalls → WalBackPressureTimeout

22 GB of WAL across 86 segments in four minutes, against a 120 MB data file.

Evidence

Measured with the workload quiescent — tick loop stopped, no transaction open:

skipped 458   repeated-from-previous-cycle 450   longest single-page streak 40
causes: ACW 24738   writer-held>100ms 0   stale-counter 0
QUIESCENT non-zero ACW: 458 of 458    139:acw=1  140:acw=2  163:acw=4  186:acw=11

Every skipped page still held registrations with nothing running; one held eleven. Isolated to migration by A/B (identical config, one giant cell ⇒ zero migrations):

migrations/tick quiescent ACW ≠ 0 gated cycles
cellSize=2000 24 45 of 45 12
cellSize=200000 0 0 of 0 0

The fix and its effect

Two usings. Same workload, 40 checkpoints, 51 migrations/tick:

before after
Segments recycled 0 11
Consecutive gated cycles 40 0
Pages skipped 458 0
Quiescent non-zero ACW 458 of 458 0 of 0
WAL after 2000 ticks 22 GB / 86 files 2.3 GB / 9 files

CP-13 — the invariant that was missing, not just untested

CP-11 constrains when ACW may be observed non-zero; CP-12 when the decrement may be deferred. Neither required that it ever happen. That is why a leaked registration was legal and why 5 000+ tests walked past it.

ClusterFinalization_ReleasesEveryActiveChunkWriter verifies it — empties a cluster, then asserts no page holds a registration once nothing is in flight. Red without the fix (Expected: 0, But was: 1). Quiescence is the whole trick: while a workload runs, a leaked counter is indistinguishable from a busy one.

Also included: a quarantined oracle for #824

ChangeSetDirtyMarkConservationTests lands deliberately red under [Category("Quarantine")], tracking #824 — a second, unrelated counter defect found in the same session. DirtyCounter inflates by K−1 per checkpoint cycle when K units of work touch one page, so pages become permanently unevictable and the cache eventually starves with PageCacheBackpressureTimeout.

It proves that in 119 ms where the demo needs half an hour, and its remarks record the fix that looks right and is not: acking the observed DirtyCounter at capture turns all four arms green and breaks FlipByteInPage_IsDetected, Truncation_IsDetected and CleanShutdownReopenDoesNotRederive — it consumes marks still owned by in-flight units of work, which is #385's lost write. Whoever takes #824 starts with a gate that rejects the naive fix instead of a demo that takes thirty minutes to say so.

The four page-counter census helpers on PagedMMF exist for these assertions and are read only at quiesce.

Verification

Not included: the demo itself (demo/SpaceBattle) and the investigation scaffolding used to find this — ACW/DC stack-trace ledgers, skip-cause counters, skip-identity diagnostics. They did their job and shouldn't outlive it; the reproduction is recorded in #817 and #824.

…o the checkpoint gate never opened

The method opened a ChunkAccessor over the cluster segment and another over the transient segment, read each drained chunk through GetChunkAddress(chunkId, dirty: true), and returned without disposing either. That dirty flag registers an ActiveChunkWriter on the chunk's page and the registration is released only by CommitChanges or Dispose, so every drained cluster left one behind permanently. Measured on the SpaceBattle demo: 458 of 458 skipped pages still held writer registrations with the simulation stopped, one of them eleven.

The consequence lands three layers away. CP-11 skips any page with ActiveChunkWriters non-zero, and CK-03 gates the whole cycle when even one page is skipped, so the coverage gate never opened again for the life of the process: CheckpointLSN never advanced, CK-04 recycled no WAL segment, and the log grew at the full write rate until the writer stalled and the engine raised WalBackPressureTimeout from inside the tick fence. Twenty-two gigabytes across eighty-six segments in four minutes, against a hundred-and-twenty megabyte data file. Adding using to both accessors takes segments recycled from 0 to 11 and consecutive gated cycles from 40 to 0 on the same workload.

CP-13 records the invariant that was missing rather than merely absent from the tests. CP-11 says when ActiveChunkWriters may be observed non-zero and CP-12 says when the decrement may be deferred; neither required that it ever happen, which is why a leaked registration was legal and why five thousand tests walked past it. ClusterFinalization_ReleasesEveryActiveChunkWriter verifies it by emptying a cluster and asserting no page holds a registration once nothing is in flight, and it is red without the fix. Quiescence is the whole trick: while a workload is running a leaked counter is indistinguishable from a busy one.

ChangeSetDirtyMarkConservationTests lands quarantined and deliberately red against #824, a second and unrelated counter defect found in the same session. DirtyCounter inflates by K-1 every checkpoint cycle when K units of work touch one page, so pages become permanently unevictable and the page cache eventually starves. The oracle proves it in a hundred and nineteen milliseconds where the demo needs half an hour, and its remarks record the fix that looks right and is not: acking the observed DirtyCounter at capture turns all four arms green and breaks three durability tests, because it consumes marks still owned by in-flight units of work, which is the lost write of #385.

The four page-counter census helpers on PagedMMF exist for those assertions and are read only with the workload quiescent.
@nockawa
nockawa merged commit bc399e1 into main Aug 15, 2026
17 checks passed
@nockawa
nockawa deleted the fix/checkpoint-counter-leaks branch August 15, 2026 22:31
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.

Spatial cluster migration leaks ActiveChunkWriters — checkpoint gate never opens, WAL grows unbounded until the process dies

1 participant