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 panic when OrderMatters = false and connection lost whilst handler in progress #505

Closed
MattBrittan opened this issue May 10, 2021 · 3 comments

Comments

@MattBrittan
Copy link
Contributor

MattBrittan commented May 10, 2021

From PR #504 - logging here so this is more visible (note that the PR will not be accepted as it stands because it sends the ACK before the handler terminates).

When OrderMatters = false there is a potential panic due to this line (panic will occur here or here); in summary the sequence of events is:

  1. Publish received and go routine started to call handler .
  2. Connection drops (or Disconnect called), internalConnLost calls c.stopCommsWorkers() which (via a chain) results in the closure of ackChan
  3. Message handler returns and ackFunc(ackChan, client.persist, message is called which attempts to send to the closed ackChan resulting in a panic.

I made major changes for @1.3.0 to try and remove a range of potential deadlocks and will have introduced this issue at that point (@1.2 did not close incomingPubChan which was not really ideal either as it will have left hanging goRoutines). From the comment

// WARNING the function returned must not be called if the comms routine is shutting down or not running
// (it needs outgoing comms in order to send the acknowledgement). Currently this is only called from
// matchAndDispatch which will be shutdown before the comms are"

I can see that I spotted the risk but missed the fact that matchAndDispatch is doing exactly what I was warning about! At the time usage of OrderMatters = false was fairly low so most tests do not set this option; I have encouraged the use of this option since due to the number of issues logged that turn out to be caused by long running message handlers blocking keep alives.

I think the only viable solution is going to be to drop ACK calls if the connection has been lost but note that it is important that matchAndDispatch shuts down cleanly/quickly even if handlers are still running.

@MattBrittan MattBrittan changed the title Potential panic when OrderMatters = false and connection lost whils handler in progress Potential panic when OrderMatters = false and connection lost whilst handler in progress May 10, 2021
MattBrittan added a commit to ChIoT-Tech/paho.mqtt.golang that referenced this issue May 18, 2021
@MattBrittan
Copy link
Contributor Author

MattBrittan commented May 18, 2021

I have created a PR that tests for, and should resolve, this issue; would appreciate any comments on this (not all that happy with the way this works but think it will reliably solve the issue without breaking anyone's code). Will leave this as a PR for a week in case someone has time to review.

MattBrittan added a commit that referenced this issue May 30, 2021
Resolve send on closed channel when order=false and connection closed. Ref issue #505
@MattBrittan
Copy link
Contributor Author

MattBrittan commented May 30, 2021

As there was no feedback I have gone ahead and merged the PR; would appreciate confirmation that @master resolves this issue and does not introduce any further problems.

@MattBrittan
Copy link
Contributor Author

Closing as v1.3.5 is now out with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant