Repository navigation
Description
Description
With the kqueue backend of the new Io\Poll API, every call to Io\Poll\Watcher::modifyEvents() permanently allocates about 32 bytes (one struct kevent). The memory isn't released by Context::wait(), only when the Context is destroyed. The poll backend doesn't leak with the same code, and adding and removing watchers doesn't leak either.
Long-running programs that enable and disable read/write interest on a handle (such as event loops) grow without bound on macOS and BSD.
<?php
use Io\Poll\{Backend, Context, Event};
use Time\Duration;
[$socket] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
foreach ([Backend::Poll, Backend::Kqueue, Backend::Poll, Backend::Kqueue] as $backend) {
$context = new Context($backend);
$watcher = $context->add(new StreamPollHandle($socket), [Event::Read]);
$before = memory_get_usage();
for ($i = 0; $i < 10_000; $i++) {
$watcher->modifyEvents($i % 2 ? [Event::Read] : [Event::Read, Event::Write]);
$context->wait(Duration::fromSeconds(0));
}
printf("%s: %+d bytes after 10000 modifyEvents()\n", $backend->name, memory_get_usage() - $before);
$watcher->remove();
}Resulted in this output:
Poll: +256 bytes after 10000 modifyEvents()
Kqueue: +321024 bytes after 10000 modifyEvents()
Poll: +0 bytes after 10000 modifyEvents()
Kqueue: +321024 bytes after 10000 modifyEvents()
But I expected this output instead:
Poll: +256 bytes after 10000 modifyEvents()
Kqueue: +0 bytes after 10000 modifyEvents()
Poll: +0 bytes after 10000 modifyEvents()
Kqueue: +0 bytes after 10000 modifyEvents()
The 256 bytes of the first run are one-time allocations, whichever backend runs first. Kqueue grows by the same amount on every run.
Calling wait() once after all the modifyEvents() calls, instead of after each one, gives the same growth (+327072 bytes for 10000 calls). Destroying the Context releases the memory.
PHP Version
PHP 8.6.0RC4 (cli) (ZTS DEBUG)
Operating System
macOS 26.5.2 (Darwin 25.5, arm64), also reproduced on GitHub Actions macos-latest
PHP Version
PHP 8.6.0RC4 (cli) (built: Oct 31 2026 13:20:44) (ZTS DEBUG)
Copyright © The PHP Group and Contributors
Zend Engine v4.6.0RC4, Copyright © Zend by Perforce
with Zend OPcache v8.6.0RC4, Copyright ©, by Zend by Perforce
Activity
Skip the stream memory leak tests for IoPollDriver with kqueue on 8.6…
Skip the stream memory leak tests whenever kqueue leaks, not only on …
Fix phpGH-25086: Io\Poll kqueue backend leaks memory on every modifyE…
Fix GH-25086: Io\Poll kqueue backend leaks memory on every modifyEven…
Merge branch 'PHP-8.6'
Skip the stream memory leak tests for IoPollDriver with kqueue on 8.6…
Skip the stream memory leak tests whenever kqueue leaks, not only on …
[ci skip] Merge branch 'PHP-8.6' into fix-zend-long-uint-ovfl-32bit-8.6
Skip the stream memory leak tests for IoPollDriver with kqueue on 8.6…
Skip the stream memory leak tests whenever kqueue leaks, not only on …
Skip the stream memory leak tests for IoPollDriver with kqueue on 8.6…
Skip the stream memory leak tests whenever kqueue leaks, not only on …