Backport(v1.19): fix(buffer): decrement queue_size in ensure so failed purge does not leak (#5475) - #5487
Merged
Conversation
…leak (#5475) **Which issue(s) this PR fixes**: Fixes #5468 **What this PR does / why we need it**: In `Fluent::Plugin::Buffer#purge_chunk`, `@queue_size_metrics` is only decremented inside `begin/rescue`. When `chunk.purge` raises (e.g. unlink/close failure on the buffer path), the error is logged and swallowed but the `sub` is skipped. The chunk is already removed from `@dequeued`, so it is never retried — the queued byte counter ratchets toward `total_limit_size`, making `storable?` permanently `false` and causing a spurious `BufferOverflowError` on a near-empty buffer. This moves `bytesize = chunk.bytesize` above the `begin` and moves `@queue_size_metrics.sub(bytesize)` into an `ensure` block so the counter is always released exactly once per `purge_chunk` call, regardless of whether `purge` succeeds. Why safe: - `ensure` runs exactly once per call → no double-decrement. - Every chunk reaching `purge_chunk` was previously enqueued (dequeue/emit path), so the `sub` is always balanced. - If `chunk.purge` raises, the chunk is lost on filesystem error (already a logged, separate failure); this change only preserves counter integrity so `storable?` no longer spuriously goes false. **Docs Changes**: None (behavior fix; inline comment added). **Release Note**: * buffer: fix spurious `BufferOverflowError` caused by `queue_size` leaking when a chunk purge fails --------- Signed-off-by: Mehrdad Biukian Naeini <mehrdadbiukian@gmail.com> Co-authored-by: Mehrdad Biukian <mehrdad@example.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Watson1978
approved these changes
Sep 7, 2026
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.
Which issue(s) this PR fixes:
Backport #5475
Fixes #5468
What this PR does / why we need it:
In
Fluent::Plugin::Buffer#purge_chunk,@queue_size_metricsis only decremented insidebegin/rescue. Whenchunk.purgeraises (e.g. unlink/close failure on the buffer path), the error is logged and swallowed but thesubis skipped. The chunk is already removed from@dequeued, so it is never retried — the queued byte counter ratchets towardtotal_limit_size, makingstorable?permanentlyfalseand causing a spuriousBufferOverflowErroron a near-empty buffer.This moves
bytesize = chunk.bytesizeabove thebeginand moves@queue_size_metrics.sub(bytesize)into anensureblock so the counter is always released exactly once perpurge_chunkcall, regardless of whetherpurgesucceeds.Why safe:
ensureruns exactly once per call → no double-decrement.purge_chunkwas previously enqueued (dequeue/emit path), so thesubis always balanced.chunk.purgeraises, the chunk is lost on filesystem error (already a logged, separate failure); this change only preserves counter integrity sostorable?no longer spuriously goes false.Docs Changes:
None (behavior fix; inline comment added).
Release Note:
BufferOverflowErrorcaused byqueue_sizeleaking when a chunk purge fails