Repository navigation
fix(microservices): await the mqtt close and reset the client state - #18393
Merged
kamilmysliwiec merged 1 commit intoNov 4, 2026
Conversation
Contributor
Author
|
thanks for merging this @kamilmysliwiec, glad it's in now, cheers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
ServerMqtt#close()fires the disconnect and returns right away, and it never clears the clientNestMicroservice#close()doesawait this.serverInstance.close(), so that await settles with the brokerconnection still open. mqtt closes it on a later tick, so anything that reads the service right after a
close sees a live connection: the socket close event landed 11 to 13 ms after
close()on a localmosquitto broker with mqtt 5.16.0
two more things follow from the same line. The ended client stays on
this.mqttClientforever, soon()attaches new listeners to a dead client instead of queueing them for the next
listen(), andunwrap()keeps handing out a closed client
every other transport server already closes this way:
ServerNats#close()resets the client first andthen awaits
natsClient.close(), and the kafka, redis, rmq and grpc servers all reset their client in afinally.ClientMqtt#close()was fixed the same way in #18352 (it awaitsendAsync()and resets thestate even when the end fails). the mqtt server was the only one left
Issue Number: N/A
What is the new behavior?
the close awaits the disconnect and resets the state the same way its siblings do
close()waits for the nextlisten()endAsync()is awaited, so aclose()the caller awaits really has the connection closed, and a failing end still leaves the state resetmaxConnectionAttemptsruns out now handles the returned promise, so the connection error is what gets reported, not a close oneSteps to reproduce
docker run -p 1883:1883 eclipse-mosquitto:2falsetrue, the real socket close lands ~2 ms after the await, andunwrap()still returns that clientthe spec in this PR shows the same gap from the other side: copied onto master it fails 6 tests and passes 91 with the fix
Does this PR introduce a breaking change?
close()now returns a promise. every caller in the repo already awaits it (NestMicroservice#close()),and awaiting a non promise was already allowed, so no call site changes
Other information
the four added tests cover the awaited settle, the reset client, the listener queueing and a failing end.
with the fix reverted 6 tests fail in this file, with it applied 91 pass, and all 1478 tests under
packages/microservicespass.npm run build(tsc -b -v packages) passes too, and oxlint plusprettier are clean
verified on a cpu box, no gpu is involved anywhere in this lane