note 126692 added to function.pcntl-alarm
| From: | molsavsky1 at gmail dot com | Date: | Fri, 17 Dec 2021 09:55:27 +0000 |
| Subject: | note 126692 added to function.pcntl-alarm | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217383@lists.php.net to get a copy of this message | ||
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;
```
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 2a02:830a:b206:0:941d:5f10:af20:5f9d)
----
Manual Page -- https://php.net/manual/en/function.pcntl-alarm.php
Edit -- https://main.php.net/note/edit/126692
Del: integrated -- https://main.php.net/note/delete/126692/integrated
Del: useless -- https://main.php.net/note/delete/126692/useless
Del: bad code -- https://main.php.net/note/delete/126692/bad+code
Del: spam -- https://main.php.net/note/delete/126692/spam
Del: non-english -- https://main.php.net/note/delete/126692/non-english
Del: in docs -- https://main.php.net/note/delete/126692/in+docs
Del: other reasons-- https://main.php.net/note/delete/126692
Reject -- https://main.php.net/note/reject/126692
Search -- https://main.php.net/manage/user-notes.php