Skip to content

fix(computer-use): release macOS screenshot memory after each request - #27374

Merged
nwparker merged 1 commit into
stablyai:mainfrom
f8team:fix/macos-computer-use-screenshot-memory
Oct 11, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
f8team:fix/macos-computer-use-screenshot-memory

Conversation

@f8team

@f8team f8team commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

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-state that 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. boundedPngData encodes with NSBitmapImageRep, 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 through AgentConnectionRequestLoop.run in the core module, which wraps each request in autoreleasepool. The loop body in main.swift is unchanged, except that continue and return become return true and return 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:

  • Wrap only boundedPngData. Fixes this path, but any other AppKit or Accessibility call that autoreleases during a request would pile up the same way.
  • Reconnect per request in the sidecar. Ending the thread drains its pool, but it adds socket churn and touches session ownership.

Moving the loop into the core module lets a unit test cover it, the same way AuthenticatedConnectionHangupMonitor is 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.mjs passes 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 because pwsh is 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-state calls added 669 MB to WindowServer, and 30 with --no-screenshot added 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:

  • Cross-platform: only the macOS helper (native/computer-use-macos, macOS 14+) changes. No Linux or Windows code is touched.
  • SSH / remote / local: the helper runs on the machine whose desktop it controls. The wire protocol, RPC and sidecar are unchanged.
  • Agents and integrations: provider-neutral. Every Computer Use client benefits.
  • Performance: one autorelease pool push and pop per request, and WindowServer no longer grows without bound.
  • Security: no change to the token check, peer check or session ownership; the loop body is the same code.
  • Risk: objects autoreleased during a request are now released when that request ends instead of at disconnect. Nothing reads them after the response is written.

Agent skill upstream boundary

  • Not applicable: no upstream skill-installer material is copied or translated.

Notes

#9128 (open, conflicting since July) also edits this loop; whichever lands second needs a small rebase.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (Swift-only change: verify-computer-native passes locally; CI will cover the rest)

Author

GitHub: @f8team. X/Twitter handle: not provided.

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.
@coderabbitai

coderabbitai Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 86abb711-c73a-43b4-b776-a4ae93348c3d

📥 Commits

Reviewing files that changed from the base of the PR and between 666d990 and 62fc566.


📒 Files selected for processing (3)
  • native/computer-use-macos/Sources/OrcaComputerUseMacOS/main.swift
  • native/computer-use-macos/Sources/OrcaComputerUseMacOSCore/AgentConnectionRequestLoop.swift
  • native/computer-use-macos/Tests/OrcaComputerUseMacOSTests/AgentConnectionRequestLoopTests.swift

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.



📝 Walkthrough

Walkthrough

AgentConnectionRequestLoop.run reads request lines and handles each inside an autorelease pool. The macOS connection handler uses this loop and signals whether processing should continue. Tests check that autoreleased objects are released before the next request, that the loop handles lines until input ends, and that it stops when the handler returns false.


Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 62fc5

The macOS request loop appears ready to merge after normal checks; no actionable risk remains in the supplied evidence.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: releasing macOS screenshot memory after each request.
Description check Passed The description is complete and focused. It explains the user impact, mechanism, rationale, alternatives, linked issue, testing, platform scope, risks, and remaining validation. It also correctly mark…
Linked Issues check Passed The PR addresses direct issue #27373. SocketListener.handleConnection now uses AgentConnectionRequestLoop.run. The new loop creates an autoreleasepool for each request and stops at end of input …
Out of Scope Changes check Passed The changes stay within issue #27373 scope. They modify only the macOS Computer Use connection loop, add the request-loop implementation, and add focused tests. The diff does not change other platform…

  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker
nwparker merged commit 810c262 into stablyai:main Oct 11, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: macOS Computer Use screenshots pile up in WindowServer memory until Orca restarts

2 participants