Repository navigation
Take publish sequence number once the message is encoded - #2170
Merged
acogoluegnes merged 3 commits intoNov 10, 2026
Merged
Conversation
basicPublish takes the next sequence number before it validates the message. If validation fails, nothing reaches the broker, but the number stays used and the channel stays open. Later acks and nacks on the channel are then matched to the wrong messages. This happens with a null routing key, an unsupported header value, headers larger than frame_max, or a routing key longer than 255 bytes. Build the publish method before taking the number, and roll the number back when serializing the message fails with an IllegalArgumentException. Other runtime exceptions keep the number, because they can come from user callbacks after the message was sent. Signed-off-by: Taewoong Kim <ktw2172@gmail.com>
There was a problem hiding this comment.
🟡 Changes recommended
The rollback can remove an already-sent publish and reuse its sequence number, causing confirms to match the wrong messages, while missing notifications can leave waitForConfirms() blocked indefinitely.
2 open issues
What changed in this PR
This PR aims to keep publisher-confirm sequence numbers aligned with the broker when a publish fails before sending.
Changes:
- Builds the publish method before allocating a sequence number
- Rolls back confirm tracking when publishing throws
IllegalArgumentException - Adds regression coverage for unsupported headers, null routing keys, and subsequent confirms
| File | Description |
|---|---|
src/test/java/com/rabbitmq/client/test/ChannelNTest.java |
Tests sequence-number preservation after rejected publishes |
src/main/java/com/rabbitmq/client/impl/ChannelN.java |
Moves method construction before sequence allocation and adds rollback handling |
Review effort: Balanced
Take the number once the command is encoded, right before its first frame is written, instead of rolling it back on IllegalArgumentException. A failure before that point does not use a number, whatever the exception type. A failure after it, for example from a write listener or an observation handler, keeps the number, because the message was sent. Only acks and nacks remove numbers from the unconfirmed set. Signed-off-by: Taewoong Kim <ktw2172@gmail.com>
There was a problem hiding this comment.
🟡 Changes recommended
A regression test is needed to verify that a routing key exceeding 255 UTF-8 bytes fails during frame encoding without consuming sequence number 1, allowing a subsequent valid publish to be confirmed by ack 1.
1 open issue
2 resolved since last review
Review effort: Balanced
It fails while encoding the method frame, right before the sequence number is taken. Signed-off-by: Taewoong Kim <ktw2172@gmail.com>
Contributor
|
@ultramancode Thanks! |
acogoluegnes
pushed a commit
that referenced
this pull request
Nov 10, 2026
* Roll back publish sequence number when publish fails before sending basicPublish takes the next sequence number before it validates the message. If validation fails, nothing reaches the broker, but the number stays used and the channel stays open. Later acks and nacks on the channel are then matched to the wrong messages. This happens with a null routing key, an unsupported header value, headers larger than frame_max, or a routing key longer than 255 bytes. Build the publish method before taking the number, and roll the number back when serializing the message fails with an IllegalArgumentException. Other runtime exceptions keep the number, because they can come from user callbacks after the message was sent. * Take publish sequence number once the message is encoded Take the number once the command is encoded, right before its first frame is written, instead of rolling it back on IllegalArgumentException. A failure before that point does not use a number, whatever the exception type. A failure after it, for example from a write listener or an observation handler, keeps the number, because the message was sent. Only acks and nacks remove numbers from the unconfirmed set. Signed-off-by: Taewoong Kim <ktw2172@gmail.com> (cherry picked from commit 47c18e0) Conflicts: src/main/java/com/rabbitmq/client/impl/ChannelN.java src/test/java/com/rabbitmq/client/test/ChannelNTest.java
Contributor
Author
|
Thanks for the review! |
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.


Proposed Changes
basicPublishtakes the next publish sequence number before it validates the message. If validation fails, nothing is sent to the broker, but the number stays used and the channel stays open.The broker only counts the messages it receives, so later acks and nacks on that channel are matched to the wrong messages. This can happen with a null routing key, a header value of an unsupported type such as
UUID, headers larger thanframe_max, or a routing key longer than 255 bytes.This PR takes the sequence number once the message is encoded, right before its first frame is written. A failure before that point does not use a number, whatever the exception type. A failure after it keeps the number, because the message may already have been sent.
I tested this against RabbitMQ 4.4.0 running in Docker, on Java 8 and 21.
Types of Changes
What types of changes does your code introduce to this project?
Put an
xin the boxes that applyChecklist
Put an
xin the boxes that apply. You can also fill these out after creatingthe PR. If you're unsure about any of them, don't hesitate to ask on the
mailing list. We're here to help! This is simply a reminder of what we are
going to look for before merging your code.
CONTRIBUTING.mddocument