Skip to content

fix(microservices): run every event handler when a sibling completes empty - #17961

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/event-handlers-complete-empty
Oct 1, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/event-handlers-complete-empty

Conversation

@johnnyhuirilef

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Issue Number: #17960

Closes #17960

Nest joins the handlers of one event pattern with forkJoin. forkJoin completes without emitting as soon as one source completes empty, and it unsubscribes from the other sources. So a handler that filters the event out cancels a sibling handler that returns a cold Observable. The work of that sibling never runs, and the processing end hook runs before it. The docs say that "Nest triggers all of them in parallel". async handlers are not affected, because Nest starts them eagerly.

Measured with the repro on 12.1.2:

order 1 (first handler completes empty)  ->  reserveStock: side effect never ran
order 2 (first handler emits, baseline)  ->  reserveStock: side effect ran

What is the new behavior?

Each branch of the join defaults to undefined when it completes empty. The join still emits once, after every handler finishes, and the end hook runs last. Nothing reads the joined value, so undefined has no effect on consumers.

An error in a handler still errors the join, and it still cancels the other branches as before. The fix changes one more case, in a good direction. On Kafka, the join was already complete when a sibling raised an error after an empty handler, so Nest dropped that error. That error now reaches the transport as a retriable error.

The same repro against a local build of this branch:

order 1 (first handler completes empty)  ->  reserveStock: side effect ran
order 2 (first handler emits, baseline)  ->  reserveStock: side effect ran

Does this PR introduce a breaking change?

  • Yes
  • No

Handlers that the join cancelled now run to the end. Nothing exported changes.

Other information

The specs cover these cases:

  • An empty branch at the head, in the middle and at the end of a chain.
  • Both call sites, static and request-scoped.
  • An error branch that still errors the join.

Each defaultIfEmpty has a spec that fails without it.

Handlers registered for the same event pattern are chained and joined
with forkJoin. forkJoin completes without emitting as soon as one source
completes empty, and it unsubscribes from the other sources. A handler
that filtered the event out therefore cancelled a sibling handler that
returned a cold observable, so its work never ran and the processing end
hook fired before it. The docs say that Nest triggers all the handlers
of a pattern in parallel.

Default each branch of the join to undefined when it completes empty.
The join still emits once, after every handler finishes, and an error
in any handler still errors the join. An error that a sibling raised
after an empty handler completed was lost before, because the join had
already completed. It now reaches the transport, e.g. as a retriable
Kafka error.

Closes nestjs#17960
@kamilmysliwiec kamilmysliwiec changed the title fix(microservices): keep sibling event handlers running on empty fix(microservices): run every event handler when a sibling completes empty Oct 1, 2026
@kamilmysliwiec
kamilmysliwiec merged commit 125144c into nestjs:master Oct 1, 2026
4 checks passed
kamilmysliwiec added a commit that referenced this pull request Oct 16, 2026
Handlers registered for the same event pattern are joined with forkJoin,
which unsubscribes from the other sources as soon as one of them errors.
A guard, pipe or handler that failed for an event therefore cancelled
its siblings: one that returned an observable or had an interceptor
never ran, or was cut off mid-way, and nothing reported it. #17961 fixed
the same for a handler that completes empty.

Each handler now runs to the end, and the join fails with the first
handler's error once all of them have finished. The error therefore
reaches the transport, e.g. as a retriable Kafka error, only after the
slowest sibling, and in registration order when several handlers fail.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-Y9PB81SO.
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.

An @EventPattern handler that completes empty cancels the other handlers of the same pattern

2 participants