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
68 changes: 67 additions & 1 deletion ext/standard/io_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "php.h"
#include "zend_enum.h"
#include "zend_exceptions.h"
#include "php_main.h"
#include "php_network.h"
#include "php_poll.h"
#include "io_poll.h"
Expand Down Expand Up @@ -70,6 +71,7 @@ struct php_io_poll_context_object {
php_poll_ctx *ctx;
HashTable *watchers; /* Maps fd -> watcher object */
bool buffered_readers; /* Some watcher can report buffered data */
uint32_t genid;
zend_object std;
};

Expand Down Expand Up @@ -363,12 +365,63 @@ static void php_io_poll_stream_unwatch(php_io_poll_watcher_object *watcher)
}
}

static php_poll_error php_io_poll_context_rebuild_after_fork(php_io_poll_context_object *context)
{
uint32_t genid = php_child_generate();

if (EXPECTED(context->genid == genid)) {
return PHP_POLL_ERR_NONE;
}

php_poll_ctx *ctx = php_poll_create(php_poll_get_backend_type(context->ctx), 0);
if (!ctx) {
return PHP_POLL_ERR_SYSTEM;
}
if (php_poll_init(ctx) != SUCCESS) {
goto failure;
}

ZEND_HASH_FOREACH_VAL(context->watchers, zval *zv) {
php_io_poll_watcher_object *watcher = PHP_POLL_WATCHER_OBJ_FROM_ZV(zv);
if (!watcher->disarmed
&& php_poll_add(ctx, (int) watcher->fd, watcher->watched_events, watcher) != SUCCESS) {
goto failure;
}
} ZEND_HASH_FOREACH_END();

php_poll_destroy(context->ctx);
context->ctx = ctx;
context->genid = genid;

return PHP_POLL_ERR_NONE;

failure:;
php_poll_error err = php_poll_get_error(ctx);
php_poll_destroy(ctx);
return err != PHP_POLL_ERR_NONE ? err : PHP_POLL_ERR_SYSTEM;
}

static bool php_io_poll_context_rebuild_after_fork_or_throw(php_io_poll_context_object *context)
{
php_poll_error err = php_io_poll_context_rebuild_after_fork(context);

if (EXPECTED(err == PHP_POLL_ERR_NONE)) {
return true;
}

php_io_poll_throw_failed_operation(php_io_poll_failed_context_init_class_entry,
"Failed to initialize polling context", err);
return false;
}

static void php_io_poll_context_retire_watcher(
php_io_poll_context_object *context, php_io_poll_watcher_object *watcher)
{
php_socket_t fd = watcher->fd;

php_poll_remove(context->ctx, (int) fd);
if (php_io_poll_context_rebuild_after_fork(context) == PHP_POLL_ERR_NONE) {
php_poll_remove(context->ctx, (int) fd);
}
php_io_poll_stream_unwatch(watcher);
php_io_poll_watcher_deactivate(watcher);
zend_hash_index_del(context->watchers, (zend_ulong) fd);
Expand Down Expand Up @@ -473,6 +526,10 @@ static zend_result php_io_poll_watcher_modify_events(
return FAILURE;
}

if (!php_io_poll_context_rebuild_after_fork_or_throw(watcher->context)) {
return FAILURE;
}

/* Re-add if the backend dropped a fired one-shot registration */
php_poll_ctx *poll_ctx = watcher->context->ctx;
int fd = (int) watcher->fd;
Expand Down Expand Up @@ -790,6 +847,7 @@ PHP_METHOD(Io_Poll_Context, __construct)

intern->watchers = emalloc(sizeof(HashTable));
zend_hash_init(intern->watchers, 8, NULL, ZVAL_PTR_DTOR, 0);
intern->genid = php_child_generate();
}

PHP_METHOD(Io_Poll_Context, add)
Expand All @@ -814,6 +872,10 @@ PHP_METHOD(Io_Poll_Context, add)
RETURN_THROWS();
}

if (!php_io_poll_context_rebuild_after_fork_or_throw(intern)) {
RETURN_THROWS();
}

/* Get file descriptor */
php_socket_t fd = php_poll_handle_get_fd(handle);
if (fd == SOCK_ERR) {
Expand Down Expand Up @@ -910,6 +972,10 @@ PHP_METHOD(Io_Poll_Context, wait)
timeout_ts.tv_nsec = timeout->duration.nanoseconds;
}

if (!php_io_poll_context_rebuild_after_fork_or_throw(intern)) {
RETURN_THROWS();
}

