From d2bfa6c0d2e9cac3e9aea0b39f1499b8e61d7271 Mon Sep 17 00:00:00 2001 From: Corey Mutter Date: Tue, 22 Oct 2024 17:59:14 +0000 Subject: [PATCH 1/2] fix: Wait for MQTT subscriptions to take effect before returning (#516) MQTTAsync_subscribe() is async. There are some places e.g. startConfigured() where we expect the subscription to be in effect right after this call. So wait for completion before returning. Fixes: #516 Signed-off-by: Corey Mutter --- src/c/bus-mqtt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/c/bus-mqtt.c b/src/c/bus-mqtt.c index 60e76ce6..2ba3461a 100644 --- a/src/c/bus-mqtt.c +++ b/src/c/bus-mqtt.c @@ -73,6 +73,10 @@ static void edgex_bus_mqtt_subscribe (void *ctx, const char *topic) { iot_log_error (cinfo->lc, "mqtt: unable to subscribe to %s, error code %d", topic, rc); } + else + { + MQTTAsync_waitForCompletion(cinfo->client, opts.token, 1000); + } } static void edgex_bus_mqtt_post (void *ctx, const char *topic, const iot_data_t *envelope) From c3442cc74ec8b65f1284546aceb8079855459ced Mon Sep 17 00:00:00 2001 From: FelixTing Date: Tue, 26 Nov 2024 10:03:25 +0800 Subject: [PATCH 2/2] fix: Add a check for the return code of MQTTAsync_waitForCompletion Signed-off-by: FelixTing --- src/c/bus-mqtt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/c/bus-mqtt.c b/src/c/bus-mqtt.c index 2ba3461a..baaffda3 100644 --- a/src/c/bus-mqtt.c +++ b/src/c/bus-mqtt.c @@ -75,7 +75,10 @@ static void edgex_bus_mqtt_subscribe (void *ctx, const char *topic) } else { - MQTTAsync_waitForCompletion(cinfo->client, opts.token, 1000); + rc = MQTTAsync_waitForCompletion(cinfo->client, opts.token, 1000); + if (rc != MQTTASYNC_SUCCESS) + { + iot_log_error (cinfo->lc, "mqtt: subscribe to %s failed, error code %d", topic, rc); } }