diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 74d75ec8..efe0b780 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -110,6 +110,7 @@ enum mosquitto_client_state { mosq_cs_connecting = 16, mosq_cs_duplicate = 17, /* client that has been taken over by another with the same id */ mosq_cs_disconnect_with_will = 18, + mosq_cs_disused = 19, /* client that has been added to the disused list to be freed */ }; enum mosquitto__protocol { diff --git a/src/context.c b/src/context.c index 2511a872..69053b6c 100644 --- a/src/context.c +++ b/src/context.c @@ -251,19 +251,19 @@ void context__send_will(struct mosquitto_db *db, struct mosquitto *ctxt) void context__disconnect(struct mosquitto_db *db, struct mosquitto *context) { + context->disconnect_t = time(NULL); + net__socket_close(db, context); + if(context->session_expiry_interval == 0){ context__send_will(db, context); - context->disconnect_t = time(NULL); - net__socket_close(db, context); - #ifdef WITH_BRIDGE if(!context->bridge) #endif { if(context->will_delay_interval == 0){ - /* This will be done later, after the will is published */ + /* This will be done later, after the will is published for delay>0. */ context__add_to_disused(db, context); if(context->id){ context__remove_from_by_id(db, context); @@ -280,6 +280,10 @@ void context__disconnect(struct mosquitto_db *db, struct mosquitto *context) void context__add_to_disused(struct mosquitto_db *db, struct mosquitto *context) { + if(context->state == mosq_cs_disused) return; + + context->state = mosq_cs_disused; + if(db->ll_for_free){ context->for_free_next = db->ll_for_free; db->ll_for_free = context;