Skip to content

ext/standard: Io\Poll\Context sharing its backend with a forked child. - #25613

Open
devnexen wants to merge 3 commits into
php:PHP-8.6from
devnexen:gh25611
Open

devnexen wants to merge 3 commits into
php:PHP-8.6from
devnexen:gh25611

Conversation

@devnexen

Copy link
Copy Markdown
Member

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.

Fix phpGH-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.

@iliaal iliaal left a comment

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.

Rebuilding the backend on first use in the child makes sense. I’d hold this until registration failures are handled. I forced one EPOLL_CTL_ADD to return ENOSPC. The watcher still reported active, but two consecutive waits missed its readable socket.

Comment thread ext/standard/io_poll.c Outdated
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);

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.

Please check php_poll_add() here. Only replace context->ctx and set owner_pid after every armed watcher has been registered. On failure, discard the new backend and propagate its error so the next call can retry.

Comment thread ext/standard/tests/poll/gh25611.phpt Outdated
fclose($reader);
exit(0);
}
pcntl_waitpid($pid, $status);

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 check that the child exited normally with status zero? It emits no output in this case, so the test can pass even if the child dies during cleanup.

$watcher = pt_stream_poll_add($context, $reader, [Event::Read], 'inherited');
$pid = pcntl_fork();
if ($pid === 0) {
$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.

Comment thread ext/standard/io_poll.c Outdated
return false;
}
if (php_poll_init(ctx) != SUCCESS) {
php_poll_destroy(ctx);

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 save php_poll_get_error(ctx) before destroying ctx and pass it through to the exception, as the constructor does? Otherwise callers lose the reason initialization failed after a fork.

@iliaal iliaal left a comment

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.

Thanks, looks good now.

@bukka

bukka commented Nov 23, 2026

Copy link
Copy Markdown
Member

@devnexen don't merge - I want to review it first

@bukka bukka left a comment

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.

I don't like that many getpid() calls. I think we should hook it up to php_child_init() and do some sort of counter there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Io\Poll\Context::wait() segfaults after a forked child adds a handle to the inherited epoll context

3 participants