Repository navigation
Description
Description
Found because revolt/event-loop 1.1.0 picks an Io\Poll driver on PHP 8.6, and Psalm forks its workers with amphp/parallel's ForkContext after the loop exists (vimeo/psalm#14238).
With the epoll backend, a pcntl_fork() child shares the parent's epoll instance (EPOLL_CLOEXEC only applies to exec). Context::add() stores the address of the new Watcher in the kernel (poll_backend_epoll.c#L130), and wait() turns the returned data.ptr back into a watcher (poll_backend_epoll.c#L215, io_poll.c#L980-L998). If the child adds a handle to the inherited context, wait() in the parent gets an address from the child's heap, reads and writes through it, and returns it as an object.
The following code:
<?php
use Io\Poll\{Backend, Context, Event};
$context = new Context(Backend::Epoll);
[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$pid = pcntl_fork();
if ($pid === 0) {
$context->add(new StreamPollHandle($reader), [Event::Read], 'child data');
fwrite($writer, 'x');
exit(0);
}
pcntl_waitpid($pid, $status);
// The parent never called add().
$start = hrtime(true);
$watchers = $context->wait(Time\Duration::fromMilliseconds(1000));
printf("%d watcher(s) after %d ms\n", count($watchers), (hrtime(true) - $start) / 1e6);
foreach ($watchers as $watcher) {
var_dump($watcher->getData());
}
echo "done\n";Resulted in this output (9 of 20 runs, each in a fresh container, exit code 139):
1 watcher(s) after 0 ms
Segmentation fault
The other 11 runs printed 0 watcher(s) after 0 ms and done. wait() returned at once, so the event arrived and its entry was dropped after reading the foreign pointer.
But I expected this output instead (what Backend::Poll prints on the same build, 5 of 5 runs):
0 watcher(s) after 1000 ms
done
Failing add() in the child would also be fine. That is what kqueue does on macOS (FailedHandleAddException), because a kqueue is not inherited.
If the parent makes the same allocations after the fork, the address belongs to the parent's own watcher, and wait() reports that watcher for a stream that has no data (10 of 10 runs):
<?php
use Io\Poll\{Backend, Context, Event};
$context = new Context(Backend::Epoll);
[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
[$idle, $idleWriter] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$pid = pcntl_fork();
// Same allocations in both processes: the child watches $reader, the parent watches $idle.
$watcher = $context->add(new StreamPollHandle($pid === 0 ? $reader : $idle), [Event::Read], $pid === 0 ? 'child' : 'parent');
if ($pid === 0) {
fwrite($writer, 'x');
exit(0);
}
pcntl_waitpid($pid, $status);
$read = [$idle];
$none = null;
echo 'stream_select() on $idle: ', stream_select($read, $none, $none, 0), "\n";
foreach ($context->wait(Time\Duration::fromMilliseconds(1000)) as $triggered) {
echo 'wait() returned the ', $triggered->getData(), ' watcher, triggered: ',
implode(',', array_map(fn ($e) => $e->name, $triggered->getTriggeredEvents())), "\n";
}stream_select() on $idle: 0
wait() returned the parent watcher, triggered: Read
The shared epoll set also affects a child that never uses the context. Closing an inherited watched stream in the child removes the parent's registration (php_io_poll_stream_notify_close() → php_poll_remove() → EPOLL_CTL_DEL on the shared epoll fd, io_poll.c#L366-L394):
<?php
use Io\Poll\{Backend, Context, Event};
$context = new Context(Backend::Epoll);
[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$context->add(new StreamPollHandle($reader), [Event::Read]);
if (($pid = pcntl_fork()) === 0) {
fclose($reader); // the child never touches $context
exit(0);
}
pcntl_waitpid($pid, $status);
fwrite($writer, 'x');
var_dump(count($context->wait(Time\Duration::fromMilliseconds(1000))));This prints int(0) after the 1 s timeout (5 of 5 runs). With Backend::Poll it prints int(1) at once.
The code involved is unchanged on PHP-8.6 and master. Perhaps the context could remember the process that created it and, in a forked child, either fail like kqueue or create a fresh epoll instance.
PHP Version
PHP 8.6.0 (cli) (built: Nov 19 2026 15:18:52) (ZTS)
Copyright © The PHP Group and Contributors
Built by https://github.com/docker-library/php
Zend Engine v4.6.0, Copyright © Zend by Perforce
with Zend OPcache v8.6.0, Copyright ©, by Zend by Perforce
Operating System
Debian GNU/Linux 13 (trixie), official php:8.6-cli Docker image with pcntl added, Linux 7.0.14-linuxkit aarch64
Activity
[-]Io\Poll\Context::wait() segfaults after a forked child adds a handle to the inherited epoll context[/-][+]`Io\Poll\Context::wait()` segfaults after a forked child adds a handle to the inherited epoll context[/+]