Skip to content

Fix buffer stage size accounting race - #5481

Draft
simonyang08 wants to merge 1 commit into
fluent:masterfrom
simonyang08:codex/fix-stage-size-race
Draft

Fix buffer stage size accounting race#5481
simonyang08 wants to merge 1 commit into
fluent:masterfrom
simonyang08:codex/fix-stage-size-race

Conversation

@simonyang08

Copy link
Copy Markdown

Which issue(s) this PR fixes:
Fixes #5479

What this PR does / why we need it:

Buffer#write previously deferred the staged-byte metric update until after releasing every chunk lock. A concurrent enqueue_chunk could remove the staged chunk and subtract its committed size before the matching addition, making the raw stage_size gauge transiently negative.

This change records committed staged bytes before releasing each chunk monitor. It deliberately does not acquire the Buffer-global lock in that position, preserving the existing lock-order constraint. A deterministic regression test pauses the writer at mon_exit and verifies that the staged bytes transfer to the queue without a negative intermediate gauge.

Docs Changes:

None.

Release Note:

Fix a race that could make the internal buffer stage_size gauge transiently negative while a write and chunk enqueue overlap.

Validation:

  • bundle exec ruby -Itest test/plugin/test_stage_size_race.rb - 1 test, 5 assertions, passed
  • repeated regression test 10 times - passed
  • bundle exec ruby -Itest test/plugin/test_buffer.rb - 73 tests, 431 assertions, passed
  • Ruby syntax checks for both changed files - passed
  • git diff --check - passed
  • bundle exec rake test was also attempted under WSL on /mnt/d; unrelated environment failures were observed around DrvFS permissions, CRLF shebangs, Unix sockets, and unavailable YJIT. The focused buffer suites above are green.

AI assistance:

This change was prepared with OpenAI Codex assistance and reviewed and validated by the contributor.

Signed-off-by: simonyang08 <ppt5928@gmail.com>
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.

buffer: stage_size gauge goes transiently negative when write and enqueue_chunk overlap

2 participants