Repository navigation
#624 Fix an ABA in the WAL commit buffer's back-pressure wait that wedged producers until their deadline - #646
Merged
Merged
Conversation
…dged producers until their deadline A producer whose claim overflows the active buffer parks in WalCommitBuffer.TryClaim until a swap gives it a fresh one. It waited on `_activeBufferIndex`, which ping-pongs 0<->1 — an ABA test on a single bit. A producer descheduled while TWO swaps complete finds the index back at the value it captured and goes on waiting for an edge that has already passed twice. Nothing recovers it: the only thing that advances the index is another producer overflowing the buffer, so once every live producer is parked that way the buffer sits drained and idle (swapState=Normal, inflight=0, drainPos==tail, space available) until each WaitContext deadline expires and throws WalBackPressureTimeoutException on a commit that could have proceeded throughout. Producers now wait on `_swapGeneration`, a 64-bit monotonic counter incremented once per completed swap, sampled under the claim gate — the Dekker pair with SwapDraining is what guarantees no swap can complete between the sample and the park, so the sampled value is necessarily the generation the claim belongs to. PerformSwap increments it LAST, after the new index, the reset claim word and the folded LSN base, so observing a new generation implies observing everything it depends on. A producer that backed out at the claim gate holding nothing no longer waits on a swap at all — it is owed none, and any generation it could sample is already racing the in-progress swap. It waits only while `_swapState == SwapDraining`, then retries. Both arms of the predicate are edge-free by construction. Found by the macOS arm64 nightly, and it is NOT arm64-specific: it reproduces on x64/Windows under a 2-CPU affinity mask. Core count is the variable, not architecture — 1 CPU passes (strict serialisation always leaves another producer runnable to trigger the next swap), 2 CPUs hang, 3 CPUs pass. The 16-core gate's hardware is exactly what hid it. The loop's bounded `_swapCompletedEvent.Wait(2ms)` backstop did not rescue this and was never going to. A backstop caps a lost WAKE-UP; a missed generation is not re-observable however often the condition is re-checked. Also fixes the reporting bug that cost us evidence every time this fired: both ThrowWalBackPressureTimeout call sites passed `ctx.Deadline.Remaining`, which is TimeSpan.Zero by definition once ShouldStop is true, so the exception could only ever read "timeout after 0ms". Deadline stores the expiry timestamp and not the start, so the waiter has to measure — now Stopwatch.GetElapsedTime over the wait. The `WaitForData(5)` added to this fixture's consumer in f4c7bef was previously credited with fixing these CI failures. It did not; it only narrowed the window. That comment is corrected rather than left to mislead. Test: BackPressureWait_ProducerLappedByTwoSwaps_IsNotStillWaiting drives two swaps deterministically and asserts the predicate directly, rather than trying to lose a scheduling race. Verified red against the old index compare (the producer is never released) and green against the fix. Verification: pinned 2-CPU repro was 5/8 failing, now 10/10 and 6/6 green — 20s deadline burn down to 45ms. Full engine suite green in Debug and Release (4108 passed, 0 failed). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PDPx9FsFqDx3stceu2Rbv3
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.
Closes #647
The first real catch from the macOS arm64 nightly (#624) — and it turned out not to be an arm64 problem at all.
The bug
A producer whose claim overflows the active WAL buffer parks in
WalCommitBuffer.TryClaimuntil a swap gives it a fresh one. It waited on_activeBufferIndex:That index ping-pongs 0↔1. It is an ABA test on a single bit. A producer descheduled while two swaps complete finds the index back at the value it captured and goes on waiting for an edge that has already passed — twice.
Nothing recovers it. The only thing that advances the index is another producer overflowing the buffer, so once every live producer is parked that way the buffer sits drained and idle until each
WaitContextdeadline expires and throwsWalBackPressureTimeoutExceptionon a commit that had space available the whole time.The frozen state from an instrumented run is the whole story:
Not arm64 — core count
It reproduces on x64/Windows under a 2-CPU affinity mask. The variable is core count, not architecture:
The 16-core gate's hardware is precisely what hid this. The 3-core hosted macOS runner is what surfaced it.
The fix
Producers wait on
_swapGeneration— a 64-bit monotonic counter incremented once per completed swap, sampled under the claim gate. The Dekker pair withSwapDrainingguarantees no swap can complete between the sample and the park, so the sampled value is necessarily the generation the claim belongs to.PerformSwapincrements it last — after the new index, the reset claim word and the folded LSN base — so observing a new generation implies observing everything it depends on.A producer that backed out at the claim gate holding nothing no longer waits on a swap at all. It is owed none, and any generation it could sample is already racing the in-progress swap — the same missed-edge hazard through a different door. It waits only while
_swapState == SwapDraining, then retries. Both arms of the predicate are edge-free by construction.The loop's bounded
_swapCompletedEvent.Wait(2ms)backstop did not rescue this and was never going to: a backstop caps a lost wake-up; a missed generation is not re-observable however often you re-check.Also in here
ThrowWalBackPressureTimeoutcall sites (WalCommitBuffer,WalWriter) passedctx.Deadline.Remaining, which isTimeSpan.Zeroby definition onceShouldStopis true — so the exception could only ever readtimeout after 0ms.Deadlinestores the expiry timestamp and not the start, so the waiter has to measure. NowStopwatch.GetElapsedTime. This cost us the one number worth having every time the bug fired, and fixing it first is what made the rest of the investigation legible.WaitForData(5)added to this fixture's consumer in f4c7bef was credited with fixing these CI failures. It did not — it only narrowed the window. That comment is corrected rather than left to mislead the next reader.WP-13inclaude/rules/durability.md(requires: WP-06), plus the same point inWAL-Design.md§4.5 where the ping-pong swap is introduced.check-rule-scopes.pypasses. Docs pushed asTyphon-Claude@e4840ca.Testing
BackPressureWait_ProducerLappedByTwoSwaps_IsNotStillWaitingdrives two swaps deterministically and asserts the predicate directly, rather than trying to lose a scheduling race — reproducing the race needed a producer descheduled across two whole swaps, which never happens on a developer box.Verified red against the old index compare (the producer is never released) and green against the fix.
check-rule-scopes.py🤖 Generated with Claude Code
https://claude.ai/code/session_01PDPx9FsFqDx3stceu2Rbv3