Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential race condition leading to publish callback not being called for QoS 0 #1561

Closed
robinsummerhill opened this issue Dec 6, 2022 · 4 comments
Labels

Comments

@robinsummerhill
Copy link

mqtt 4.3.7

We're experiencing infrequent incidents where it appears that the callback supplied to client.publish is not always called when QoS is set to zero.

First, am I right in assuming the callback should always be called for qos=0 - either with an error or null indicating success? The documentation would seem to indicate this but is not explicit:

callback - function (err), fired when the QoS handling completes, or at the next tick if QoS 0. An error occurs if client is disconnecting.

We think that the issue occurs when an unexpected disconnect from the broker happens at exactly the right moment - just before a call to publish but before the disconnect has been picked up by the library.

Looking at the source code, checks are made for the connection state before attempting to publish at client.js#617:

 if (this._checkDisconnecting(callback)) {
    return this
  }

and client.js#617:

if (!this.connected) {
...
}

If these checks pass then the packet is written to client.stream and the callback is scheduled to be called on the next drain event. (client.js#180):

client.stream.once('drain', cb)

However, if the disconnect happens after the checks but before actually sending the packet there is a potential race condition where the packet is written but drain will never be fired and the callback is not called.

Can you confirm if this is a bug or we are making assumptions about the callback processing that are not valid for qos=0?

@robertsLando
Copy link
Member

Please consider opening a PR to fix this issue

@robertsLando
Copy link
Member

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

Copy link

This is an automated message to let you know that this issue has
gone 365 days without any activity. In order to ensure that we work
on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a
"bump" to keep it open.

Thank you for your contribution.

@github-actions github-actions bot added the stale label Jul 21, 2024
Copy link

github-actions bot commented Aug 4, 2024

This issue was automatically closed due to inactivity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants