Skip to content

Take publish sequence number once the message is encoded - #2170

Merged
acogoluegnes merged 3 commits into
rabbitmq:mainfrom
ultramancode:fix/publish-confirm-seq-rollback
Nov 10, 2026
Merged

acogoluegnes merged 3 commits into
rabbitmq:mainfrom
ultramancode:fix/publish-confirm-seq-rollback

Conversation

@ultramancode

@ultramancode ultramancode commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Proposed Changes

basicPublish takes 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 than frame_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 x in the boxes that apply

  • Bugfix (non-breaking change which fixes issue #NNNN)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (correction or otherwise)
  • Cosmetics (whitespace, appearance)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating
the 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.

  • I have read the CONTRIBUTING.md document
  • I have signed the CA (see https://cla.pivotal.io/sign/rabbitmq)
  • All tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in related repositories

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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@ultramancode ultramancode changed the title Roll back publish sequence number when publish fails before sending Take publish sequence number once the message is encoded Nov 10, 2026
@acogoluegnes
acogoluegnes requested a balanced review from Copilot November 10, 2026 13:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@acogoluegnes
acogoluegnes merged commit 47c18e0 into rabbitmq:main Nov 10, 2026
7 checks passed
@acogoluegnes

Copy link
Copy Markdown
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
@ultramancode

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants