Skip to content

fix(pty): kill orphaned PTY when TerminalProcess constructor throws#3745

Merged
gregpriday merged 3 commits intodevelopfrom
feature/issue-3688-pty-process-leaks-when
Mar 19, 2026
Merged

fix(pty): kill orphaned PTY when TerminalProcess constructor throws#3745
gregpriday merged 3 commits intodevelopfrom
feature/issue-3688-pty-process-leaks-when

Conversation

@gregpriday
Copy link
Copy Markdown
Collaborator

Summary

  • Wraps new TerminalProcess(...) in PtyManager.spawn() with try/catch that kills the raw PTY process if the constructor throws after spawning
  • Extracts PTY spawning into a standalone spawnPtyProcess() function so the raw process handle is available for cleanup before it gets buried inside the constructor
  • Adds dedicated test suite for spawn failure scenarios covering both pool-acquired and freshly-spawned PTY cleanup

Resolves #3688

Changes

  • electron/services/PtyManager.ts — try/catch around TerminalProcess construction; kills raw PTY on failure and logs context
  • electron/services/pty/terminalSpawn.ts — new spawnPtyProcess() extracted from TerminalProcess, accepts raw pty process as parameter
  • electron/services/pty/TerminalProcess.ts — constructor now receives a pre-spawned PTY process instead of spawning internally
  • electron/services/pty/__tests__/PtyManager.spawnFailure.test.ts — new test suite for orphan prevention
  • Updated existing TerminalProcess test files to use class-based mocks matching the new constructor signature

Testing

  • All new and existing tests pass
  • Typecheck, lint, and format checks pass cleanly

@gregpriday
Copy link
Copy Markdown
Collaborator Author

Review status: Rebased and ready

- Extract computeSpawnContext() helper into terminalSpawn.ts
- Move acquirePtyProcess() call from TerminalProcess constructor to PtyManager.spawn()
- Pass pre-acquired ptyProcess and SpawnContext into TerminalProcess constructor
- Wrap constructor call in try/catch that kills the PTY on failure
- Add regression tests for spawn failure cleanup
- Update all existing TerminalProcess tests for new constructor signature
…tests

- Mock via barrel module ../pty/index.js instead of direct file
- Use class syntax so mock works as constructor
- Assert ptyProcess.kill() called in kill-also-throws test case
- Add test for acquirePtyProcess throwing before constructor
@gregpriday gregpriday force-pushed the feature/issue-3688-pty-process-leaks-when branch from 0b5d55a to 11f5a69 Compare March 19, 2026 03:28
@gregpriday gregpriday merged commit 0f55acf into develop Mar 19, 2026
0 of 3 checks passed
@gregpriday gregpriday deleted the feature/issue-3688-pty-process-leaks-when branch March 19, 2026 03:29
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.

PTY process leaks when TerminalProcess constructor throws

1 participant