if (max_events_is_null) {
max_events = php_poll_get_suitable_max_events(intern->ctx);
if (max_events <= 0) {
Expand Down
91 changes: 91 additions & 0 deletions ext/standard/tests/poll/gh25611.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
--TEST--
GH-25611 (Io\Poll\Context shares its backend state with a forked child)
--CREDITS--
alies-dev
--EXTENSIONS--
pcntl
--FILE--
<?php
require_once __DIR__ . '/poll.inc';

use Io\Poll\Event;

function wait_names(Io\Poll\Context $context, int $ms): string {
$names = [];
foreach ($context->wait(Time\Duration::fromMilliseconds($ms)) as $watcher) {
$names[] = $watcher->getData();
}
return $names ? implode(',', $names) : 'none';
}

function wait_child(int $pid): void {
pcntl_waitpid($pid, $status);
echo "child exited cleanly: ", var_export(pcntl_wifexited($status) && pcntl_wexitstatus($status) === 0, true), PHP_EOL;
}

echo "child adds a handle", PHP_EOL;
$context = pt_new_stream_poll();
[$reader, $writer] = pt_new_socket_pair();
[$idle, $idleWriter] = pt_new_socket_pair();
$pid = pcntl_fork();
$watcher = pt_stream_poll_add($context, $pid === 0 ? $reader : $idle, [Event::Read], $pid === 0 ? 'child' : 'parent');
if ($pid === 0) {
fwrite($writer, 'x');
echo "child: ", wait_names($context, 1000), PHP_EOL;
exit(0);
}
wait_child($pid);
echo "parent: ", wait_names($context, 100), PHP_EOL;
unset($context, $watcher);

echo "child closes a watched stream", PHP_EOL;
$context = pt_new_stream_poll();
[$reader, $writer] = pt_new_socket_pair();
$watcher = pt_stream_poll_add($context, $reader, [Event::Read], 'parent');
$pid = pcntl_fork();
if ($pid === 0) {
fclose($reader);
exit(0);
}
wait_child($pid);
fwrite($writer, 'x');
echo "parent: ", wait_names($context, 1000), PHP_EOL;
unset($context, $watcher);

echo "child waits on an inherited watcher", PHP_EOL;
$context = pt_new_stream_poll();
[$reader, $writer] = pt_new_socket_pair();
[$other, $otherWriter] = pt_new_socket_pair();
$watcher = pt_stream_poll_add($context, $reader, [Event::Read], 'inherited');
$pid = pcntl_fork();
if ($pid === 0) {
fwrite($writer, 'x');
echo "child: ", wait_names($context, 1000), PHP_EOL;
fread($reader, 1);
$watcher->modifyEvents([Event::Read, Event::OneShot]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make wait() the child’s first context operation and assert that it returns the inherited watcher? modifyEvents() can re-register a missing watcher, and the child currently checks readiness only on the newly added one.

pt_stream_poll_add($context, $other, [Event::Read], 'child only');
fwrite($otherWriter, 'x');
echo "child: ", wait_names($context, 1000), PHP_EOL;
exit(0);
}
wait_child($pid);
echo "parent: ", wait_names($context, 100), PHP_EOL;
fwrite($writer, 'x');
echo "parent: ", wait_names($context, 1000), PHP_EOL;
echo "parent: ", wait_names($context, 100), PHP_EOL;
?>
--EXPECT--
child adds a handle
child: child
child exited cleanly: true
parent: none
child closes a watched stream
child exited cleanly: true
parent: parent
child waits on an inherited watcher
child: inherited
child: child only
child exited cleanly: true
parent: none
parent: inherited
parent: inherited
8 changes: 8 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,16 @@ static void sigchld_handler(int apar)
/* }}} */
#endif

static uint32_t php_child_genid = 0;

PHPAPI uint32_t php_child_generate(void)
{
return php_child_genid;
}

PHPAPI void php_child_init(void)
{
php_child_genid++;
refresh_memory_manager();
zend_max_execution_timer_init();
php_clear_stat_cache(true, NULL, 0);
Expand Down
1 change: 1 addition & 0 deletions main/php_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ PHPAPI char *php_get_version(sapi_module_struct *sapi_module);
PHPAPI void php_print_version(sapi_module_struct *sapi_module);

PHPAPI void php_child_init(void);
PHPAPI uint32_t php_child_generate(void);
PHPAPI zend_result php_request_startup(void);
PHPAPI void php_request_shutdown(void *dummy);
PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module);
Expand Down
5 changes: 4 additions & 1 deletion main/poll/poll_backend_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

#include "php_poll_internal.h"
#include "php_main.h"

#ifdef HAVE_KQUEUE

Expand All @@ -29,6 +30,7 @@

typedef struct kqueue_backend_data {
int kqueue_fd;
uint32_t genid;
struct kevent *events;
int events_capacity;
int fd_count; /* Track number of unique FDs (not individual filters) */
Expand Down Expand Up @@ -62,6 +64,7 @@ static zend_result kqueue_backend_init(php_poll_ctx *ctx)
return FAILURE;
}
data->events_capacity = initial_capacity;
data->genid = php_child_generate();
data->fd_count = 0;
data->filter_count = 0;

Expand All @@ -81,7 +84,7 @@ static void kqueue_backend_cleanup(php_poll_ctx *ctx)
{
kqueue_backend_data_t *data = (kqueue_backend_data_t *) ctx->backend_data;
if (data) {
if (data->kqueue_fd >= 0) {
if (data->kqueue_fd >= 0 && data->genid == php_child_generate()) {
close(data->kqueue_fd);
}
pefree(data->events, ctx->persistent);
Expand Down
Loading