Skip to content

Commit

Permalink
BugFix(MQTT): send an event in ReceiveMessageHandler before completin…
Browse files Browse the repository at this point in the history
…g received message (Azure#3116)
  • Loading branch information
tmahmood-microsoft authored Feb 16, 2023
1 parent 9ba754e commit 6cc8b58
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 3 additions & 0 deletions e2e/test/iothub/messaging/MessageReceiveE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down
4 changes: 1 addition & 3 deletions iothub/device/src/Transport/Mqtt/MqttTransportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 6cc8b58

Please sign in to comment.