Change PipeStream's sync I/O cancellation support to avoid blocking CancellationToken...
authorStephen Toub <stoub@microsoft.com>
Fri, 22 Jul 2022 21:41:55 +0000 (17:41 -0400)
committerGitHub <noreply@github.com>
Fri, 22 Jul 2022 21:41:55 +0000 (17:41 -0400)
commita68ab4a50f9167795d469d5cefacb7de4b5c2da7
treeed25c2e6a214132db2f1cbf2c9e3cfe0ae63b131
parentfce65490da1d15c0f6db88c8e782dc247de47c8b
Change PipeStream's sync I/O cancellation support to avoid blocking CancellationTokenSource.Cancel (#72612)

* Change PipeStream's sync I/O cancellation support to avoid blocking CancellationTokenSource.Cancel

In a degenerate case, where cancellation is requested between the cancellation being registered and the I/O being performed, and where the thread performing the I/O is delayed significantly, the thread calling CTS.Cancel to cancel the operation could be blocked waiting for the callback cancellation loop which is in turn waiting for the I/O to be performed.

This changes the implementation to not block Cancel by instead queueing the cancellation handling.

It also factors the whole implementation of the async-over-sync support into a helper class, setting us up to reuse it elsewhere as needed (and cleaning up the PipeStream code to avoid duplication).

* Address PR feedback

* Address PR feedback (and a few other tweaks)
src/libraries/Common/src/System/Threading/AsyncOverSyncWithIoCancellation.cs [new file with mode: 0644]
src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj
src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs