Deliver cancellation to condition-variable waiters exactly, without the periodic check (7.1.0)
CancellationSource::request_cancel() stores the flag and notifies the registered condition variables without the mutex of the waiters. A notification that comes between a waiter's check of the token and its wait is lost. The cancellation-aware send(), recv() and emplace() of BoundedChannel then slept on until something else happened on the channel: a probe that cancels a recv() about to block saw it in 249 of 91 336 rounds.
In 7.0.1 (the concurrency batch, branch fix/7.0.1-concurrency), BoundedChannel bounds the loss: while blocked with a token connected to a source, it wakes up every 10 ms to check the token again. CancellationToken::notify_on_cancel(cv) keeps the limitation and documents it.
Goal for 7.1.0 (a feat, since it adds public API): deliver the cancellation exactly, without a periodic check, through a new overload such as notify_on_cancel(cv, mutex). The cancellation locks the mutex of the waiters before it notifies, so it cannot fall between their check and their wait. BoundedChannel then drops its 10 ms check.
A first version was written for 7.0.1 and dropped after an independent review. What it taught:
-
The cancellation must never wait for a mutex of the user while it holds
observer_mutex. That version did, and it deadlocks: a thread that holds the mutex of one registration destroys the guard of another registration of the same source while another thread cancels, which closes the cyclem1 → observer_mutex → m1(reproduced). Copying the registrations and releasing the lock instead brings back F08, a notification of a condition variable whose guard is gone. Proposed design: state per registration, in a node with an increasing id, abusycount and aremovedflag. The cancellation walks the registrations with a cursor:- under
observer_mutex, it takes the next live node and marks it busy; - it releases the lock, locks and unlocks the mutex of the user, and notifies;
- it takes the lock again and clears the mark.
Unregistering waits only while its own node is busy. Two rules remain to document: do not destroy a guard while holding its own mutex, and do not cancel while holding a registered mutex.
- under
-
Keep
CancellationStatesource compatible. It is a documented public aggregate (seeVERSIONING.md), and a new member breaks structured bindings. Keep the new state in an internal type derived from it, whichCancellationSourcecreates, or leave the change for 8.0.0. -
Keep the precedence. With a token already cancelled on entry, a channel operation must throw
operation_canceledbefore it releases its mutex. That version opened an unlock window in which progress orclose()by another thread could win. -
State the guarantee per registration. Destroying or assigning a guard ends its registration. The condition variable and the mutex may be destroyed only when no registration uses them, which matters with duplicate registrations and moved guards.
-
Write tests without ambiguity:
- controlled interleavings, for instance stalling the cancellation by holding a registered mutex, rather than timing;
- never infer a notification from a wake-up, which may be spurious;
- stress tests that stop at the first failure;
- regressions for the deadlock across registrations, for the precedence with a cancelled token, and for duplicate and moved registrations;
- runs under ThreadSanitizer, and under ASan with GCC and with Clang.