Skip to content

macOS agent: camera watcher leaks autorelease-pool entries every second (no pool on its thread) #2650

Backlog

Description

@mikahsargent

On macOS the agent's camera watcher (watchers::camera::spawn, 1 s period from startup.rs) calls CoreMediaIO on a plain std::thread with no autorelease pool. CoreMediaIO autoreleases a few objects on every CMIOObjectGetPropertyData call, so the thread's implicit pool grows forever.

What I saw: OpenLogi 0.9.0 on macOS 27.2.1 (Apple Silicon), MX Master 4 over Bluetooth, permissions granted. The agent grew steadily, about 4.5 MB a day, around the clock, for 12 days (22 MB to 72 MB on one PID). No retiring its channel lines in the logs, and HIDLibElement stayed at 1,248, so this isn't #2617. heap on the 12-day agent:

   COUNT      BYTES       AVG   CLASS_NAME
    8688   35586048    4096.0   @autoreleasepool content

That's about half the footprint, and it's pool pages, not live objects.

Repro outside OpenLogi: a small C program doing the same calls as camera_in_use() (dev#, then gone per device) on a pthread:

no pool: 20000 polls, heap +41.4 bytes/poll   (heap: 203 × "@autoreleasepool content")
pool   : 10000 polls, heap +0.0 bytes/poll

At one poll a second, that's about 3.5 MB a day, which matches what I measured on the agent. The cost scales with the number of CMIO devices. This Mac has five, two of them virtual (CMIOExtension), which may be why it's slow here and invisible for others.

Suggested fix: wrap each poll in objc2::rc::autoreleasepool(|_| camera_in_use()) in the watcher loop (or inside macos::camera_in_use). A property listener instead of a 1 s poll would also work, but the pool is the minimal fix.

Repro source (clang -O1 -o repro repro.c -framework CoreMediaIO -framework CoreFoundation -lobjc, then ./repro nopool 20000 or ./repro pool 20000)
// Mirrors OpenLogi 0.9.0's camera watcher: CMIO "dev#" then "gone" per device,
// on a plain pthread, optionally inside an autorelease pool per iteration.
#include <CoreMediaIO/CMIOHardware.h>
#include <malloc/malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern void *objc_autoreleasePoolPush(void);
extern void objc_autoreleasePoolPop(void *);
static int use_pool, iters;
static void poll_once(void) {
  CMIOObjectPropertyAddress a = {kCMIOHardwarePropertyDevices, kCMIOObjectPropertyScopeGlobal, kCMIOObjectPropertyElementMain};
  UInt32 size = 0, used = 0;
  if (CMIOObjectGetPropertyDataSize(kCMIOObjectSystemObject, &a, 0, NULL, &size)) return;
  CMIOObjectID *devs = malloc(size);
  if (CMIOObjectGetPropertyData(kCMIOObjectSystemObject, &a, 0, NULL, size, &used, devs)) { free(devs); return; }
  CMIOObjectPropertyAddress r = {kCMIODevicePropertyDeviceIsRunningSomewhere, kCMIOObjectPropertyScopeGlobal, kCMIOObjectPropertyElementMain};
  for (UInt32 i = 0; i < used / sizeof(CMIOObjectID); i++) {
    UInt32 running = 0, pu = 0;
    if (CMIOObjectGetPropertyData(devs[i], &r, 0, NULL, sizeof running, &pu, &running) == 0 && running) break;
  }
  free(devs);
}
static size_t inuse(void) { malloc_statistics_t s; malloc_zone_statistics(NULL, &s); return s.size_in_use; }
static void *worker(void *x) {
  poll_once(); // warm up
  size_t before = inuse();
  for (int i = 0; i < iters; i++) {
    void *p = use_pool ? objc_autoreleasePoolPush() : NULL;
    poll_once();
    if (p) objc_autoreleasePoolPop(p);
  }
  size_t after = inuse();
  printf("%s: %d polls, heap in use %zu -> %zu bytes (%+.1f bytes/poll)\n", use_pool ? "pool   " : "no pool", iters, before, after, (double)((long)after - (long)before) / iters);
  fflush(stdout);
  if (getenv("PAUSE")) sleep(30);
  return NULL;
}
int main(int c, char **v) {
  use_pool = c > 1 && !strcmp(v[1], "pool");
  iters = c > 2 ? atoi(v[2]) : 3000;
  pthread_t t; pthread_create(&t, NULL, worker, NULL); pthread_join(t, NULL);
  return 0;
}

Note: running heap on the live agent trips the HID tap watchdog and exits it (as #2617 mentions), so grab data with footprint instead.

Activity

  1. added
    platform: macos
    type: bug
    on Nov 30, 2026
  2. added a commit that references this issue on Dec 1, 2026
    4f189cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform: macostype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions