Repository navigation
fix: eliminate TOCTOU race in BufferPool memory cap backpressure - #3029
Merged
Merged
Conversation
The wait_for_capacity/track_checkout gap allowed multiple threads to pass the capacity check before either incremented outstanding bytes, causing deadlock when outstanding exceeded the cap. Replace with wait_and_reserve using CAS fast path and mutex-guarded slow path that atomically checks and increments outstanding. Use notify_all instead of notify_one so all waiters re-evaluate after a return. Fixes flaky memory_cap_backpressure_multiple_waiters test on Windows.
oferchen
force-pushed
the
feature/zstd-lz4-per-token-codec
branch
from
March 28, 2026 08:16
8b1620d to
5e9cbd6
Compare
Two bugs in wait_and_reserve: 1. Slow path used plain store instead of CAS, racing with the lockless fast path and clobbering concurrent increments - causing outstanding to desync and eventually wrap on subtraction. 2. current + requested overflows in debug mode (nightly) when outstanding has already wrapped to a large value. Use saturating_add for the comparison to avoid panic.
oferchen
added a commit
that referenced
this pull request
May 1, 2026
* fix: eliminate TOCTOU race in BufferPool memory cap backpressure The wait_for_capacity/track_checkout gap allowed multiple threads to pass the capacity check before either incremented outstanding bytes, causing deadlock when outstanding exceeded the cap. Replace with wait_and_reserve using CAS fast path and mutex-guarded slow path that atomically checks and increments outstanding. Use notify_all instead of notify_one so all waiters re-evaluate after a return. Fixes flaky memory_cap_backpressure_multiple_waiters test on Windows. * fix: use CAS in slow path and saturating_add to prevent overflow Two bugs in wait_and_reserve: 1. Slow path used plain store instead of CAS, racing with the lockless fast path and clobbering concurrent increments - causing outstanding to desync and eventually wrap on subtraction. 2. current + requested overflows in debug mode (nightly) when outstanding has already wrapped to a large value. Use saturating_add for the comparison to avoid panic.
oferchen
added a commit
that referenced
this pull request
May 5, 2026
* fix: eliminate TOCTOU race in BufferPool memory cap backpressure The wait_for_capacity/track_checkout gap allowed multiple threads to pass the capacity check before either incremented outstanding bytes, causing deadlock when outstanding exceeded the cap. Replace with wait_and_reserve using CAS fast path and mutex-guarded slow path that atomically checks and increments outstanding. Use notify_all instead of notify_one so all waiters re-evaluate after a return. Fixes flaky memory_cap_backpressure_multiple_waiters test on Windows. * fix: use CAS in slow path and saturating_add to prevent overflow Two bugs in wait_and_reserve: 1. Slow path used plain store instead of CAS, racing with the lockless fast path and clobbering concurrent increments - causing outstanding to desync and eventually wrap on subtraction. 2. current + requested overflows in debug mode (nightly) when outstanding has already wrapped to a large value. Use saturating_add for the comparison to avoid panic.
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.
Summary
BufferPoolmemory cap that causedmemory_cap_backpressure_multiple_waitersto hang indefinitely on Windows CIwait_for_capacity+track_checkoutwith atomicwait_and_reserveusing CAS fast path and mutex-guarded slow pathnotify_onetonotify_allso all blocked waiters re-evaluate after a buffer returnRoot cause
Gap between
wait_for_capacity()(checksoutstanding + requested <= limit) andtrack_checkout()(incrementsoutstanding) allowed multiple threads to pass the check simultaneously, pushing outstanding past the cap and causing deadlock.Test plan
memory_cap_backpressure_multiple_waitersno longer hangs on Windows