Skip to content

Commit f95fa2e

Browse files
committed
ext/pcntl: fix pcntl_signal_dispatch() stale tail pointer and exception handling.
1 parent fcb55f8 commit f95fa2e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

ext/pcntl/pcntl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ void pcntl_signal_dispatch(void)
13421342

13431343
queue = PCNTL_G(head);
13441344
PCNTL_G(head) = NULL; /* simple stores are atomic */
1345+
PCNTL_G(tail) = NULL;
13451346

13461347
/* Allocate */
13471348
while (queue) {
@@ -1363,6 +1364,9 @@ void pcntl_signal_dispatch(void)
13631364
#ifdef HAVE_STRUCT_SIGINFO_T
13641365
zval_ptr_dtor(&params[1]);
13651366
#endif
1367+
if (EG(exception)) {
1368+
break;
1369+
}
13661370
}
13671371
}
13681372

@@ -1372,6 +1376,14 @@ void pcntl_signal_dispatch(void)
13721376
queue = next;
13731377
}
13741378

1379+
/* drain the remaining in case of exception thrown */
1380+
while (queue) {
1381+
next = queue->next;
1382+
queue->next = PCNTL_G(spares);
1383+
PCNTL_G(spares) = queue;
1384+
queue = next;
1385+
}
1386+
13751387
PCNTL_G(pending_signals) = 0;
13761388

13771389
/* Re-enable queue */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
pcntl_signal_dispatch() stops dispatching after handler throws exception
3+
--EXTENSIONS--
4+
pcntl
5+
posix
6+
--FILE--
7+
<?php
8+
9+
$called = [];
10+
11+
pcntl_signal(SIGUSR1, function ($signo) use (&$called) {
12+
$called[] = 'SIGUSR1';
13+
throw new \Exception('Exception in signal handler');
14+
});
15+
16+
pcntl_signal(SIGUSR2, function ($signo) use (&$called) {
17+
$called[] = 'SIGUSR2';
18+
});
19+
20+
posix_kill(posix_getpid(), SIGUSR1);
21+
posix_kill(posix_getpid(), SIGUSR2);
22+
23+
try {
24+
pcntl_signal_dispatch();
25+
} catch (\Exception $e) {
26+
echo $e->getMessage() . "\n";
27+
}
28+
29+
echo "Handlers called: " . implode(', ', $called) . "\n";
30+
31+
?>
32+
--EXPECT--
33+
Exception in signal handler
34+
Handlers called: SIGUSR1

0 commit comments

Comments
 (0)