Repository navigation
fix(computer-use): release macOS screenshot memory after each request - #27374
Conversation
The macOS helper serves each sidecar connection on a dedicated thread that lives as long as the connection, and the sidecar keeps one connection open while the helper runs. Encoding a screenshot with NSBitmapImageRep leaves autoreleased objects that keep the captured image alive, and that thread never drained its pool, so every screenshot kept a window-sized buffer in WindowServer until the helper restarted. Drain an autorelease pool around each request.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to The macOS request loop appears ready to merge after normal checks; no actionable risk remains in the supplied evidence. Pre-merge checks |
|
ELI5
On macOS, every screenshot an agent took through Computer Use stayed in memory until Orca restarted. Over a day or two that adds up to gigabytes, and it shows up under the system's WindowServer process rather than under Orca. Now each screenshot is freed as soon as the request that took it finishes.
What Changed
Before: each
get-app-statethat captured a screenshot added about one window-sized image to WindowServer's memory, about 22 MB for a 1512x949 window on a Retina display. It was only returned when the helper exited.After: WindowServer stays flat however many screenshots are taken.
Mechanism: the helper handles each sidecar connection on its own thread (
SocketListener.handleConnection), and the sidecar keeps one connection open for the helper's lifetime.boundedPngDataencodes withNSBitmapImageRep, which leaves autoreleased objects that keep the captured image alive. That thread never drained an autorelease pool, so every capture stayed alive. The request loop now runs throughAgentConnectionRequestLoop.runin the core module, which wraps each request inautoreleasepool. The loop body inmain.swiftis unchanged, except thatcontinueandreturnbecomereturn trueandreturn false. The snapshot cache already drops the screenshot (#4094), so after this change nothing holds a capture past its request.Why
A per-request pool is the standard way to bound autoreleased objects on a long-lived thread, and it covers every request type, not only screenshots.
Alternatives I considered:
boundedPngData. Fixes this path, but any other AppKit or Accessibility call that autoreleases during a request would pile up the same way.Moving the loop into the core module lets a unit test cover it, the same way
AuthenticatedConnectionHangupMonitoris tested.Linked Issue
Fixes #27373
Visual Proof
N/A: no UI change. This fixes memory retention in the macOS helper; the numbers are in Testing.
Testing
AgentConnectionRequestLoopTests: an object autoreleased while one request is handled must be released before the next request is read. Two more cases cover end of input and the handler closing the connection. The first test fails without the pool and passes with it.node config/scripts/verify-computer-native.mjspasses on macOS 26.5.2 (Apple silicon), including all 97 Swift tests and the universal helper app bundle and signature check (ad-hoc signed). The Windows checks were skipped becausepwshis not installed.Real captures: a standalone build of the same ScreenCaptureKit + PNG code, 20 captures of a 1512x949 window on a thread that stays alive. WindowServer grew by 441 MB with the old loop and by 0 MB through
AgentConnectionRequestLoop.run.Shipped v1.4.224 before the fix: 30
get-app-statecalls added 669 MB to WindowServer, and 30 with--no-screenshotadded 0 MB.Not done: running a full Orca build with the patched helper signed for Screen Recording, and
pnpm lint/typecheck/test/build, since no TypeScript changed.I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
The investigation, code and this description were produced with Claude Opus 5.5 (Anthropic) in Claude Code, at my direction.
Review
AI code review summary:
native/computer-use-macos, macOS 14+) changes. No Linux or Windows code is touched.Agent skill upstream boundary
Notes
#9128 (open, conflicting since July) also edits this loop; whichever lands second needs a small rebase.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (Swift-only change:verify-computer-nativepasses locally; CI will cover the rest)Author
GitHub: @f8team. X/Twitter handle: not provided.