Fix race condition with cancellation tokens in Read/Flush operations … (#61500)
authorDavid Fowler <davidfowl@gmail.com>
Mon, 15 Nov 2021 23:34:08 +0000 (15:34 -0800)
committerGitHub <noreply@github.com>
Mon, 15 Nov 2021 23:34:08 +0000 (15:34 -0800)
commit39ecea66f01371e9a8af3169a02a63f916e54217
treec848475504448761da33ba72cb15b94242221284
parent0c78d8087eb33adcc5e8a9aee344654af56be5e3
Fix race condition with cancellation tokens in Read/Flush operations … (#61500)

* Fix race condition with cancellation tokens in Read/Flush operations (#59090)

* Fix race condition with cancellation tokens in Read/Write operations
- There's a tight race where UnsafeRegister can fire inline and then ReadAsync never completes. This is because the cancellation token property has not been assigned yet so the callback noops. This change checks the result of the UnsafeRegister operation to see if ran synchronously and throws if it did. We also move any state transitions to after these checks to make sure the PipeAwaitable state doesn't change before throwing.

* Add a debug assert to make sure there's no state change.

* Add if debug

* More if DEBUG

* add packaging changes

* Run validation checks for just netcoreapp

* use targetIdentifier property to avoid function call and use netframework

* simplify some conditions

Co-authored-by: Anirudh Agnihotry <anirudhagnihotry098@gmail.com>
Co-authored-by: Anirudh Agnihotry <anagniho@microsoft.com>
eng/packaging.targets
src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeAwaitable.cs