-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Disconnect delay with mosquitto_loop_forever() and WITH_SRV #2947
Comments
I produced a self-contained reproducer without threads#include <mosquitto.h>
#include <stdio.h>
#include <time.h>
struct data {
time_t start_time;
};
static void print_time(struct data* data) {
time_t now = time(NULL);
printf("After %d sec: ", now - data->start_time);
}
static void my_connect_callback(struct mosquitto *mosq, void* data, int) {
print_time(data);
puts("connected");
mosquitto_disconnect(mosq);
}
static void my_disconnect_callback(struct mosquitto *, void* data, int) {
print_time(data);
puts("disconnected");
}
static void my_log_callback(struct mosquitto *, void* data, int, const char* msg) {
print_time(data);
printf("Log: %s\n", msg);
}
int main() {
struct data data;
struct mosquitto *mosq = NULL;
data.start_time = time(NULL);
mosquitto_lib_init();
mosq = mosquitto_new(NULL, 1, &data);
if (!mosq) {
puts("mosquitto_new() failed");
return 1;
}
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
mosquitto_log_callback_set(mosq, my_log_callback);
if (MOSQ_ERR_SUCCESS != mosquitto_connect(mosq, "localhost", 1883, 120)) {
puts("mosquitto_connect() failed");
return 0;
}
mosquitto_loop_forever(mosq, 12 * 1000, 1);
print_time(&data);
puts("exiting");
return 0;
} I ran this against current master. Output when build with
Output when build with
With the proposed patch from my original post, even |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
we are using libmosquitto in an externally threaded way (
mosquitto_threaded_set(conn, 1)
). We have one thread runningmosquitto_loop_forever()
and want to make this exit by callingmosquitto_disconnect()
in another thread. This usually works fine (on Ubuntu using Mosquitto 2.0.11), but we recently had this same code hang for 120 seconds on a Gentoo system (both with 2.0.18 and 2.0.15; 120 is our keepalive value).A debugging session found out that normally the fourth(*) call to
mosquitto_loop()
immediately returnsMOSQ_ERR_NO_CONN
, but on the Gentoo system this only happens after 120 seconds. Some more debugging indicated that this is related to theWITH_SRV
compile option, which is not enabled on Ubuntu, but conditionally enabled on Gentoo. Disabling the srv use flag on Gentoo made the hang go away.Some code inspection came up with a patch to the following code:
mosquitto/lib/loop.c
Lines 83 to 96 in 15292b2
This code is in an
#ifdef WITH_SRV
. Without SRV, this just returnsMOSQ_ERR_NO_CONN
. With SRV, this only returnsMOSQ_ERR_NO_CONN
ifmosq->achan != NULL && mosquitto_get_state(mosq) != mosq_cs_connect_srv
. In our case,mosq->achan == NULL
and thus this code falls through to the following code. With the proposed patch, this instead also returnsMOSQ_ERR_NO_CONN
ifmosq->achan == NULL
, changing the code to always return NO_CONN except ifachan != NULL
and the the state ismosq_cs_connect_srv
.Testing done: None. We are going with the work-around of disabling SRV support. Sorry. I hope you have a better understanding of the SRV feature and can actually test this patch properly.
(*): The program that we debugged with just publishes one message and then disconnects again. Thus, it is quite deterministic and "the fourth call" seems to be meaningful / comparable to the working system.
The text was updated successfully, but these errors were encountered: