From 838a355e620a3a65570f12593fb3c79048becc6f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 19 Nov 2026 23:32:28 +0000 Subject: [PATCH 1/3] ext/standard: Io\Poll\Context sharing its backend with a forked child. Fix GH-25611 A context inherited through fork kept using the parent's epoll instance, so the child's registrations and removals reached the parent and wait() dereferenced watcher pointers from the other process. The context now rebuilds its backend on first use in a new process. --- ext/standard/io_poll.c | 60 ++++++++++++++++++++- ext/standard/tests/poll/gh25611.phpt | 79 ++++++++++++++++++++++++++++ main/poll/poll_backend_kqueue.c | 4 +- 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 ext/standard/tests/poll/gh25611.phpt diff --git a/ext/standard/io_poll.c b/ext/standard/io_poll.c index 583dfdbc0335..f475118843da 100644 --- a/ext/standard/io_poll.c +++ b/ext/standard/io_poll.c @@ -70,6 +70,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 */ + pid_t owner_pid; zend_object std; }; @@ -363,12 +364,56 @@ static void php_io_poll_stream_unwatch(php_io_poll_watcher_object *watcher) } } +static bool php_io_poll_context_rebuild_after_fork(php_io_poll_context_object *context) +{ + pid_t pid = getpid(); + + if (EXPECTED(context->owner_pid == pid)) { + return true; + } + + php_poll_ctx *ctx = php_poll_create(php_poll_get_backend_type(context->ctx), 0); + if (!ctx) { + return false; + } + if (php_poll_init(ctx) != SUCCESS) { + php_poll_destroy(ctx); + return false; + } + + php_poll_destroy(context->ctx); + context->ctx = ctx; + context->owner_pid = pid; + + 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); + } + } ZEND_HASH_FOREACH_END(); + + return true; +} + +static bool php_io_poll_context_rebuild_after_fork_or_throw(php_io_poll_context_object *context) +{ + if (EXPECTED(php_io_poll_context_rebuild_after_fork(context))) { + return true; + } + + php_io_poll_throw_failed_operation(php_io_poll_failed_context_init_class_entry, + "Failed to initialize polling context", PHP_POLL_ERR_SYSTEM); + 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_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); @@ -473,6 +518,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; @@ -790,6 +839,7 @@ PHP_METHOD(Io_Poll_Context, __construct) intern->watchers = emalloc(sizeof(HashTable)); zend_hash_init(intern->watchers, 8, NULL, ZVAL_PTR_DTOR, 0); + intern->owner_pid = getpid(); } PHP_METHOD(Io_Poll_Context, add) @@ -814,6 +864,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) { @@ -910,6 +964,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) { diff --git a/ext/standard/tests/poll/gh25611.phpt b/ext/standard/tests/poll/gh25611.phpt new file mode 100644 index 000000000000..42c6008147ad --- /dev/null +++ b/ext/standard/tests/poll/gh25611.phpt @@ -0,0 +1,79 @@ +--TEST-- +GH-25611 (Io\Poll\Context shares its backend state with a forked child) +--CREDITS-- +alies-dev +--EXTENSIONS-- +pcntl +--FILE-- +wait(Time\Duration::fromMilliseconds($ms)) as $watcher) { + $names[] = $watcher->getData(); + } + return $names ? implode(',', $names) : 'none'; +} + +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); +} +pcntl_waitpid($pid, $status); +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); +} +pcntl_waitpid($pid, $status); +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) { + $watcher->modifyEvents([Event::Read, Event::OneShot]); + pt_stream_poll_add($context, $other, [Event::Read], 'child only'); + fwrite($otherWriter, 'x'); + echo "child: ", wait_names($context, 1000), PHP_EOL; + exit(0); +} +pcntl_waitpid($pid, $status); +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 +parent: none +child closes a watched stream +parent: parent +child waits on an inherited watcher +child: child only +parent: none +parent: inherited +parent: inherited diff --git a/main/poll/poll_backend_kqueue.c b/main/poll/poll_backend_kqueue.c index 65e825dbf858..37e94e71891b 100644 --- a/main/poll/poll_backend_kqueue.c +++ b/main/poll/poll_backend_kqueue.c @@ -29,6 +29,7 @@ typedef struct kqueue_backend_data { int kqueue_fd; + pid_t owner_pid; struct kevent *events; int events_capacity; int fd_count; /* Track number of unique FDs (not individual filters) */ @@ -62,6 +63,7 @@ static zend_result kqueue_backend_init(php_poll_ctx *ctx) return FAILURE; } data->events_capacity = initial_capacity; + data->owner_pid = getpid(); data->fd_count = 0; data->filter_count = 0; @@ -81,7 +83,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->owner_pid == getpid()) { close(data->kqueue_fd); } pefree(data->events, ctx->persistent); From 8e01ec9ed676cced1f90f8f2b44bd32ea30e00f9 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 22 Nov 2026 18:16:33 +0000 Subject: [PATCH 2/3] feedback --- ext/standard/io_poll.c | 37 +++++++++++++++++----------- ext/standard/tests/poll/gh25611.phpt | 18 +++++++++++--- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/ext/standard/io_poll.c b/ext/standard/io_poll.c index f475118843da..e9936d5d3ec4 100644 --- a/ext/standard/io_poll.c +++ b/ext/standard/io_poll.c @@ -364,45 +364,52 @@ static void php_io_poll_stream_unwatch(php_io_poll_watcher_object *watcher) } } -static bool php_io_poll_context_rebuild_after_fork(php_io_poll_context_object *context) +static php_poll_error php_io_poll_context_rebuild_after_fork(php_io_poll_context_object *context) { pid_t pid = getpid(); if (EXPECTED(context->owner_pid == pid)) { - return true; + return PHP_POLL_ERR_NONE; } php_poll_ctx *ctx = php_poll_create(php_poll_get_backend_type(context->ctx), 0); if (!ctx) { - return false; + return PHP_POLL_ERR_SYSTEM; } if (php_poll_init(ctx) != SUCCESS) { - php_poll_destroy(ctx); - return false; + goto failure; } - php_poll_destroy(context->ctx); - context->ctx = ctx; - context->owner_pid = pid; - 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); + if (!watcher->disarmed + && php_poll_add(ctx, (int) watcher->fd, watcher->watched_events, watcher) != SUCCESS) { + goto failure; } } ZEND_HASH_FOREACH_END(); - return true; + php_poll_destroy(context->ctx); + context->ctx = ctx; + context->owner_pid = pid; + + 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) { - if (EXPECTED(php_io_poll_context_rebuild_after_fork(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", PHP_POLL_ERR_SYSTEM); + "Failed to initialize polling context", err); return false; } @@ -411,7 +418,7 @@ static void php_io_poll_context_retire_watcher( { php_socket_t fd = watcher->fd; - if (php_io_poll_context_rebuild_after_fork(context)) { + 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); diff --git a/ext/standard/tests/poll/gh25611.phpt b/ext/standard/tests/poll/gh25611.phpt index 42c6008147ad..fc5b19e3ce73 100644 --- a/ext/standard/tests/poll/gh25611.phpt +++ b/ext/standard/tests/poll/gh25611.phpt @@ -18,6 +18,11 @@ function wait_names(Io\Poll\Context $context, int $ms): string { 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(); @@ -29,7 +34,7 @@ if ($pid === 0) { echo "child: ", wait_names($context, 1000), PHP_EOL; exit(0); } -pcntl_waitpid($pid, $status); +wait_child($pid); echo "parent: ", wait_names($context, 100), PHP_EOL; unset($context, $watcher); @@ -42,7 +47,7 @@ if ($pid === 0) { fclose($reader); exit(0); } -pcntl_waitpid($pid, $status); +wait_child($pid); fwrite($writer, 'x'); echo "parent: ", wait_names($context, 1000), PHP_EOL; unset($context, $watcher); @@ -54,13 +59,16 @@ $context = pt_new_stream_poll(); $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]); pt_stream_poll_add($context, $other, [Event::Read], 'child only'); fwrite($otherWriter, 'x'); echo "child: ", wait_names($context, 1000), PHP_EOL; exit(0); } -pcntl_waitpid($pid, $status); +wait_child($pid); echo "parent: ", wait_names($context, 100), PHP_EOL; fwrite($writer, 'x'); echo "parent: ", wait_names($context, 1000), PHP_EOL; @@ -69,11 +77,15 @@ 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 From f7c49e029bb0ccddfef76d573107c7b6adb1e784 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 28 Nov 2026 19:10:50 +0000 Subject: [PATCH 3/3] apply suggestion --- ext/standard/io_poll.c | 11 ++++++----- main/main.c | 8 ++++++++ main/php_main.h | 1 + main/poll/poll_backend_kqueue.c | 7 ++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ext/standard/io_poll.c b/ext/standard/io_poll.c index e9936d5d3ec4..c605bafe5840 100644 --- a/ext/standard/io_poll.c +++ b/ext/standard/io_poll.c @@ -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" @@ -70,7 +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 */ - pid_t owner_pid; + uint32_t genid; zend_object std; }; @@ -366,9 +367,9 @@ 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) { - pid_t pid = getpid(); + uint32_t genid = php_child_generate(); - if (EXPECTED(context->owner_pid == pid)) { + if (EXPECTED(context->genid == genid)) { return PHP_POLL_ERR_NONE; } @@ -390,7 +391,7 @@ static php_poll_error php_io_poll_context_rebuild_after_fork(php_io_poll_context php_poll_destroy(context->ctx); context->ctx = ctx; - context->owner_pid = pid; + context->genid = genid; return PHP_POLL_ERR_NONE; @@ -846,7 +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->owner_pid = getpid(); + intern->genid = php_child_generate(); } PHP_METHOD(Io_Poll_Context, add) diff --git a/main/main.c b/main/main.c index f75223e6cc89..26305b54bf03 100644 --- a/main/main.c +++ b/main/main.c @@ -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); diff --git a/main/php_main.h b/main/php_main.h index de2ace1c25ed..fd98cd918958 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -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); diff --git a/main/poll/poll_backend_kqueue.c b/main/poll/poll_backend_kqueue.c index 37e94e71891b..207ededb3ee2 100644 --- a/main/poll/poll_backend_kqueue.c +++ b/main/poll/poll_backend_kqueue.c @@ -13,6 +13,7 @@ */ #include "php_poll_internal.h" +#include "php_main.h" #ifdef HAVE_KQUEUE @@ -29,7 +30,7 @@ typedef struct kqueue_backend_data { int kqueue_fd; - pid_t owner_pid; + uint32_t genid; struct kevent *events; int events_capacity; int fd_count; /* Track number of unique FDs (not individual filters) */ @@ -63,7 +64,7 @@ static zend_result kqueue_backend_init(php_poll_ctx *ctx) return FAILURE; } data->events_capacity = initial_capacity; - data->owner_pid = getpid(); + data->genid = php_child_generate(); data->fd_count = 0; data->filter_count = 0; @@ -83,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 && data->owner_pid == getpid()) { + if (data->kqueue_fd >= 0 && data->genid == php_child_generate()) { close(data->kqueue_fd); } pefree(data->events, ctx->persistent);