mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

connect_async and loop_start order matters when no network connection.

Open andrewwade opened this issue 3 years ago • 0 comments

If I start my client like this without a network connection then once the connection is available the client will connect and be happy. The problem is if I call mosquitto_disconnect then reconnect later calling init() again, I don't get a connect callback.

void init() {
    mosquitto_loop_start(mosq);
    mosquitto_connect_async(mosq);
}

If I start my client like this without a network connection then it doesn't automatically connect when the network becomes available:

void init() {
    mosquitto_connect_async(mosq);
    mosquitto_loop_start(mosq);
}

So to auto-connect correctly and still get a callback when called a second time, I have to do this:

void init() {
    mosquitto_loop_start(mosq);
    mosquitto_connect_async(mosq);
    mosquitto_loop_start(mosq);
}

This works in both scenarios for some reason even though the second mosquitto_loop_start returns MOSQ_ERR_INVAL. This doesn't makes sense to me because it looks like the second mosquitto_loop_start shouldn't do anything except return the error. https://github.com/eclipse/mosquitto/blob/v2.0.14/lib/thread_mosq.c#L42

Mosquitto Version: 2.0.14 OS: Linux Ubuntu 20.04

andrewwade avatar Jun 22 '22 17:06 andrewwade