GeezerVison is showing github.com — [fix][broker] Release the compaction buffers and permits on flush failures by nodece · Pull Request #26576 · apache/pulsar
Conversation
nodece
commented
Sep 14, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation
The compaction flush path leaks buffers and permits on failure:
StrategicTwoPhaseCompactor.flushBatchMessage(): the serialized batch buffer is owned solely by the local variable once toByteBuf() returned (the container has already cleared itself), but a failure before the BookKeeper hand-off completes — an interrupted outstanding.acquire(), a throwing stats update, or asyncAddEntry failing synchronously — only discarded the (already empty) container: the serialized buffer leaked on every failed flush.
- The outstanding-writes permit taken before such a failure was never returned either (the write callback that owns the release never runs), permanently shrinking the limit until compaction stalls in
acquire().
RawBatchMessageContainerImpl.toByteBuf(): the output buffer is allocated before the header writes, but a failure between the allocation and the return left it orphaned — the finally released the intermediate buffers only.
Modifications
flushBatchMessage() releases the serialized buffer when the hand-off never happened (it is nulled after the successful hand-off, when BookKeeper owns it) and returns the permit when it was acquired — an acquire() that itself throws must not release one it never took, or the outstanding-writes limit silently grows.
- The write callback's metrics update is caught: the callback can run inline inside
asyncAddEntry, and an exception escaping it would reach the caller's catch, releasing the permit and the buffer a second time after the write already took them. With the callback unable to throw, an exception from asyncAddEntry itself can only mean the callback never ran — verified against BookKeeper 4.18.0, whose synchronous throws in doAsyncAddEntry all precede any callback execution — which is exactly the case the caller's catch handles.
toByteBuf() releases the output buffer in its finally as well, nulled on the success path where its ownership moved to the returned buffer; its allocation goes through the instance allocator (the default constructor already uses PulsarByteBufAllocator.DEFAULT), which is what lets the regression test track it.
Verifying this change
- Make sure that the change passes the CI checks.
This change added tests and can be verified as follows: RawBatchMessageContainerImplTest#testToByteBufReleasesTheOutputBufferWhenSerializationFails (fails with "expected [0] but found [1]" on the previous code). The compactor fixes have no direct test seam (private flush method, inline container); the reasoning is stated above.
Does this pull request potentially affect one of the following parts:
- Dependencies (does it pull in a new dependency?): no
- The public API: no
- The schema registry: no
- The default values of configurations: no
- The wire protocol: no
- The rest endpoints: no
- The admin cli options: no
- Anything that affects deployment: no
Documentation
-
no-doc needed (internal leak fixes on the compaction flush path, no user-facing behavior or configuration change)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…lures
Motivation
Found in the review of apache#26455, on the compaction flush path:
- StrategicTwoPhaseCompactor.flushBatchMessage(): the serialized batch
buffer is owned solely by the local variable once toByteBuf()
returned (the container has already cleared itself), but a failure
before the BookKeeper hand-off completes - an interrupted
outstanding.acquire(), a throwing stats update, or asyncAddEntry
failing synchronously - only discarded the (already empty)
container: the serialized buffer leaked on every failed flush.
- The outstanding-writes permit taken before such a failure was never
returned either (the write callback that owns the release never
runs), permanently shrinking the limit until compaction stalls in
acquire().
- RawBatchMessageContainerImpl.toByteBuf(): the output buffer is
allocated before the header writes, but a failure between the
allocation and the return left it orphaned - the finally released
the intermediate buffers only.
Modifications
- flushBatchMessage() releases the serialized buffer when the hand-off
never happened (it is nulled after the successful hand-off, when
BookKeeper owns it) and returns the permit when it was acquired -
an acquire() that itself throws must not release one it never took,
or the outstanding-writes limit silently grows.
- The write callback's metrics update is caught: the callback can run
inline inside asyncAddEntry, and an exception escaping it would reach
the caller's catch, releasing the permit and the buffer a second
time after the write already took them. With the callback unable to
throw, an exception from asyncAddEntry itself can only mean the
callback never ran - verified against BookKeeper 4.18.0, whose
synchronous throws in doAsyncAddEntry all precede any callback
execution - which is exactly the case the caller's catch handles.
- toByteBuf() releases the output buffer in its finally as well,
nulled on the success path where its ownership moved to the returned
buffer; its allocation goes through the instance allocator (the
default constructor already uses PulsarByteBufAllocator.DEFAULT),
which is what lets the regression test track it.
Verification
- Regression test: a tracked output buffer whose final write throws
must be released (fails with "expected [0] but found [1]" on the
previous code).
- The compactor fixes have no direct test seam (private flush method,
inline container); the reasoning is stated above.
Assisted-by: Claude Code
- the output buffer is allocated before the header writes, but a failure between the allocation and the return left it orphaned
a meta-question: are you running into allocation failures? Perhaps that should be addressed as the primary solution instead of handling all possible code paths where allocation could fail. A recommended setting is to crash the broker when memory allocation fails instead of trying to continue operations.
We do have this setting in bin/pulsar by default, but perhaps it doesn't work?
|
# These two settings work together to ensure the Pulsar process exits immediately and predictably |
|
# if it runs out of either Java heap memory or its internal off-heap memory, |
|
# as these are unrecoverable errors that require a process restart to clear the faulty state and restore operation |
|
OPTS="-XX:+ExitOnOutOfMemoryError -Dpulsar.allocator.exit_on_oom=true $OPTS" |
There's a lot of discussion on #25021 about addressing Netty allocator problems.
In particular, the comment #25021 (comment)
more in #25522 (comment)
One of the gaps in the broker is this one:
#24926
The problem itself is also referred in the OOM issue.
Focusing on fixing the root cause could be more effective in the long term.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lhotari
commented
Sep 14, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
One source of OOM problems in dispatching might be caused by the default dispatcherDispatchMessagesInSubscriptionThread=true setting. The default causes slower performance and higher resource consumption as well. There has been discussions with @merlimat to change the default to dispatcherDispatchMessagesInSubscriptionThread=false.
The way how dispatcherDispatchMessagesInSubscriptionThread=true causes problems for memory consumption is that the entries are held until the entries are dispatched (and written to the socket). This is also related to #24926. If the entries are sitting in the outgoing buffer, the memory won't be released.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
PR to change dispatcherDispatchMessagesInSubscriptionThread=false as the default: #26578
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
nodece
commented
Sep 15, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Agreed that crashing on allocation failure is the right primary strategy — and the default does work: PulsarByteBufAllocator registers an OOM listener that calls triggerImmediateForcefulShutdown() when pulsar.allocator.exit_on_oom=true (PulsarByteBufAllocator.java:88-98), and bin/pulsar sets both flags by default. The nuance from #25021 stands though: pooled-allocator exhaustion doesn't always surface as a clean OutOfMemoryError.
This PR is defensive hardening for the failure paths that crash-on-OOM cannot cover — the non-allocation triggers (an interrupted outstanding.acquire() on compaction cancellation, a throwing stats update, asyncAddEntry failing synchronously on a concurrently closed ledger, an exception escaping a synchronously-executed write callback), where the old code leaked the serialized buffer and permanently consumed an outstanding-writes permit (compaction would stall in acquire() once all 500 are gone). The toByteBuf() output-buffer leak is allocation-scoped and unreachable under the exit-on-OOM default — belt-and-suspenders for deployments launched without the bin/pulsar defaults, and for the #25021 case. The callback guard is what makes these releases exactly-once by construction.
I'd suggest keeping the changes — they are small, verified, and turn the cleanup on these paths from best-effort into structural — but happy to drop the allocation-scoped part if you prefer to rely on the crash-on-OOM strategy alone.
(Noted the dispatcherDispatchMessagesInSubscriptionThread context — following #26578.)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Sep 20, 2026
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Sep 29, 2026
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Sep 29, 2026
… WrappedByteBuf visibility
Same treatment as the pulsar-client tests in apache#26455: bridge the package-private
WrappedByteBuf from a class under io.netty.buffer so FailingWriteByteBuf can
subclass it.
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 16, 2026
Reviewers
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|