@@ -316,13 +316,22 @@ public async Task<SubscribeResult> SubscribeAsync(SubscribeOptions options)
316316
317317 // Setup the task completion source to wait for the SUBACK
318318 var taskCompletionSource = new TaskCompletionSource < SubAckPacket > ( ) ;
319- void TaskHandler ( object ? sender , OnSubAckReceivedEventArgs args ) => taskCompletionSource . SetResult ( args . SubAckPacket ) ;
319+ void TaskHandler ( object ? sender , OnSubAckReceivedEventArgs args )
320+ {
321+ if ( args . SubAckPacket . PacketIdentifier == subscribePacket . PacketIdentifier )
322+ {
323+ taskCompletionSource . SetResult ( args . SubAckPacket ) ;
324+ }
325+ }
326+
320327 EventHandler < OnSubAckReceivedEventArgs > eventHandler = TaskHandler ;
321328 this . OnSubAckReceived += eventHandler ;
322329
323330 // Queue the constructed packet to be sent on the wire
324331 this . Connection . SendQueue . Enqueue ( subscribePacket ) ;
325332
333+
334+
326335 SubAckPacket subAck ;
327336 SubscribeResult subscribeResult ;
328337 try
@@ -423,7 +432,14 @@ public async Task<UnsubscribeResult> UnsubscribeAsync(UnsubscribeOptions unsubOp
423432 var unsubscribePacket = new UnsubscribePacket ( unsubOptions , ( ushort ) packetIdentifier ) ;
424433
425434 var taskCompletionSource = new TaskCompletionSource < UnsubAckPacket > ( ) ;
426- void TaskHandler ( object ? sender , OnUnsubAckReceivedEventArgs args ) => taskCompletionSource . SetResult ( args . UnsubAckPacket ) ;
435+ void TaskHandler ( object ? sender , OnUnsubAckReceivedEventArgs args )
436+ {
437+ if ( args . UnsubAckPacket . PacketIdentifier == unsubscribePacket . PacketIdentifier )
438+ {
439+ taskCompletionSource . SetResult ( args . UnsubAckPacket ) ;
440+ }
441+ }
442+
427443 EventHandler < OnUnsubAckReceivedEventArgs > eventHandler = TaskHandler ;
428444 this . OnUnsubAckReceived += eventHandler ;
429445
0 commit comments