Skip to content

Commit 555489d

Browse files
alexdowadnikic
authored andcommitted
Honor script time limit when calling shutdown functions
A time limit can be set on PHP script execution via `set_time_limit` (or .ini file). When the time limit is reached, the OS will notify PHP and `timed_out` and `vm_interrupt` flags are set. While these flags are regularly checked when executing PHP code, once the end of the script is reached, they are not checked while invoking shutdown functions (registered via `register_shutdown_function`). Of course, if the shutdown functions are implemented *in* PHP, then the interrupt flag will be checked while the VM is running PHP bytecode and the timeout will take effect. But if the shutdown functions are built-in (implemented in C), it will not. Since the shutdown functions are invoked through `zend_call_function`, add a check of the `vm_interrupt` flag there. Then, the script time limit will be respected when *entering* each shutdown function. The fact still remains that if a shutdown function is built-in and runs for a long time, script execution will not time out until it finishes and the interpreter tries to invoke the next one. Still, the behavior of scripts with execution time limits will be more consistent after this patch. To make the execution time-out feature work even more precisely, it would be necessary to scrutinize all the built-in functions and add checks of the `vm_interrupt` flag in any which can run for a long time. That might not be worth the effort, though. It should be mentioned that this patch does not solely affect shutdown functions, neither does it solely allow for interruption of running code due to script execution timeout. Anything else which causes `vm_interrupt` to be set, such as the PHP interpreter receiving a signal, will take effect when exiting from an internal function. And not just internal functions which are called because they were registered to run at shutdown; there are other cases where a series of internal functions might run in the midst of a script. In all such cases, it will be possible to interrupt the interpreter now. Closes GH-5543.
1 parent f06f260 commit 555489d

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

Zend/zend_execute_API.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,17 @@ int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) /
830830
/* We must re-initialize function again */
831831
fci_cache->function_handler = NULL;
832832
}
833+
834+
/* This flag is regularly checked while running user functions, but not internal
835+
* So see whether interrupt flag was set while the function was running... */
836+
if (EG(vm_interrupt)) {
837+
EG(vm_interrupt) = 0;
838+
if (EG(timed_out)) {
839+
zend_timeout();
840+
} else if (zend_interrupt_function) {
841+
zend_interrupt_function(EG(current_execute_data));
842+
}
843+
}
833844
}
834845

835846
zend_vm_stack_free_call_frame(call);

ext/pcntl/tests/async_signals_2.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Async signals in zend_call_function
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("pcntl")) print "skip";
6+
if (getenv("SKIP_SLOW_TESTS")) print "skip slow test";
7+
?>
8+
--FILE--
9+
<?php
10+
11+
pcntl_async_signals(1);
12+
pcntl_signal(SIGALRM, function($signo) {
13+
throw new Exception("Alarm!");
14+
});
15+
16+
pcntl_alarm(1);
17+
try {
18+
array_map(
19+
'time_nanosleep',
20+
array_fill(0, 360, 1),
21+
array_fill(0, 360, 0)
22+
);
23+
} catch (Exception $e) {
24+
echo $e->getMessage(), "\n";
25+
}
26+
27+
?>
28+
--EXPECT--
29+
Alarm!

tests/basic/timeout_variation_10.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Timeout within shutdown function, variation
55
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
66
if (PHP_OS_FAMILY !== "Windows") die("skip Windows only test");
77
?>
8-
--XFAIL--
9-
Missing timeout check in call_user_function
108
--FILE--
119
<?php
1210

@@ -19,4 +17,5 @@ register_shutdown_function("sleep", 1);
1917
shutdown happens after here
2018
--EXPECTF--
2119
shutdown happens after here
20+
2221
Fatal error: Maximum execution time of 1 second exceeded in %s on line %d

tests/basic/timeout_variation_9.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Timeout within shutdown function
55
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
66
if (PHP_OS_FAMILY !== "Windows") die("skip Windows only test");
77
?>
8-
--XFAIL--
9-
Missing timeout check in call_user_function
108
--FILE--
119
<?php
1210

0 commit comments

Comments
 (0)