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

Topic Aliases - Failures after connection loss #276

Open
MattBrittan opened this issue Nov 14, 2024 · 2 comments
Open

Topic Aliases - Failures after connection loss #276

MattBrittan opened this issue Nov 14, 2024 · 2 comments

Comments

@MattBrittan
Copy link
Contributor

Describe the bug

As per the MQTT spec:

Alias mappings exist only within a Network Connection and last only for the lifetime of that Network Connection

Currently this library stores QOS1+ packets that contain the alias. This is a problem because the conneciton may be dropped/re-established before the messages are sent (meaning the topic alias is either invalid, or worse, wrong).

Comments in this issue indicate that this may also be an issue in the autopaho queue, but I cannot see a problem there (of course if the user is setting the alias when creating the message that will cause issues).

I think the messages stored need to be the full messages, this means moving PublishHook so it's called immediatly before the message is transmitted (and noting that it may be called more than once if a message needs to be retransmitted).

To reproduce

Send loads of QOS2 messages with TAHandler enabled, drop and reestablish connection whilst transmission is in progress..

Expected behaviour

TAHandler should work accross reconnects.

Software used:

v0.22

@shirou
Copy link
Contributor

shirou commented Nov 15, 2024

My previous comment includes two issues:

  1. When reconnecting, aliases remain in the TAHandler, causing the Topic Alias to be used from the start.
  2. When using a Queue, the Topic within the Queue disappears.

For issue 1, I submit a separate PR(#277) with sample code.

For issue 2, as you mentioned, since OnPublish is called right before publishing, it doesn’t cause an issue. I wrote sample code to verify this, and the problem did not occur. It seems that another part of my code was likely causing the issue. I will retract the issue.

@MattBrittan
Copy link
Contributor Author

Thanks for the PR @shirou.
I was thinking about this last night and wondered if a solution might be to set both (undoing your previous fix) topic and alias before the message is added to the store.

Then add another callback just before the message is sent. This would check if they are both set and:

If both set then lookup the alias ID in the map:

  • Not in map - add to map and send both (don't chnage the packet)
  • In map and matches topic - do not send the text topic
  • In map and does not match topic - send both and reset the map entry (this assumes the alias is being changed)

This way the aliases could work accross reconnections (either clear the map, or add a flag indicating that the broker needs to be send both parts the next time a message is sent).

This would require a callback immediatly before a publish packet is sent (or resent) but should be fairly simple to implement. The user could use something like the existing topicalias, or assign their own when publishing the message (would be simple when only a few topics are used).

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

2 participants