Skip to content

Commit 69798e4

Browse files
ggeurtskblok
authored andcommitted
Fix race condition between disposal and cancellation of CancellationTokenSource (Issue #942) (#943)
1 parent db2753d commit 69798e4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/PuppeteerSharp/Transport/WebSocketTransport.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,16 @@ public Task SendAsync(string message)
8585
/// <summary>
8686
/// Stops reading incoming data.
8787
/// </summary>
88-
public void StopReading() => _readerCancellationSource.Cancel();
88+
public void StopReading()
89+
{
90+
if (!IsClosed)
91+
{
92+
// No need to cancel reading if no reading operation is in progress. Moreover,
93+
// the _readerCancellationSource may already have been disposed, so cancelling
94+
// it could cause an ObjectDisposedException.
95+
_readerCancellationSource.Cancel();
96+
}
97+
}
8998

9099
/// <summary>
91100
/// Starts listening the socket

0 commit comments

Comments
 (0)