Skip to content

Close the requester and temporary publisher when a send fails synchronously - #3789

Merged
artembilan merged 2 commits into
spring-projects:mainfrom
sharanggupta:amqp-template-close-on-sync-failure
Oct 28, 2026
Merged

artembilan merged 2 commits into
spring-projects:mainfrom
sharanggupta:amqp-template-close-on-sync-failure

Conversation

@sharanggupta

Copy link
Copy Markdown
Contributor

RabbitAmqpTemplate closes the per-request Requester (and, with a RoutingAmqpConnectionFactory, the per-call Publisher) only in whenComplete() of the publish future, so when message conversion or publish() throws synchronously the future never exists and the requester's reply queue, consumer and sender link stay open on the connection. This closes those resources on the synchronous failure path and rethrows.

…nously

`RabbitAmqpTemplate` closes the per-request `Requester` and, for a routing
connection factory, the per-call `Publisher` only from the completion of the
publish future. When message conversion or `publish()` throws, that future
never completes and the client resources stay open on the connection.

* Close the `Requester` and rethrow when `doSendAndReceive()` fails before
the future exists
* Close a temporary `Publisher` the same way in `doSend()`
* Add `RabbitAmqpTemplateUnitTests` with mocked client resources

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
@artembilan artembilan added this to the 4.2.0 milestone Oct 28, 2026

@Test
void requesterIsClosedWhenPublishFails() {
AmqpConnectionFactory connectionFactory = mock();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK. Stop it, please.
I think most of the cases really could be covered with real broker interaction.
We would need some mock for peculiar cases or unreachable API.

A request that cannot be converted must not leave the reply consumer attached.
Verify this with a real broker in `RabbitAmqpTemplateTests` and keep the mock
test only for the temporary publisher, which the broker does not expose.

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
@artembilan
artembilan merged commit 0de7813 into spring-projects:main Oct 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants