-
Notifications
You must be signed in to change notification settings - Fork 51
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
Pending Subscription message is overwritten by Publish => subscribe callback is missed #18
Comments
Hi I also face similar problem. I genereally raise a flag to show that scubscription is done if not I restart the process after some tries. subscribtion call back is not called. as you post above said not to immediately publish while subscribing. so I pushed my publish call to subscribe_cb. still I don't get subscription callback this use to work before. this is my codes out put. (sorry code is complex now )
my subscribe_cb and
|
In
connected_cb()
there is a subscribe immediately followed by a publish:esp32-mqtt/main/app_main.c
Lines 28 to 33 in da0e976
I was monitoring the behaviour with Wireshark and noticed that although the SUBACK is returned by my mosquitto server, the client is not processing it and therefore not calling
subscribe_cb()
. I uncommented this line:https://github.com/tuanpmt/espmqtt/blob/2967332b95454d4b53068a0d5484ae60e312eb12/mqtt.c#L425
Well, actually, the commented code was incorrect so I wrote this instead:
mqtt_info("msg_type %d, msg_id %d, pending_type %d, pending_id %d", msg_type, msg_id, client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
When only the subscribe happens (the publish is removed), there is a SUBACK (9) received, with ID 1, and the pending message is a SUBSCRIBE (8), so the MQTT_MSG_TYPE_SUBACK case is executed and the
subscribe_cb()
is called.This successfully matches up the SUBACK (9) with the previous SUBSCRIBE (8) and fires the subscribe callback.
However if the publish happens immediately after the subscribe, it seems to overwrite the pending message, resulting in:
The pending message's type is no longer SUBSCRIBE (8) but PUBLISH (3), so when the SUBACK (9) is received there is no pending SUBSCRIBE (8) to compare against and the
subscribe_cb()
callback is not executed.Oddly I don't see any debug from
mqtt_start_receive_schedule()
indicating it received a SUBACK (8) in this case at all - where does it go? Wireshark confirms that it is delivered.I need to check what the MQTT spec says about ignoring ACKs, however missing the
subscribe_cb()
as a result is surely a bug. I think the solution could be to maintain a list of pending messages, and then look for a corresponding match when processing ACKs to ensure the callbacks are all fired.Alternatively, the API could be changed to support an asynchronous model where the API consumer is responsible for handling incoming future acknowledgements, which also allows the API consumer to handle timeouts and retries. However it would be nice if this library took care of that because the current API is nice and simple.
The text was updated successfully, but these errors were encountered: