Skip to content

Commit

Permalink
fix(mqtt): Force disconnect if it takes longer than 1500ms
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Dec 19, 2021
1 parent ab7c3e7 commit ced9ee1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions backend/lib/mqtt/MqttController.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,27 @@ class MqttController {
});
}));

await this.asyncClient.end();
await closePromise;
let forceDisconnectTimeout;

await Promise.race([
Promise.all([
this.asyncClient.end(),
closePromise
]),
new Promise((resolve) => {
forceDisconnectTimeout = setTimeout(() => {
if (this.client !== null && typeof this.client.end === "function") {
this.client.end(true);

Logger.warn("Forced MQTT disconnect");
}

resolve();
}, 1500);
})
]);
clearTimeout(forceDisconnectTimeout);


if (this.client && !this.client.disconnected) {
throw new Error("MQTT.js is pretending to be disconnected");
Expand Down

0 comments on commit ced9ee1

Please sign in to comment.