[php-src] Issue #25086: Io\Poll kqueue backend leaks memory on every Watcher::modifyEvents() call
| From: | kelunik | Date: | Sat, 31 Oct 2026 14:36:03 +0000 |
| Subject: | [php-src] Issue #25086: Io\Poll kqueue backend leaks memory on every Watcher::modifyEvents() call | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-253949@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25086
Author: kelunik
### 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
<?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 (Darwin 25.5, arm64), also reproduced on GitHub Actions macos-latest
### PHP Version
```plain
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
```
### Operating System
macOS 26.5.2