Skip to content

Io\Poll kqueue backend leaks memory on every Watcher::modifyEvents() call #25086

Closed

Description

@kelunik

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

  1. added 2 commits that reference this issue on Nov 1, 2026
    c0b8710
    1e1eb7f
  2. added a commit that references this issue on Nov 1, 2026
    2359a79
  3. added a commit that references this issue on Nov 2, 2026
    36c80e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions