[fix][client] Release the reserved memory only once when failing a send in a terminal state - #26476
Open
SongOf wants to merge 1 commit into
Open
Conversation
…nd in a terminal state
nodece
approved these changes
Sep 8, 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.
Motivation
ProducerImpl#processOpSendMsgfails a message immediately when the producer is in a terminalstate (
Terminated/Closed/ProducerFenced) instead of leaving it inpendingMessagesuntil the send timeout fires. That path releases the memory reserved for the message twice:
releaseSemaphoreForSendOp(op)already returnsop.uncompressedSizeto theMemoryLimitController, so the explicit call right after it releases the same bytes a secondtime.
MemoryLimitControlleronly validates that the argument is non-negative, not that the runningtotal stays sane, so this fails silently: every message published after the producer reaches a
terminal state drives
currentUsagefurther below zero. Once it is negative,tryReserveMemory()never returnsfalseagain andmemoryLimitBytesis effectively disabledfor the whole client — every producer and consumer sharing that
PulsarClientloses its memorylimit, which is exactly the protection that is supposed to keep the client from running out of
memory. An application that keeps publishing to a terminated topic, or that publishes after the
producer was fenced, hits this on every send.
This is a regression from #25317, which introduced the terminal-state fast-fail branch.
Modifications
ProducerImpl#processOpSendMsg: drop the redundantclient.getMemoryLimitController().releaseMemory(op.uncompressedSize)from the terminal-statebranch, leaving the single release performed by
releaseSemaphoreForSendOp(op). Added a shortcomment recording that
releaseSemaphoreForSendOp()already gives the reserved memory back, sothe call is not re-added later.
No other path changes: the semaphore permits, the send callback, the
ByteBufPairrelease andthe
OpSendMsgrecycling in that branch are untouched.Verifying this change
This change added tests and can be verified as follows:
ProducerImplTest#testProcessOpSendMsgInTerminalStateReleasesMemoryOnce— reserves a knownnumber of bytes in a real
MemoryLimitController, drivesprocessOpSendMsg()once for each ofthe three terminal states (
Terminated,Closed,ProducerFenced), and asserts thatcurrentUsage()returns to exactly0. On the unpatched code it fails withexpected [0] but found [-128], i.e. for the real reason — the double release — rather than byforcing internal state. The
OpSendMsgis built through the batched factory so thatop.msgisnull, which keeps the test on the memory-accounting path and out of the batch-flush andmessage-size checks.
Local run:
The whole
:pulsar-client-original:testmodule also passes.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
This is an internal bug fix in the client send path. It changes no public API, schema,
configuration default, wire protocol, REST endpoint, CLI option or metric; it restores the
intended behaviour of the existing
memoryLimitBytessetting rather than changing it.