Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ PHP NEWS
. Fixed bug GH-23106 (mb_strpos() reads past the end of a haystack ending in
a truncated UTF-8 sequence). (Lazizbek Ergashev)

- PCNTL:
. Fixed pcntl_signal_dispatch() dropping the queued signals when it runs while
an exception is pending. (nicolas-grekas)
. Fixed pcntl_signal_dispatch() dropping the signals queued behind a handler
that throws. (nicolas-grekas)

- Zip:
. Fixed ZipArchive::extractTo() ignoring files given in a non-list array.
(David Carlier)
Expand Down
65 changes: 55 additions & 10 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ext/standard/info.h"
#include "php_signal.h"
#include "php_ticks.h"
#include "zend_exceptions.h"
#include "zend_fibers.h"

#if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY) || defined(HAVE_WAIT3)
Expand Down Expand Up @@ -1318,6 +1319,8 @@ void pcntl_signal_dispatch(void)
{
zval params[2], *handle, retval;
struct php_pcntl_pending_signal *queue, *next;
zend_object *old_exception;
const zend_op *old_opline_before_exception = NULL;
sigset_t mask;
sigset_t old_mask;

Expand Down Expand Up @@ -1345,8 +1348,25 @@ void pcntl_signal_dispatch(void)
PCNTL_G(head) = NULL; /* simple stores are atomic */
PCNTL_G(tail) = NULL;

/* Dispatching can happen while an exception is propagating, typically from the interrupt
* check ZEND_DO_FCALL runs right after an internal function returned with an exception
* pending. call_user_function() does nothing in that state, so set the exception aside
* while the handlers run, the way destructors are called during unwinding. Restoring
* EG(opline_before_exception) matters: ZEND_HANDLE_EXCEPTION derives the throwing op, and
* from it the enclosing try block, out of that pointer. */
old_exception = EG(exception);
if (old_exception) {
if (EG(current_execute_data)) {
EG(current_execute_data)->opline = EG(opline_before_exception);
old_opline_before_exception = EG(opline_before_exception);
}
EG(exception) = NULL;
}

/* Allocate */
while (queue) {
bool handler_threw = false;

if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
if (Z_TYPE_P(handle) != IS_LONG) {
ZVAL_NULL(&retval);
Expand All @@ -1365,27 +1385,52 @@ void pcntl_signal_dispatch(void)
#ifdef HAVE_STRUCT_SIGINFO_T
zval_ptr_dtor(&params[1]);
#endif
if (EG(exception)) {
break;
}
handler_threw = NULL != EG(exception);
}
}

next = queue->next;
queue->next = PCNTL_G(spares);
PCNTL_G(spares) = queue;
queue = next;

/* No other handler can be called while the exception propagates */
if (handler_threw) {
break;
}
}

/* drain the remaining in case of exception thrown */
while (queue) {
next = queue->next;
queue->next = PCNTL_G(spares);
PCNTL_G(spares) = queue;
queue = next;
if (old_exception) {
if (EG(current_execute_data)) {
EG(current_execute_data)->opline = EG(exception_op);
EG(opline_before_exception) = old_opline_before_exception;
}
if (EG(exception)) {
zend_exception_set_previous(EG(exception), old_exception);
} else {
EG(exception) = old_exception;
}
}

PCNTL_G(pending_signals) = 0;
if (UNEXPECTED(queue)) {
/* Put back what the throwing handler did not get to, instead of dropping it, and ask
* the engine to come back once the exception has been handled. Signals are still
* blocked here, so PCNTL_G(head) cannot have been repopulated in the meantime. */
next = queue;

while (next->next) {
next = next->next;
}

PCNTL_G(head) = queue;
PCNTL_G(tail) = next;

if (PCNTL_G(async_signals)) {
zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
}
} else {
PCNTL_G(pending_signals) = 0;
}

/* Re-enable queue */
PCNTL_G(processing_signal_queue) = 0;
Expand Down
44 changes: 44 additions & 0 deletions ext/pcntl/tests/pcntl_signal_dispatch_exception_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
pcntl_signal_dispatch() keeps the signals left in the queue by a throwing handler
--EXTENSIONS--
pcntl
posix
--FILE--
<?php

$called = [];

pcntl_signal(SIGUSR1, function ($signo) use (&$called) {
$called[] = 'SIGUSR1';
throw new \Exception('Exception in signal handler');
});

pcntl_signal(SIGUSR2, function ($signo) use (&$called) {
$called[] = 'SIGUSR2';
});

pcntl_signal(SIGHUP, function ($signo) use (&$called) {
$called[] = 'SIGHUP';
});

posix_kill(posix_getpid(), SIGUSR1);
posix_kill(posix_getpid(), SIGUSR2);
posix_kill(posix_getpid(), SIGHUP);

try {
pcntl_signal_dispatch();
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}

echo "Handlers called: " . implode(', ', $called) . "\n";

pcntl_signal_dispatch();

echo "Handlers called: " . implode(', ', $called) . "\n";

?>
--EXPECT--
Exception in signal handler
Handlers called: SIGUSR1
Handlers called: SIGUSR1, SIGUSR2, SIGHUP
47 changes: 47 additions & 0 deletions ext/pcntl/tests/pcntl_signal_dispatch_exception_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
pcntl_signal_dispatch() delivers the signals a throwing handler left behind once its exception is handled
--EXTENSIONS--
pcntl
posix
--FILE--
<?php

$called = [];

pcntl_signal(SIGUSR1, function ($signo) use (&$called) {
$called[] = 'SIGUSR1';
throw new \Exception('Exception in signal handler');
});

pcntl_signal(SIGUSR2, function ($signo) use (&$called) {
$called[] = 'SIGUSR2';
});

pcntl_signal(SIGHUP, function ($signo) use (&$called) {
$called[] = 'SIGHUP';
});

// Queued, not dispatched: asynchronous signals are off
posix_kill(posix_getpid(), SIGUSR1);
posix_kill(posix_getpid(), SIGUSR2);

pcntl_async_signals(true);

try {
// Delivered asynchronously, so the whole queue is dispatched
posix_kill(posix_getpid(), SIGHUP);
echo "Not reached\n";
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}

// No explicit dispatch: the engine delivers what the throwing handler left behind
// on its own, as soon as the exception has been handled
usleep(1000);

echo "Handlers called: " . implode(', ', $called) . "\n";

?>
--EXPECT--
Exception in signal handler
Handlers called: SIGUSR1, SIGUSR2, SIGHUP
Loading