Repository navigation
fix(microservices): call RMQ listen callback once - #17675
Conversation
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
left a comment
There was a problem hiding this comment.
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:
callbackCalledlives in thestart()scope's closure context, which was already retained before this PR — the samesetuparrow capturedcallback, and theconnectFailedhandler capturedcallback+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, butsetupChannelonly calls it and drops it, so it's garbage as soon assetupChannelresolves. 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 = trueis 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 theonce(CONNECT)handler is effectively dead, sinceoncealready 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>
|
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 |
PR Checklist
PR Type
What is the current behavior?
Issue: #17673
amqp-connection-managerreruns a channelsetupfunction whenever the broker reconnects.ServerRMQpasses thelisten()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?
Closes #17673
Validation