@@ -217,7 +217,9 @@ private async Task ConnectionPublishWriterAsync(CancellationToken cancellationTo
217217 {
218218 Logger . Trace ( $ "{ this . Client . Options . ClientId } -(PW)- ConnectionPublishWriter: Failed to write to transport.") ;
219219
220- if ( this . State == ConnectState . Connected )
220+ // Capture state once to avoid race conditions
221+ var currentState = this . State ;
222+ if ( currentState == ConnectState . Connected )
221223 {
222224 // This is an unexpected exit and may be due to a network failure.
223225 Logger . Debug ( $ "{ this . Client . Options . ClientId } -(PW)- ConnectionPublishWriter: unexpected exit. Disconnecting...") ;
@@ -244,7 +246,9 @@ private async Task ConnectionPublishWriterAsync(CancellationToken cancellationTo
244246 Logger . Error ( $ "{ this . Client . Options . ClientId } -(PW)- Exception: { ex } ") ;
245247
246248 // Handle exception gracefully - trigger disconnection and exit
247- if ( this . State == ConnectState . Connected )
249+ // Capture state once to avoid race conditions
250+ var currentState = this . State ;
251+ if ( currentState == ConnectState . Connected )
248252 {
249253 try
250254 {
@@ -344,7 +348,10 @@ private async Task ConnectionWriterAsync(CancellationToken cancellationToken)
344348 if ( ! writeSuccess )
345349 {
346350 Logger . Error ( $ "{ this . Client . Options . ClientId } -(W)- Write failed. Disconnecting...") ;
347- if ( this . State == ConnectState . Connected )
351+
352+ // Capture state once to avoid race conditions
353+ var currentState = this . State ;
354+ if ( currentState == ConnectState . Connected )
348355 {
349356 await this . HandleDisconnectionAsync ( false ) . ConfigureAwait ( false ) ;
350357 }
@@ -371,7 +378,9 @@ private async Task ConnectionWriterAsync(CancellationToken cancellationToken)
371378 Logger . Error ( $ "{ this . Client . Options . ClientId } -(W)- Exception: { ex } ") ;
372379
373380 // Handle exception gracefully - trigger disconnection and exit
374- if ( this . State == ConnectState . Connected )
381+ // Capture state once to avoid race conditions
382+ var currentState = this . State ;
383+ if ( currentState == ConnectState . Connected )
375384 {
376385 try
377386 {
@@ -408,7 +417,10 @@ private async Task<bool> ConnectionReaderAsync(CancellationToken cancellationTok
408417 if ( readResult . Failed )
409418 {
410419 Logger . Debug ( $ "{ this . Client . Options . ClientId } -(R)- ConnectionReader exiting: Read from transport failed.") ;
411- if ( this . State == ConnectState . Connected )
420+
421+ // Capture state once to avoid race conditions
422+ var currentState = this . State ;
423+ if ( currentState == ConnectState . Connected )
412424 {
413425 await this . HandleDisconnectionAsync ( false ) . ConfigureAwait ( false ) ;
414426 }
@@ -525,7 +537,9 @@ private async Task<bool> ConnectionReaderAsync(CancellationToken cancellationTok
525537 Logger . Error ( $ "{ this . Client . Options . ClientId } -(R)- Exception: { ex } ") ;
526538
527539 // Handle exception gracefully - trigger disconnection and exit
528- if ( this . State == ConnectState . Connected )
540+ // Capture state once to avoid race conditions
541+ var currentState = this . State ;
542+ if ( currentState == ConnectState . Connected )
529543 {
530544 try
531545 {
@@ -623,7 +637,9 @@ private async Task ReceivedPacketsHandlerAsync(CancellationToken cancellationTok
623637 Logger . Error ( $ "{ this . Client . Options . ClientId } -(RPH)- Exception: { ex } ") ;
624638
625639 // Handle exception gracefully - trigger disconnection and exit
626- if ( this . State == ConnectState . Connected )
640+ // Capture state once to avoid race conditions
641+ var currentState = this . State ;
642+ if ( currentState == ConnectState . Connected )
627643 {
628644 try
629645 {
0 commit comments