Aleph-w icon indicating copy to clipboard operation
Aleph-w copied to clipboard

Deliver cancellation to condition-variable waiters exactly, without the periodic check (7.1.0)

Open lrleon opened this issue 1 month ago • 0 comments

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:

  1. 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 cycle m1 → 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, a busy count and a removed flag. 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.

  2. Keep CancellationState source compatible. It is a documented public aggregate (see VERSIONING.md), and a new member breaks structured bindings. Keep the new state in an internal type derived from it, which CancellationSource creates, or leave the change for 8.0.0.

  3. Keep the precedence. With a token already cancelled on entry, a channel operation must throw operation_canceled before it releases its mutex. That version opened an unlock window in which progress or close() by another thread could win.

  4. 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.

  5. 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.

lrleon avatar Oct 15 '26 22:10 lrleon