Fix connection problems when using mosquitto_connect_async().

The connection wouldn't always complete if mosquitto_loop_start() was
called before mosquitto_connect_async(). Closes #848.

Thanks to Ian Gough.

Bug: https://github.com/eclipse/mosquitto/issues/848

Signed-off-by: Roger A. Light <roger@atchoo.org>
pull/891/head
Roger A. Light 7 years ago
parent abaceb9c9e
commit 7f0b4d6271

@ -21,6 +21,8 @@ Library:
- Fix some places where return codes were incorrect, including to the - Fix some places where return codes were incorrect, including to the
on_disconnect() callback. This has resulted in two new error codes, on_disconnect() callback. This has resulted in two new error codes,
MOSQ_ERR_KEEPALIVE and MOSQ_ERR_LOOKUP. MOSQ_ERR_KEEPALIVE and MOSQ_ERR_LOOKUP.
- Fix connection problems when mosquitto_loop_start() was called before
mosquitto_connect_async(). Closes #848.
1.5 - 20180502 1.5 - 20180502

@ -178,6 +178,9 @@ static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking)
}else }else
#endif #endif
{ {
pthread_mutex_lock(&mosq->state_mutex);
mosq->state = mosq_cs_connecting;
pthread_mutex_unlock(&mosq->state_mutex);
rc = net__socket_connect(mosq, mosq->host, mosq->port, mosq->bind_address, blocking); rc = net__socket_connect(mosq, mosq->host, mosq->port, mosq->bind_address, blocking);
} }
if(rc>0){ if(rc>0){

@ -107,6 +107,7 @@ enum mosquitto_client_state {
mosq_cs_socks5_userpass_reply = 13, mosq_cs_socks5_userpass_reply = 13,
mosq_cs_socks5_send_userpass = 14, mosq_cs_socks5_send_userpass = 14,
mosq_cs_expiring = 15, mosq_cs_expiring = 15,
mosq_cs_connecting = 16,
}; };
enum mosquitto__protocol { enum mosquitto__protocol {

@ -79,15 +79,27 @@ int mosquitto_loop_stop(struct mosquitto *mosq, bool force)
void *mosquitto__thread_main(void *obj) void *mosquitto__thread_main(void *obj)
{ {
struct mosquitto *mosq = obj; struct mosquitto *mosq = obj;
int state;
if(!mosq) return NULL; if(!mosq) return NULL;
pthread_mutex_lock(&mosq->state_mutex); do{
if(mosq->state == mosq_cs_connect_async){ pthread_mutex_lock(&mosq->state_mutex);
state = mosq->state;
pthread_mutex_unlock(&mosq->state_mutex); pthread_mutex_unlock(&mosq->state_mutex);
if(state == mosq_cs_new){
#ifdef WIN32
Sleep(10);
#else
usleep(10000);
#endif
}else{
break;
}
}while(1);
if(state == mosq_cs_connect_async){
mosquitto_reconnect(mosq); mosquitto_reconnect(mosq);
}else{
pthread_mutex_unlock(&mosq->state_mutex);
} }
if(!mosq->keepalive){ if(!mosq->keepalive){

Loading…
Cancel
Save