diff --git a/ext/standard/io_poll.c b/ext/standard/io_poll.c index 583dfdbc0335..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,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; }; @@ -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); @@ -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; @@ -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) @@ -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) { @@ -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) { diff --git a/ext/standard/tests/poll/gh25611.phpt b/ext/standard/tests/poll/gh25611.phpt new file mode 100644 index 000000000000..fc5b19e3ce73 --- /dev/null +++ b/ext/standard/tests/poll/gh25611.phpt @@ -0,0 +1,91 @@ +--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'; +} + +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]); + 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 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 65e825dbf858..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,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) */ @@ -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; @@ -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);