-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
<condition_variable>: Parallel invocations of stop token condition_variable_any::wait_until
can result in deadlocks
#2218
Comments
Doesn't STL/stl/inc/condition_variable Line 226 in 6213792
? |
Execution only reaches that point if |
Oh, I see. Then we should probably put STL/stl/inc/condition_variable Line 217 in 6213792
|
See #2220 |
Thanks for the report, and thanks for the fix! |
Multiple calls of
stop_token
std::condition_variable_any::wait_until
invoked in parallel (e.g. via threads) can result in a deadlock arising. Specifically, givenInvocation of
do_work
on two separate threads can result in a deadlock:wait_until
:wait_until
sees the predicate is unmet, and takes the guard for thecondition_variable_any
.wait_until
sees the stop token's source hasn't requested a stop, and unlockslock
, granting access tomutex
.mutex
.wait_until
, and blocks trying to take the lock on the guard.wait_until
, sees that the wait had expired, andbreak
s out of an internal loop; this causes three variables to go out of scope, which are deconstructed in the reverse order of their construction:_Now
, which is trivial._Unlock_outer
, and to do this we need to takemutex
, which is currently locked by thread 2._Guard
, which would unlock the guard thread 2 is currently waiting for.This results in thread 1 wanting to take a lock held by thread 2, and thread 2 wanting to take a lock held by thread 1. There are, of course, different sequences of events which can also lead to this (thread 1 could have been waiting for a while already, or thread 2 could have taken the lock immediately but then yielded for thread 1).
I'm not versed well enough in the actual implementation of the STL to attempt a fix myself, but I've currently worked around the issue by using the non-stop-token version of the method.
The text was updated successfully, but these errors were encountered: