Beware that pcntl_signal will interrupt (u)sleep calls which will not be resumed once the handler is completed.
It's a documented behaviour (https://www.php.net/manual/en/function.sleep.php) although it may look like a bug when encountered for the first time.
From the docs:
"If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of the WAIT_IO_COMPLETION constant within the Windows API). On other platforms, the return value will be the number of seconds left to sleep."
```
<?php
$interval = 1;
pcntl_async_signals(true);
pcntl_signal(SIGALRM, function () use ($interval): void {
echo 'SIGALRM called' . PHP_EOL;
pcntl_alarm($interval);
});
pcntl_alarm($interval);
echo 'Sleep (will be interrupted) started' . PHP_EOL;
sleep(100000000000);
echo 'Sleep ended soon due to interrupt' . PHP_EOL;
$sleepTimeout = 10;
echo "Proper sleep for {$sleepTimeout} seconds" . PHP_EOL;
$startedAt = time();
while ($sleepTimeout > 0 && ($sleepTimeout = sleep($sleepTimeout)) !== true) {
echo "Sleep interrupted, {$sleepTimeout} seconds remaining" . PHP_EOL;
}
$elapsed = time() - $startedAt;
echo "Sleep finished after {$elapsed} seconds" . PHP_EOL;
```