Skip to content

Commit 5455d2c

Browse files
Dispatch async signals during PHP time_sleep_until()
1 parent 17b6575 commit 5455d2c

7 files changed

Lines changed: 136 additions & 6 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ PHP NEWS
123123
sockets. (David Carlier)
124124

125125
- Standard:
126+
. Dispatch asynchronous signal handlers while time_sleep_until() waits.
127+
(Nicolas Grekas)
126128
. Fixed bug #60110 (fclose(), file_put_contents(), copy() do not return false
127129
properly). (Jakub Zelenka, Ilija Tovilo)
128130
. Fixed three Windows-only proc_open() defects: an uninitialized

‎UPGRADING‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ PHP 8.7 UPGRADE NOTES
3737
application terminate one attribute and append further ones.
3838

3939
- Standard:
40+
. With pcntl_async_signals(true), time_sleep_until() now runs signal
41+
handlers before the sleep ends. Previously, handlers waited until the
42+
deadline. If a handler returns normally, the sleep resumes until the
43+
original deadline. A handler can now interrupt the sleep by throwing an
44+
exception or calling exit.
4045
. The number of filters that can be chained in a php://filter URL is limited
4146
to 16 by default. Set the stream context option max_filter_count to change
4247
this.

‎ext/standard/basic_functions.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ext/standard/info.h"
3232
#include "ext/session/php_session.h"
3333
#include "zend_exceptions.h"
34+
#include "zend_hrtime.h"
3435
#include "zend_ini.h"
3536
#include "zend_operators.h"
3637
#include "ext/standard/php_dns.h"
@@ -1206,8 +1207,9 @@ PHP_FUNCTION(time_sleep_until)
12061207
{
12071208
double target_secs;
12081209
struct timeval tm;
1209-
struct timespec php_req, php_rem;
1210+
struct timespec php_req;
12101211
uint64_t current_ns, target_ns, diff_ns;
1212+
zend_hrtime_t start;
12111213
const uint64_t ns_per_sec = 1000000000;
12121214
const double top_target_sec = (double)(UINT64_MAX / ns_per_sec);
12131215

@@ -1218,6 +1220,7 @@ PHP_FUNCTION(time_sleep_until)
12181220
if (gettimeofday((struct timeval *) &tm, NULL) != 0) {
12191221
RETURN_FALSE;
12201222
}
1223+
start = zend_hrtime();
12211224

12221225
if (UNEXPECTED(!(target_secs >= 0 && target_secs <= top_target_sec))) {
12231226
zend_argument_value_error(1, "must be between 0 and %" PRIu64, (uint64_t)top_target_sec);
@@ -1235,13 +1238,23 @@ PHP_FUNCTION(time_sleep_until)
12351238
php_req.tv_sec = (time_t) (diff_ns / ns_per_sec);
12361239
php_req.tv_nsec = (long) (diff_ns % ns_per_sec);
12371240

1238-
while (nanosleep(&php_req, &php_rem)) {
1239-
if (errno == EINTR) {
1240-
php_req.tv_sec = php_rem.tv_sec;
1241-
php_req.tv_nsec = php_rem.tv_nsec;
1242-
} else {
1241+
while (nanosleep(&php_req, NULL)) {
1242+
if (errno != EINTR) {
12431243
RETURN_FALSE;
12441244
}
1245+
if (UNEXPECTED(atomic_load(&EG(vm_interrupt)))) {
1246+
zend_fcall_interrupt(execute_data);
1247+
if (UNEXPECTED(EG(exception))) {
1248+
RETURN_THROWS();
1249+
}
1250+
}
1251+
zend_hrtime_t elapsed = zend_hrtime() - start;
1252+
if (elapsed >= diff_ns) {
1253+
break;
1254+
}
1255+
uint64_t remaining_ns = diff_ns - elapsed;
1256+
php_req.tv_sec = (time_t) (remaining_ns / ns_per_sec);
1257+
php_req.tv_nsec = (long) (remaining_ns % ns_per_sec);
12451258
}
12461259

12471260
RETURN_TRUE;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
time_sleep_until() dispatches async signals and keeps the original deadline
3+
--EXTENSIONS--
4+
pcntl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('time_sleep_until')) die('skip time_sleep_until() not available');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
9+
?>
10+
--FILE--
11+
<?php
12+
pcntl_async_signals(true);
13+
$deadline = microtime(true) + 2;
14+
$calledBeforeDeadline = false;
15+
pcntl_signal(SIGALRM, static function () use ($deadline, &$calledBeforeDeadline): void {
16+
$calledBeforeDeadline = microtime(true) < $deadline;
17+
time_sleep_until($deadline + 0.1);
18+
});
19+
20+
pcntl_alarm(1);
21+
var_dump(time_sleep_until($deadline));
22+
var_dump($calledBeforeDeadline);
23+
var_dump(microtime(true) >= $deadline);
24+
var_dump(microtime(true) < $deadline + 0.8);
25+
?>
26+
--EXPECT--
27+
bool(true)
28+
bool(true)
29+
bool(true)
30+
bool(true)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
An async signal handler can throw during time_sleep_until()
3+
--EXTENSIONS--
4+
pcntl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('time_sleep_until')) die('skip time_sleep_until() not available');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
9+
?>
10+
--FILE--
11+
<?php
12+
pcntl_async_signals(true);
13+
$deadline = microtime(true) + 2;
14+
pcntl_signal(SIGALRM, static function () use ($deadline): void {
15+
var_dump(microtime(true) < $deadline);
16+
throw new Exception('Interrupted');
17+
});
18+
19+
pcntl_alarm(1);
20+
try {
21+
time_sleep_until($deadline);
22+
echo "Sleep completed\n";
23+
} catch (Exception $e) {
24+
echo $e->getMessage(), "\n";
25+
}
26+
?>
27+
--EXPECT--
28+
bool(true)
29+
Interrupted
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
An async signal handler can exit during time_sleep_until()
3+
--EXTENSIONS--
4+
pcntl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('time_sleep_until')) die('skip time_sleep_until() not available');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
9+
?>
10+
--FILE--
11+
<?php
12+
pcntl_async_signals(true);
13+
$deadline = microtime(true) + 2;
14+
pcntl_signal(SIGALRM, static function () use ($deadline): void {
15+
var_dump(microtime(true) < $deadline);
16+
exit;
17+
});
18+
19+
pcntl_alarm(1);
20+
time_sleep_until($deadline);
21+
echo "Sleep completed\n";
22+
?>
23+
--EXPECT--
24+
bool(true)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
time_sleep_until() leaves manually dispatched signals pending
3+
--EXTENSIONS--
4+
pcntl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('time_sleep_until')) die('skip time_sleep_until() not available');
8+
if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
9+
?>
10+
--FILE--
11+
<?php
12+
pcntl_async_signals(false);
13+
$called = false;
14+
pcntl_signal(SIGALRM, static function () use (&$called): void {
15+
$called = true;
16+
});
17+
18+
pcntl_alarm(1);
19+
var_dump(time_sleep_until(microtime(true) + 2));
20+
var_dump($called);
21+
pcntl_signal_dispatch();
22+
var_dump($called);
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
bool(false)
27+
bool(true)

0 commit comments

Comments
 (0)