Skip to content

[Mocha] Fix acknowledgements of saga commands without a response landing in the skipped queue - #11051

Merged
PascalSenn merged 2 commits into
mainfrom
ps/mocha-saga-discard-unhandled-acknowledgements
Nov 23, 2026
Merged

PascalSenn merged 2 commits into
mainfrom
ps/mocha-saga-discard-unhandled-acknowledgements

Conversation

@PascalSenn

Copy link
Copy Markdown
Member

Follow-up to #11022.

Problem

Since #11022, the replies to a saga's commands go to the saga's queue, and a reply that no transition handles moves to the saga's skipped queue. A command handled by an IEventRequestHandler<T> without a response is acknowledged with an AcknowledgedEvent to the reply address the saga stamped. No route of the saga selected that acknowledgement unless the saga declared a transition for it, so every such command left one AcknowledgedEvent in {service}.{saga}_skipped.

A saga could not opt out with one declaration. A transition must name a target state, so there is no "accept AcknowledgedEvent in any state and stay there", and an acknowledgement that arrives after the saga ended has no state at all.

Change

  • SagaConsumer adds a saga-id gated reply route for AcknowledgedEvent when the saga has a reply transition and none of its reply transitions already selects acknowledgements (OnAnyReply, OnReply<AcknowledgedEvent>). The route binds to the saga's existing reply endpoint.
  • Saga<TState>.HandleEvent discards an AcknowledgedEvent with a debug log entry when no transition of the current state handles it. The state is not changed, not re-entered and not saved.
  • An AcknowledgedEvent for a saga that has ended is consumed with a debug log entry.
  • The transition lookup of OnHandleTransitionAsync moves to a private FindTransition helper that both paths use.

The alternative, stamping a reply address only on messages with a response type, would break sagas that wait for the acknowledgement of a command with OnAnyReply (Saga_Should_ReceiveAcknowledgement_When_CommandWithoutResponseSucceeds).

Behavior changes

  • An acknowledgement that no transition handles is discarded instead of moved to the skipped queue. Other replies and faults that no transition handles still go to the skipped queue.
  • An acknowledgement that arrives in a state without a transition for it, while another state declares OnAnyReply, is discarded. Before, it threw SagaExecutionException.
  • A saga without a reply transition is unchanged. Its acknowledgements still go to the instance reply queue.

Tests

  • Saga_Should_MoveReplyToSkippedQueue_When_NoReplyTransitionMatches (Postgres) asserted the acknowledgement in the skipped queue. It now uses a typed reply that no transition handles.
  • New Postgres tests leave every queue empty when a saga sends a command without a response, during the saga and on the transition into its final state. Both fail without this change, with the acknowledgement in the skipped queue.
  • New unit tests for the discard and for the acknowledgement route.

…ing in the skipped queue

A saga with a reply transition receives the replies to its commands on its own
endpoint. A command handled by an `IEventRequestHandler<T>` without a response
is acknowledged with an `AcknowledgedEvent`, which no route of the saga
selected unless the saga declared a transition for it, so every such
acknowledgement was moved to the saga's skipped queue.

The saga consumer now adds a saga-id gated reply route for `AcknowledgedEvent`
when the saga has a reply transition and none of them selects acknowledgements.
The saga discards an acknowledgement with a debug log entry when no transition
of its current state handles it, or when the saga has ended. Other replies
that no transition handles still go to the skipped queue.
@PascalSenn PascalSenn added 📚 documentation This issue is about working on our documentation. 🌶️ mocha labels Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Patch coverage

100.0% of changed lines covered (38/38)

File Covered Changed Patch %
src/Mocha/src/Mocha/Sagas/Saga.cs 24 24 100.0% 🟢
src/Mocha/src/Mocha/Sagas/SagaEventListener.cs 14 14 100.0% 🟢

Project coverage: 60.1% (330776/550329 lines)

@PascalSenn
PascalSenn marked this pull request as ready for review November 23, 2026 16:18
@PascalSenn
PascalSenn merged commit f70ab07 into main Nov 23, 2026
159 checks passed
@PascalSenn
PascalSenn deleted the ps/mocha-saga-discard-unhandled-acknowledgements branch November 23, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📚 documentation This issue is about working on our documentation. 🌶️ mocha

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant