Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition with cancellation tokens in Read/Flush operations … #61500

Merged
merged 5 commits into from
Nov 15, 2021

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Nov 12, 2021

Backport of #59090 to release/6.0

Customer Impact

@AArnott reported a race condition with PipeReader where the ReadAsync operation could hang in some hard to reproduce cases when used with a cancellation token. The race resulting in hangs in various usages of pipelines in https://github.com/AArnott/Nerdbank.Streams (which is used in VS).

Testing

Manual testing. It's very hard to write a unit test for this scenario. Also verified by @AArnott

Risk

Low. The change was made to only affect a very narrow case (synchronous completion). Asserts were added to make sure existing invariants weren't broken.

@danmoseley danmoseley added the Servicing-approved Approved for servicing release label Nov 12, 2021
@Anipik
Copy link
Contributor

Anipik commented Nov 12, 2021

@davidfowl we will need to add the packaging changes here https://github.com/dotnet/runtime/blob/release/6.0/docs/project/library-servicing.md

can you please add those ? example 96c5ebf

this is part of the aspnetcore as well so @safern while validation please make sure the assembly versions are correct here.

cc @ericstj

davidfowl and others added 4 commits November 15, 2021 10:28
…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
@Anipik Anipik force-pushed the davidfowl/backport-cancellation-fix branch from 9b90063 to e02f0db Compare November 15, 2021 18:43
@Anipik Anipik merged commit 39ecea6 into release/6.0 Nov 15, 2021
@safern safern deleted the davidfowl/backport-cancellation-fix branch November 15, 2021 23:34
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants