Bug #71448 [Com]: declare(ticks=1) limited to scope of file makes Signal handling difficult
| From: | hamid dot a85 at gmail dot com | Date: | Sat, 14 May 2016 16:26:38 +0000 |
| Subject: | Bug #71448 [Com]: declare(ticks=1) limited to scope of file makes Signal handling difficult | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201092@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71448&edit=1
ID: 71448
Comment by: hamid dot a85 at gmail dot com
Reported by: dave at atst dot io
Summary: declare(ticks=1) limited to scope of file makes
Signal handling difficult
Status: Open
Type: Bug
Package: PCNTL related
Operating System: Linux
PHP Version: 7.0Git-2016-01-25 (Git)
Block user comment: N
Private report: N
New Comment:
We were trying to add a timeout feature to laravel artisan command
queue:work. But the
tests failed in PHP 7.0:
Pull request: https://github.com/laravel/framework/pull/13425
A simpler test case that fails:
https://github.com/halaei/ticks-bug-php-7
https://travis-ci.org/halaei/ticks-bug-php-7/builds/130244558
Code:
#main.php
<?php
declare(ticks=1);
pcntl_signal(SIGALRM, function ($sig) {
die(0);
});
require_once __DIR__ . '/loop.php';
#loop.php
<?php
pcntl_alarm(2);
while (true) {
sleep(10);
}
Expected behavior: the code terminates after 2 seconds
Observed behavior: the code will never terminate.
Previous Comments:
------------------------------------------------------------------------
[2016-04-25 01:19:39] nathan dot renniewaldock at gmail dot com
I've noticed this too.
I've got an IRC bot in PHP, I'm handling SIGINT to clean up before exiting.
With declare(ticks=1] in an included file,
in 5.6.20: triggered near instant
in 7.0.4: can take up to 20 seconds. Near instant after moving declare to the main file.
There's also no mention of this change in the migration guide.
------------------------------------------------------------------------
[2016-01-25 21:42:16] dave at atst dot io
Description:
------------
Not sure if this is intended behaviour or fallout from other declare directives intending to be
scoped to a particular file.
The manual states:
The declare construct can also be used in the global scope, affecting all code following it (however
if the file with declare was included then it does not affect the parent file).
I've attached a script that tries to show what I'm trying to do automatically, but you can
remove the forking and just try and ctrl-c from your terminal.
Test script:
---------------
<?php
declare(ticks=1);
$start = time();
pcntl_signal(SIGINT, function() use ($start) {
$diff = time() - $start;
echo "SIGINT received after $diff seconds\n";
exit;
});
$id = posix_getpid();
$child = pcntl_fork();
if ($child) {
include 'inc.php';
} else {
posix_kill($id, SIGINT);
}
## inc.php
<?php
$a = 0;
$start = time();
while(time() < $start + 5) {
$a = $a + 2; # something tickable
}
Expected result:
----------------
SIGINT received after 0 seconds
Actual result:
--------------
SIGINT received after 5 seconds
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71448&edit=1