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>