-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Open
Labels
multistreamRelated to Janus 1.xRelated to Janus 1.x
Description
What version of Janus is this happening on?
Master
Have you tested a more recent version of Janus too?
Yes
Additional context
Currently, when the MQTT client disconnects normally (via MQTTAsync_disconnect), the status message is not sent reliably. This happens because:
- The will message (configured with disconnect status) is not published by the broker during normal disconnect (as per MQTT spec)
- The explicit status message publication sometimes fails due to timing issues with the disconnect process
Changes made:
- Moved status message publication to the beginning of the disconnect process
- Added a small delay after message publication to ensure delivery
- Improved error handling to continue disconnect even if status message fails
Code changes:
int janus_mqtt_client_disconnect(janus_mqtt_context *ctx) {
/* First, send disconnect status message */
if (ctx->status.enabled && ctx->status.disconnect_message != NULL) {
int rc = janus_mqtt_client_publish_status_message(ctx, ctx->status.disconnect_message);
if (rc != MQTTASYNC_SUCCESS) {
JANUS_LOG(LOG_ERR, "Failed to publish disconnect status MQTT message, topic: %s, message: %s, return code: %d\n",
ctx->status.topic, ctx->status.disconnect_message, rc);
/* Continue disconnect even if status message fails */
}
}
/* Give time for message delivery */
g_usleep(100000); // 100ms delay
/* Now proceed with disconnect */
MQTTAsync_disconnectOptions options = MQTTAsync_disconnectOptions_initializer;
// ... rest of disconnect code ...
}
Alternative approaches that could be considered:
- Use MQTT 5.0's Will Delay Interval property to delay will message publication
- Implement a two-phase disconnect process with explicit status message
- Use a separate status topic for disconnect notifications
- Implement a heartbeat mechanism to detect unexpected disconnects
Testing:
- Normal disconnect: status message is now sent reliably
- Unexpected disconnect (e.g., SIGKILL): will message is published by broker
- No more errors during disconnect process
References:
- MQTT 3.1.1 spec, section 3.14.4 "DISCONNECT Actions"
- MQTT 5.0 spec, section 3.14.4 "DISCONNECT Actions"
This fix ensures that the disconnect status is always communicated to the broker, either through explicit message or will message, depending on the disconnect type.
That's what Cursor AI suggest, i don't know if it's best fix, but it's work.
Metadata
Metadata
Assignees
Labels
multistreamRelated to Janus 1.xRelated to Janus 1.x