phasync
Cancellation and cleanup / CancelledException

Exception

CancelledException

Thrown in a coroutine whose wait was cancelled: with phasync::cancel(), or with its frame (Frame::cancel(), the frame's deadline, a deadlock).

Introduction#

The coroutine meets it at its next wait, or at once if it is waiting, and its finally blocks run as it unwinds. It is sticky: waits after it throw it again, so cleanup that must wait belongs in phasync::finally(). A coroutine that ends with it does not fail its run(). The failure that caused a cancellation, when there is one, is getPrevious().

A wait on a flag that can no longer be raised throws it too ("The flag no longer exists").

Class synopsis#

class CancelledException extends RuntimeException implements \Throwable, \Stringable, RethrowExceptionInterface {

}

Examples#

Example 1
phasync::run(function () {
    $worker = phasync::go(function () {
        try {
            phasync::sleep(10);
        } catch (phasync\CancelledException $e) {
            echo "cancelled: ", $e->getMessage(), "\n";   // shutting down
            throw $e;
        }
    });
    phasync::sleep(0.01);
    phasync::cancel($worker, 'shutting down');
});

See also#

  • phasync::cancelCancels a coroutine with a CancelledException.
  • phasync::finallyRegisters $fn to run when the current coroutine ends, or when the nested run() it is in returns.
  • TimeoutExceptionA wait with a timeout ran out before it was satisfied.