Skip to content

fix(ios): close launchctl pipes so the daemon does not leak descriptors - #177

Closed
dmy147 wants to merge 1 commit into
lycorp-jp:mainfrom
dmy147:fix/ios-daemon-launchctl-pipe-leak
Closed

dmy147 wants to merge 1 commit into
lycorp-jp:mainfrom
dmy147:fix/ios-daemon-launchctl-pipe-leak

Conversation

@dmy147

@dmy147 dmy147 commented Oct 24, 2026 •

Copy link
Copy Markdown

Problem

BundleIdentifierResolver.runLaunchctlList creates a stdout and a stderr Pipe for xcrun simctl spawn <udid> launchctl list and never closes their read ends. In a one-shot CLI process that does not matter. The per-simulator daemon is long-lived, though, and it leaks two descriptors on every process-list read:

  • the liveness probe reads the list before every iOS command;
  • ui reuses that snapshot or reads the list itself, and app-state reads it too.

So the leak grows with the number of commands. Once the daemon reaches its descriptor limit, spawning fails. After that, ui returns an empty appPackage and app-state answers "Could not read the running-process list" until the daemon is restarted. I hit this after roughly a thousand commands in one long automation session.

Reproduction

Booted iPhone 17 Pro simulator, iOS 26.5, Xcode 27.1, with Settings in the foreground. Any daemon-routed iOS command works; ui is shown here:

UDID=<booted simulator>
sim-use daemon stop --device "$UDID"
sim-use ui --device "$UDID" >/dev/null           # starts the daemon
PID=$(cat /tmp/sim-use-$(id -u)/$UDID.pid)
lsof -p "$PID" | wc -l
for i in $(seq 1 100); do sim-use ui --device "$UDID" >/dev/null; done
lsof -p "$PID" | wc -l                          # repeat the loop a few times

Measured with a debug build of each revision. Values are lsof -p <daemon pid> | wc -l, with the number of PIPE rows in parentheses:

ui calls main this PR
0 35 (2) 34 (0)
100 236 (202) 34 (0)
200 436 (402) 34 (0)
300 636 (602) 34 (0)

Every ui call adds exactly two pipe descriptors on main.

Fix

Close both read ends with defer, so they are closed on every path: success, non-zero exit and timeout. The runner is factored into runCapturing(executable:arguments:timeout:makePipe:) so it can be tested without a simulator. runLaunchctlList keeps its behaviour and its 5 s timeout.

Tests

  • BundleIdentifierResolverProcessTests runs /bin/echo and a failing /bin/sh -c 'echo oops >&2; exit 3'. Through makePipe it records each read end's descriptor and inode, then checks that neither is still open after the call. The check is per pipe on purpose. A first version counted the process's open descriptors, and that was flaky under make test, because other suites open descriptors in parallel. A descriptor number that another pipe reuses has a different inode, so the reuse is not mistaken for a leak.
  • Without the defer, both cases fail ("read end N is still open"). With it, they pass.
  • make test passes on macOS with Xcode 27.1: 1272 Swift Testing tests and 231 XCTest tests. make e2e-ios passes on an iPhone 17 Pro simulator (iOS 26.5): 29 test runs, 0 failures (6 known issues, as on main).

Side note (not changed here)

stdout is read only after the child exits. Output larger than the pipe buffer (~64 KB) would block the child until the timeout. launchctl list is far below that today, so I left it alone.

BundleIdentifierResolver.runLaunchctlList created a stdout and a stderr
Pipe for `simctl spawn <udid> launchctl list` and never closed their
read ends. The long-lived daemon reads the process list before every
iOS command (liveness probe) and again for `ui` / `app-state`, so it
leaked two descriptors per read until spawning failed and every
process-list read broke until the daemon restarted.

Close both read ends with `defer` on every path. The runner is factored
into runCapturing(executable:arguments:timeout:makePipe:) so a test can
check, pipe by pipe, that both read ends are closed after a successful
and a failing child.

Signed-off-by: DMY <147dmy@gmail.com>
@dmy147 dmy147 closed this Oct 24, 2026
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.

1 participant