Skip to content

Commit 08b2559

Browse files
committed
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 a 'timed_out' flag is set. While this flag is regularly checked when executing PHP code, once the end of the script is reached, it is not checked while invoking shutdown functions (registered via register_shutdown_function). Of course, if the shutdown functions are implemented *in* PHP, then the script time limit will be checked while they are running and 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 'timed_out' 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 'timed_out' flag in any which can run for a long time. That might not be worth the effort, though.
1 parent b111674 commit 08b2559

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Zend/zend_execute_API.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,12 @@ 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 we timed out while the function was running... */
836+
if (EG(timed_out)) {
837+
zend_timeout(0);
838+
}
833839
}
834840

835841
zend_vm_stack_free_call_frame(call);

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)