phasync
phasync / shielded

Method

phasync::shielded

Run $fn out of every cancellation's reach: phasync::cancel(), Frame::cancel(), and phasync::shutdown() don't cut its waits short. A cancellation that comes meanwhile applies at the coroutine's first wait after $fn returns. For infrastructure that must finish what it started, such as a server ending a protocol cleanly while the application's coroutines stop; application code rarely needs it.

Description#

public static phasync::shielded(
    Closure $fn
): mixed

A finally block's own wait is not shielded by default: the cancellation is sticky, so it throws again there too. When that is what ends up terminating the coroutine, phasync warns (E_USER_WARNING) that cleanup was cut short -- wrap the wait in shielded() instead.

Parameters#

$fn Closure

Return values#

What $fn returns mixed

Errors / Exceptions#

ExceptionWhen
LogicExceptionOutside a coroutine

Examples#

Example 1
phasync::shielded(function () use ($socket) {
    fwrite($socket, $goodbye);   // still sent during a shutdown
    phasync::writable($socket, 1.0);
});