GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][client] Release a timed-out send's memory and permit before completing it by merlimat · Pull Request #27567 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Motivation

ProducerMemoryLimitTest.testProducerTimeoutMemoryRelease passes only on retry in CI (4 runs, 3 of them on 2026-11-10, including master run 42765695070). Each first attempt fails after about 5.2 s with expected [0] but found [11]. 11 bytes is the "memory-test" payload.

It's a client bug, not a test bug. When the send timeout fires, ProducerImpl.failPendingMessages(null, ex) completes each queued op's callback first. Only after that does it release the op's memory, and it releases the pending-queue permits after the whole queue is drained:

for (int i = 0; i < pendingMessagesToFailCount; i++) {
    OpSendMsg op = pendingMessages.remove();
    releaseCount.addAndGet(batchMessagingEnabled ? op.numMessagesInBatch : 1);
    ...
    op.sendComplete(ex);                                                  // completes the user's future
    ...
    client.getMemoryLimitController().releaseMemory(op.uncompressedSize); // memory released afterwards
    releaseOpCmdAndRecycle(op);
}
semaphoreRelease(releaseCount.get());                                     // permits released after the loop

op.sendComplete(ex) calls future.completeExceptionally(...). That wakes the thread blocked in producer.send(), and the test then reads memoryLimitController.currentUsage() while the timer thread has not yet reached releaseMemory. On a loaded CI runner the timer thread loses the CPU in that gap.

This affects applications too, not just the test: a send callback that retries (the loop deliberately keeps "newly added messages by synchronous retries") still finds the failed message's memory and permit held. With a full memory limit or queue, the retry gets MemoryBufferIsFullError / ProducerQueueIsFullError, or blocks with blockIfQueueFull. Every other completion path (acks, failSendOp, checksum and not-allowed errors) already releases first, through releaseSemaphoreForSendOp.

Modifications

  • failPendingMessages now calls releaseSemaphoreForSendOp(op) for each op before it completes that op's callback. This releases the same permit and memory totals as before, just earlier, and the batched releaseCount goes away.
  • The failSendOp javadoc no longer says that failPendingMessages returns its permits together after draining the queue.
  • New unit test: ProducerImplTest.testFailPendingMessagesReleasesAccountingBeforeCompletingSends. It queues two unbatched sends on a producer that isn't connected, then calls failPendingMessages(null, TimeoutException). Each send's whenComplete records memory usage and available permits at the moment it completes.

Verifying this change

  • The new test fails without the fix. Each send's completion still sees its own accounting held, so memory reads [20L, 10L] instead of [10L, 0L] (and permits read [8, 8] instead of [9, 10]). It passes with the fix. It is deterministic: the stage runs on the failing thread as the future completes.
  • ProducerImplTest (102 tests), plus ProducerMemoryLimitTest, ProducerSemaphoreTest and ProducerMemoryLeakTest in pulsar-broker: all pass. ./gradlew quickCheck passes.

Follow-up, not in this PR: the incompatible-schema branch of the pending-queue resend (op.sendComplete(new IncompatibleSchemaException(...)) followed by releaseSemaphoreForSendOp(op)) has the same ordering.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merlimat added 2 commits November 10, 2026 11:53
@merlimat
[fix][client] Release a timed-out send's memory and permit before com…
ae8ba50
…pleting it

ProducerImpl.failPendingMessages, which the send timeout uses to fail the
queued messages, completed each op's callback first and only then gave
its memory back to the MemoryLimitController (and its permits back to the
pending-queue semaphore after the whole queue). A caller woken by the
failed send, or retrying from its callback, could still see them held.
This is what makes ProducerMemoryLimitTest.testProducerTimeoutMemoryRelease
flaky (expected [0] but found [11]).

Release each op's accounting with releaseSemaphoreForSendOp before
completing its callback, as the ack and the other failure paths do.
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/producer-fai…
0c73c76
…l-pending-release-before-callback
codelipenghui
codelipenghui approved these changes Nov 10, 2026
View reviewed changes
@codelipenghui
codelipenghui merged commit 0c0cb36 into apache:master Nov 11, 2026
82 of 84 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@merlimat
merlimat deleted the mmerli/producer-fail-pending-release-before-callback branch November 11, 2026 04:22
lhotari pushed a commit that referenced this pull request Nov 11, 2026
@merlimat @lhotari
[fix][client] Release a timed-out send's memory and permit before com…
43efbd0
…pleting it (#27567)

(cherry picked from commit 0c0cb36)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 11, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][client] Release a timed-out send's memory and permit before com…
80b2931
…pleting it (apache#27567)

(cherry picked from commit 0c0cb36)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@merlimat @lhotari
[fix][client] Release a timed-out send's memory and permit before com…
04ed0e2
…pleting it (apache#27567)

(cherry picked from commit 0c0cb36)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@merlimat @codelipenghui @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.