Open markrad opened 4 years ago
@markrad Yeah, I think the documentation should say something about this (IIRC this came up once before). The receive callback is called in mqtt_sync()
while the mutex is locked, so calls to mqtt_sync()
(along with some other functions) is not permitted. The callback is moreso meant to be like an interrupt, in that it should contain pretty minimal logic and be run quick, but that isn't explicitly documented anywhere.
Thanks for opening. I'll update the docs accordingly (unless someone else wants to do it) when I get a chance.
You might consider asserting on a situation like that, to make it clear (also in code) that you are not supposed to do stuff like that.
I'm also facing this problem. In the simple_subscriber example I add a mqtt_publish within the publish_callback function leaving the mqtt_sync call in the client_refresher thread. This causes a segmentation fault. Somehow the first time I tried this it worked, but now it doesn't work anymore. I saw the publish_callback is called by mqtt_sync after locking the mutex so a mqtt_publish within the publish_callback try to lock again the mutex causing the segmentation fault. The only way I thought to get it working is to start a new thread from the publish_callback only with the aim to send a mqtt_publish. May this be a solution?
Possibly this is more of a documentation change than a fix.
I have an instance where I need to publish a message to the server while my code is in the received message callback function. The mqtt_publish works fine but, if one calls mqtt_sync inside that function it will crash.