From 077ca2611aadbb7f94453dae7c63b6ec4b705dce Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 15 Feb 2023 15:16:05 -0800 Subject: [PATCH] BugFix(MQTT): send an event in ReceiveMessageHandler before completing received message --- e2e/test/iothub/messaging/MessageReceiveE2ETests.cs | 3 +++ iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/e2e/test/iothub/messaging/MessageReceiveE2ETests.cs b/e2e/test/iothub/messaging/MessageReceiveE2ETests.cs index 5b4eab8635..b54677a689 100644 --- a/e2e/test/iothub/messaging/MessageReceiveE2ETests.cs +++ b/e2e/test/iothub/messaging/MessageReceiveE2ETests.cs @@ -938,6 +938,9 @@ await deviceClient.SetReceiveMessageHandlerAsync( async (message, context) => { VerboseTestLogger.WriteLine($"Received message over the first message handler: MessageId={message.MessageId}"); + var messageD2C = new Client.Message(Encoding.UTF8.GetBytes("DeviceToCloud")); + // sending an event within message handler to make sure its flow is not affected by the event. + await deviceClient.SendEventAsync(messageD2C); await deviceClient.CompleteAsync(message).ConfigureAwait(false); firstHandlerSemaphore.Release(); }, diff --git a/iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs b/iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs index cf1328de9a..ff8f8f287f 100644 --- a/iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs +++ b/iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs @@ -578,9 +578,7 @@ private async Task HandleIncomingMessagesAsync() // We are intentionally not awaiting _deviceMessageReceivedListener callback. // This is a user-supplied callback that isn't required to be awaited by us. We can simply invoke it and continue. _ = _deviceMessageReceivedListener?.Invoke(message); - // Messages with QoS = 1 need to be Acknowledged otherwise it results in mismatched Ack to IoT Hub - // causing next message being replayed and all subsequent messages being queued. - await CompleteIncomingMessageAsync(message).ConfigureAwait(false); + await TaskHelpers.CompletedTask.ConfigureAwait(false); if (Logging.IsEnabled) Logging.Exit(this, "Process C2D message via callback", nameof(HandleIncomingMessagesAsync));