Skip to content

fix(microservices): call RMQ listen callback once - #17675

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
xia-chao:fix/rmq-listen-callback-once
Sep 14, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
xia-chao:fix/rmq-listen-callback-once

Conversation

@xia-chao

@xia-chao xia-chao commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

  • The commit message follows the Nest conventions
  • Regression test added
  • No documentation change is needed for this internal fix

PR Type

  • Bugfix

What is the current behavior?

Issue: #17673

amqp-connection-manager reruns a channel setup function whenever the broker reconnects. ServerRMQ passes the listen() callback through that setup function, so the callback runs again after reconnecting. In the reproduction, one broker reconnect caused four callback invocations instead of one.

What is the new behavior?

The callback is guarded per listen() call and is invoked only once, while channel setup still runs normally on reconnect to restore queues and consumers. A regression test covers repeated setup execution.

Does this PR introduce a breaking change?

  • No

Closes #17673

Validation

  • RMQ server tests: 38/38 passed
  • microservices TypeScript build passed
  • oxlint passed

Guard the bootstrap callback because channel setup runs again after an
AMQP broker reconnect. Add a regression test for repeated setup calls.

Closes nestjs#17673

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@micalevisk micalevisk left a comment

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.

Thanks for the fix — the diagnosis is right: amqp-connection-manager re-runs the channel setup function on every reconnect, so the listen() callback was being re-invoked. The guard is correct and the regression test reproduces the failure properly.

I checked the closure/retention side of the new callbackCalled variable, since adding state to a long-lived closure is the kind of thing that can quietly leak:

  • callbackCalled lives in the start() scope's closure context, which was already retained before this PR — the same setup arrow captured callback, and the connectFailed handler captured callback + maxConnectionAttempts. So this adds one boolean slot to an already-live context, not a new retention root.
  • The inner wrapper arrow is allocated fresh per setup() run, but setupChannel only calls it and drops it, so it's garbage as soon as setupChannel resolves. No accumulation across reconnects.

So: no leak introduced.

One minor improvement I'd like to request, though. The guard flips the boolean but keeps the user's callback strongly reachable for the entire lifetime of the server, across every reconnect. A listen() callback commonly closes over the whole bootstrap scope (app instance, config, DI refs), so holding it forever after its single invocation is wasteful. Releasing it after the one call subsumes callbackCalled — same guard, one variable instead of two, and it drops the reference:

See the inline suggestion.

To be clear, retaining callback is pre-existing behaviour (the old callback! capture had the same property), so this is a nice-to-have rather than a defect in your change — but since this PR is already touching exactly that closure, it seems like the natural place to do it.

Two unrelated observations while reading, neither blocking and neither introduced here:

  • callbackCalled = true is set before invoking, so a throwing callback won't be retried. That's the right call — just noting it's intentional.
  • The if (this.channel) return; guard inside the once(CONNECT) handler is effectively dead, since once already deregisters. Harmless and pre-existing.

Clear the listen callback after the first setup or startup failure so the
connection closure does not retain the user callback after it is invoked.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@xia-chao

xia-chao commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor Author

The original code means that the user's callback and the objects it captures will be referenced by the connection closure of ServerRMQ until the end of its lifecycle
So, it is indeed possible to easily release references

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.

ServerRMQ runs the listen() callback again on every broker reconnect (setup: is re-run)

3 participants