Skip to content

fix: eliminate TOCTOU race in BufferPool memory cap backpressure - #3029

Merged
oferchen merged 2 commits into
masterfrom
feature/zstd-lz4-per-token-codec
Mar 28, 2026
Merged

oferchen merged 2 commits into
masterfrom
feature/zstd-lz4-per-token-codec

Conversation

@oferchen

@oferchen oferchen commented Mar 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Fix TOCTOU race in BufferPool memory cap that caused memory_cap_backpressure_multiple_waiters to hang indefinitely on Windows CI
  • Replace separate wait_for_capacity + track_checkout with atomic wait_and_reserve using CAS fast path and mutex-guarded slow path
  • Switch notify_one to notify_all so all blocked waiters re-evaluate after a buffer return

Root cause

Gap between wait_for_capacity() (checks outstanding + requested <= limit) and track_checkout() (increments outstanding) allowed multiple threads to pass the check simultaneously, pushing outstanding past the cap and causing deadlock.

Test plan

  • memory_cap_backpressure_multiple_waiters no longer hangs on Windows
  • All existing buffer pool tests pass
  • All CI checks pass (fmt, clippy, nextest, Windows, macOS, Linux musl)

@github-actions github-actions Bot added the enhancement New feature or request label Mar 28, 2026
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
oferchen force-pushed the feature/zstd-lz4-per-token-codec branch from 8b1620d to 5e9cbd6 Compare March 28, 2026 08:16
@oferchen oferchen changed the title feat: add zstd/lz4 per-token compression codecs and fix BufferPool TOCTOU race fix: eliminate TOCTOU race in BufferPool memory cap backpressure Mar 28, 2026
@github-actions github-actions Bot added the bug Something isn't working label Mar 28, 2026
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
oferchen merged commit 5d7dee2 into master Mar 28, 2026
34 checks passed
@oferchen
oferchen deleted the feature/zstd-lz4-per-token-codec branch March 28, 2026 08:46
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant