Skip to content

mac: stop leaking flashtex-project-files helpers, one per test (#687) - #707

Merged
d-q222 merged 1 commit into
mainfrom
agent/daniel-parent/mac-helper-leak
Sep 17, 2026
Merged

d-q222 merged 1 commit into
mainfrom
agent/daniel-parent/mac-helper-leak

Conversation

@d-q222

@d-q222 d-q222 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

What

LineProcessClient (shared by the flashtex-project-files, flashtex-bridge,
and flashtex-edit-ledger helper clients) and PreviewControllerClient now
terminate and reap their child process in deinit, not only when a caller
remembers to call terminate()/detachHelper()/detachController().

Why

Root cause: no object in the DocumentFilesState -> ProjectFilesClient -> LineProcessClient chain ever tore down the spawned Process/pipes on its
own. Most real-helper tests in DocumentFilesTests.swift never call
detachHelper()/terminate() before returning, so each left its helper
running (its stdin pipe still held open by the parent) as an orphan of the
xctest process until the whole test binary exited — exactly the "one per
test, reaped never [until run end]" behavior in #687.

Fix: tie the helper's lifetime to the Swift object's lifetime instead of to
every call site remembering cleanup. PreviewControllerClient gets the same
treatment since #687 flagged it as the same launch pattern; its existing call
sites already call terminate() explicitly, so this is a safety net, not a
behavior change for them.

Overlap check

Test results

Before the fix, a temporary repro (spawn 15 real helpers, ping() each to
confirm they are live, drop all references, no terminate() call — the same
thing the real tests do):

REPRO_AT_SPAWN=15
REPRO_AFTER_DROP=15

15 helpers stayed alive after every Swift reference was dropped: the leak.

After the fix, identical repro:

REPRO_AT_SPAWN=15
REPRO_AFTER_DROP=0

All 15 are killed and reaped within the poll window, with no explicit
teardown call anywhere in the repro.

Regression suite runs on the fixed code:

swift test --filter DocumentFilesTests
Executed 14 tests, with 0 failures (0 unexpected) in ~13s

(includes the new testDroppedClientKillsAndReapsItsHelperWithoutExplicitTeardown)

swift test --filter 'BridgeClientTests|BridgeRecoveryTests|RealEditLedgerTests|DirtySnapshotsTests'
Executed 32 tests, with 3 tests skipped (missing optional real-binary env vars, pre-existing) and 0 failures

Not done

🤖 Generated with Claude Code

https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz

What changed:
- LineProcessClient (shared by the project-files, bridge, and edit-ledger
  helper clients) now terminates and reaps its child process in `deinit`,
  not only when a caller remembers to call `terminate()`. Root cause: no
  object in the DocumentFilesState -> ProjectFilesClient -> LineProcessClient
  chain ever tore down the spawned Process/pipes on its own, and most
  real-helper tests in DocumentFilesTests.swift never called
  detachHelper()/terminate() before returning, so each left its helper
  running (stdin pipe still open) as an orphan of the xctest process until
  the whole test binary exited.
- PreviewControllerClient gets the same deinit safety net (same
  Process/Pipe launch pattern; flagged in #687 as worth auditing).
- Added a regression test: dropping a ProjectFilesClient with no explicit
  terminate()/detachHelper() call must still kill and reap its helper.

What was run (literal lines):
- Before the fix, a temporary repro (spawn 15 real helpers, ping each,
  drop all references, no terminate()): REPRO_AT_SPAWN=15,
  REPRO_AFTER_DROP=15 -- confirms the leak.
- After the fix, same repro: REPRO_AT_SPAWN=15, REPRO_AFTER_DROP=0.
- `swift test --filter DocumentFilesTests` (14 tests incl. the new
  regression test): Executed 14 tests, with 0 failures.
- `swift test --filter 'BridgeClientTests|BridgeRecoveryTests|RealEditLedgerTests|DirtySnapshotsTests'`:
  Executed 32 tests, with 3 tests skipped (missing optional real-binary
  env vars, pre-existing) and 0 failures.

Next step: none; #687 is addressed. Not touched: flashtex-preview-controller's
other call sites already call terminate() explicitly (DocumentKindsTests.swift,
V2ConformanceTests.swift) -- the new deinit is a safety net, not a behavior
change for them.

Implementation-Agent: claude-sonnet-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222
d-q222 merged commit ba74630 into main Sep 17, 2026
20 of 21 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.

1 participant