Skip to content

context: fix race between CancellableContext and cancel#5981

Merged
carl-mastrangelo merged 1 commit intogrpc:masterfrom
carl-mastrangelo:ctxrace
Jul 17, 2019
Merged

context: fix race between CancellableContext and cancel#5981
carl-mastrangelo merged 1 commit intogrpc:masterfrom
carl-mastrangelo:ctxrace

Conversation

@carl-mastrangelo
Copy link
Copy Markdown
Contributor

The pendingDeadline variable is modified from the ctor of CancellableContext, but it isn't final. The cancellation can happen before the variable is assigned. It's generally bad practice to leak the this reference from the ctor to other threads anyways.

This code refactors the deadline calculation and scheduling so that pendingDeadline is modified under the lock, and the this reference is not exposed.

Discovered by TSAN.

The `pendingDeadline` variable is modified from the ctor of CancellableContext, but it isn't final.  The cancellation can happen before the variable is assigned.  It's generally bad practice to leak the this reference from the ctor to other threads anyways.

This code refactors the deadline calculation and scheduling so that `pendingDeadline` is modified under the lock, and the `this` reference is not exposed.

Discovered by TSAN.
@carl-mastrangelo carl-mastrangelo changed the title context: fix race between CancellableContext and Context context: fix race between CancellableContext and cancel Jul 13, 2019
@carl-mastrangelo carl-mastrangelo requested a review from ejona86 July 16, 2019 22:18
@carl-mastrangelo carl-mastrangelo merged commit 8a9afd6 into grpc:master Jul 17, 2019
@carl-mastrangelo carl-mastrangelo deleted the ctxrace branch July 17, 2019 08:12
@carl-mastrangelo carl-mastrangelo restored the ctxrace branch August 17, 2019 01:13
@lock lock bot locked as resolved and limited conversation to collaborators Nov 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants