Skip to content

fix(microservices): await the mqtt close and reset the client state - #18393

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
hktitof:fix/microservices-await-the-mqtt-close-and-reset-the-client
Nov 4, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
hktitof:fix/microservices-await-the-mqtt-close-and-reset-the-client

Conversation

@hktitof

@hktitof hktitof commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

ServerMqtt#close() fires the disconnect and returns right away, and it never clears the client

public close() {
  this.mqttClient && this.mqttClient.end();
  this.pendingEventListeners = [];
}

NestMicroservice#close() does await this.serverInstance.close(), so that await settles with the broker
connection 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 local
mosquitto broker with mqtt 5.16.0

close() returns: [object Undefined] | is promise: false
RIGHT AFTER close(): driver.connected = true
RIGHT AFTER close(): server keeps the client = true
350ms later: driver.connected = false | close event after ms = 13

two more things follow from the same line. The ended client stays on this.mqttClient forever, so on()
attaches new listeners to a dead client instead of queueing them for the next listen(), and unwrap()
keeps handing out a closed client

every other transport server already closes this way: ServerNats#close() resets the client first and
then awaits natsClient.close(), and the kafka, redis, rmq and grpc servers all reset their client in a
finally. ClientMqtt#close() was fixed the same way in #18352 (it awaits endAsync() and resets the
state 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

public async close() {
  const mqttClient = this.mqttClient;
  this.mqttClient = null;
  this.pendingEventListeners = [];
  await mqttClient?.endAsync();
}
  • the client is reset before the await, so a status update from the closing client is not taken for the connection that gave up, and a listener registered right after close() waits for the next listen()
  • endAsync() is awaited, so a close() the caller awaits really has the connection closed, and a failing end still leaves the state reset
  • the error listener that closes the server when maxConnectionAttempts runs out now handles the returned promise, so the connection error is what gets reported, not a close one

Steps to reproduce

  1. start a broker, docker run -p 1883:1883 eclipse-mosquitto:2
  2. boot a microservice against it, close it, and read the driver right after the await
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.MQTT,
  options: { url: 'mqtt://127.0.0.1:1883' },
});
await app.listen();
const driver = (app as any).getTransportServer().unwrap();
await app.close();
console.log(driver.connected);
  1. expected: the awaited close leaves the connection closed, so this logs false
  2. actual on master: it logs true, the real socket close lands ~2 ms after the await, and unwrap() still returns that client

the 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?

  • Yes
  • No

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/microservices pass. npm run build (tsc -b -v packages) passes too, and oxlint plus
prettier are clean

verified on a cpu box, no gpu is involved anywhere in this lane

@kamilmysliwiec
kamilmysliwiec merged commit 96ebd34 into nestjs:master Nov 4, 2026
4 checks passed
@hktitof

hktitof commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

thanks for merging this @kamilmysliwiec, glad it's in now, cheers

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.

2 participants