Skip to content

Reject async delivery when reply cannot be sent - #3783

Merged
artembilan merged 1 commit into
spring-projects:mainfrom
beyondwin:async-reply-failure
Oct 28, 2026
Merged

artembilan merged 1 commit into
spring-projects:mainfrom
beyondwin:async-reply-failure

Conversation

@beyondwin

Copy link
Copy Markdown
Contributor

When a listener returns a CompletableFuture or a Mono (or is a
Kotlin suspend function) and the result cannot be sent as a reply,
for example because the publish fails or there is no reply address,
the delivery is neither acknowledged nor rejected.
The container switches to MANUAL ack for such listeners, so the message
stays unacked until the consumer is restarted, and once prefetch such
messages accumulate, the consumer stops receiving.
A synchronous listener throws a ReplyFailureException to the
container in the same situation, and the message is requeued by default.

The exception is thrown by asyncSuccess() inside the whenComplete()
callback, so it only completes the derived future, which nobody
observes, and basicAck() is skipped.
Since GH-2608, Mono and suspend results go through this callback
too; before, MonoHandler.subscribe() routed such an exception to the
error callback.

  • Catch a failure of asyncSuccess() and handle it like a failed async
    result: report it on the observation, since it no longer reaches
    scoped(), and settle the delivery via asyncFailure()

The catch that was dropped while reviewing #3646 only reported the exception to the observation, which scoped() already does; this one settles the delivery.
Like any failed async result, it now also reaches the RabbitListenerErrorHandler (so a null result acks it per acknowledgeOnError), which the synchronous path skips for a ReplyFailureException.
AmqpMessagingListenerAdapter in spring-amqp-client has the same gap for CompletableFuture; I can fix it here or in a separate PR.

When a listener returns a `CompletableFuture` or a `Mono` (or is a
Kotlin `suspend` function) and the result cannot be sent as a reply,
for example because the publish fails or there is no reply address,
the delivery is neither acknowledged nor rejected.
The container switches to MANUAL ack for such listeners, so the message
stays unacked until the consumer is restarted, and once `prefetch` such
messages accumulate, the consumer stops receiving.
A synchronous listener throws a `ReplyFailureException` to the
container in the same situation, and the message is requeued by default.

The exception is thrown by `asyncSuccess()` inside the `whenComplete()`
callback, so it only completes the derived future, which nobody
observes, and `basicAck()` is skipped.
Since spring-projectsGH-2608, `Mono` and `suspend` results go through this callback
too; before, `MonoHandler.subscribe()` routed such an exception to the
error callback.

* Catch a failure of `asyncSuccess()` and handle it like a failed async
  result: report it on the observation, since it no longer reaches
  `scoped()`, and settle the delivery via `asyncFailure()`

Signed-off-by: WooSeung Kim <wooseung.dev@gmail.com>
@artembilan artembilan added this to the 4.2.0 milestone Oct 28, 2026
@artembilan
artembilan enabled auto-merge (squash) October 28, 2026 19:30
@artembilan

Copy link
Copy Markdown
Member

Will be merged when PR build is green.
Please, consider to fix spring-amqp-client in the separate PR.

Thank you for the contribution; looking forward for more!

@artembilan
artembilan disabled auto-merge October 28, 2026 20:10
@artembilan
artembilan merged commit de1bcce into spring-projects:main Oct 28, 2026
3 checks passed
artembilan pushed a commit that referenced this pull request Oct 29, 2026
Fixes: #3805

When an `@AmqpListener` method returns a `CompletableFuture` and the
result cannot be sent as a reply, for example because there is no
reply address or the broker does not accept the reply, the delivery
is never settled.
The container adds credit back only on settlement, so once the
initial credits are used up, the consumer stops receiving.
As in `spring-rabbit` before GH-3783, `asyncSuccess()` throws inside
`whenComplete()` and `asyncAck()` is skipped.
For a `Mono` (or Kotlin `suspend`) result, the error callback settles
the delivery, but Reactor then calls the complete callback as well,
and `asyncAck()` settles it a second time.

The reject itself fails, too: the container calls
`delivery.reject(null, null)`, and ProtonJ2 throws a
`NullPointerException` for an `ErrorCondition` without a condition.
The message then stays unacknowledged until the receiver link or
connection is closed.
This affects a manual `acknowledge(Status.REJECT)` and every failed
async `@AmqpListener` result, which is rejected by default.
A `Mono` result whose reply fails is accepted instead, since
`asyncAck()` still runs.

* Settle `REJECT` with `DeliveryState.rejected(null, null)`, as the
  RabbitMQ AMQP 1.0 Java client does for `discard()`
* Handle a failure of `asyncSuccess()` for a `CompletableFuture` like a
  failed future, via `asyncFailure()`
* Do not accept a `Mono` delivery that the error callback has already
  settled

Signed-off-by: WooSeung Kim <wooseung.dev@gmail.com>

**Auto-cherry-pick to `4.1.x`**
spring-builds pushed a commit that referenced this pull request Oct 29, 2026
Fixes: #3805

When an `@AmqpListener` method returns a `CompletableFuture` and the
result cannot be sent as a reply, for example because there is no
reply address or the broker does not accept the reply, the delivery
is never settled.
The container adds credit back only on settlement, so once the
initial credits are used up, the consumer stops receiving.
As in `spring-rabbit` before GH-3783, `asyncSuccess()` throws inside
`whenComplete()` and `asyncAck()` is skipped.
For a `Mono` (or Kotlin `suspend`) result, the error callback settles
the delivery, but Reactor then calls the complete callback as well,
and `asyncAck()` settles it a second time.

The reject itself fails, too: the container calls
`delivery.reject(null, null)`, and ProtonJ2 throws a
`NullPointerException` for an `ErrorCondition` without a condition.
The message then stays unacknowledged until the receiver link or
connection is closed.
This affects a manual `acknowledge(Status.REJECT)` and every failed
async `@AmqpListener` result, which is rejected by default.
A `Mono` result whose reply fails is accepted instead, since
`asyncAck()` still runs.

* Settle `REJECT` with `DeliveryState.rejected(null, null)`, as the
  RabbitMQ AMQP 1.0 Java client does for `discard()`
* Handle a failure of `asyncSuccess()` for a `CompletableFuture` like a
  failed future, via `asyncFailure()`
* Do not accept a `Mono` delivery that the error callback has already
  settled

Signed-off-by: WooSeung Kim <wooseung.dev@gmail.com>

(cherry picked from commit db88c63)
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