Skip to content

Fix Windows terminal daemon death after workspace close - #8140

Merged
Jinwoo-H merged 2 commits into
mainfrom
Jinwoo-H/windows-terminal-daemon-dies-after-closing-a-wor
Jul 10, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
Jinwoo-H/windows-terminal-daemon-dies-after-closing-a-wor

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Prevent overlapping graceful and forced workspace teardown from closing the same Windows ConPTY native handle twice and terminating the shared daemon.
  • Preserve cold-restored terminal history by seeding the fresh daemon emulator before its first persistence re-anchor.
  • Add deterministic wrapper regressions and a built-daemon Windows harness that runs 25 real ConPTY close races while an unrelated witness PTY remains alive.

Fixes #8048.

Important

This PR bumps the daemon protocol from 18 to 19 because recovered history is now carried atomically in createOrAttach. A parallel PR also bumps the daemon protocol, so the final merge order must reconcile this number and retain 18 in the previous-version list.

Screenshots

No visual change. Electron validation screenshots were inspected locally and deliberately not committed.

Testing

  • pnpm lint — focused lint passed, but the repository-wide command stops on an unrelated existing ja.json interpolation mismatch for components.native-chat.composer.uploadingAttachments; this PR does not touch localization files.
  • pnpm typecheck — pnpm typecheck:node passed; CLI/web typechecks were not run because this change is main-process/daemon-only.
  • pnpm test — focused daemon suite: 294 passed, 12 platform skips; workflow contract: 12 passed.
  • pnpm build — pnpm build:electron-vite passed.
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Additional validation:

  • node config/scripts/windows-daemon-workspace-close-repro.mjs: all 25 victim sessions and OS PIDs were reaped while the built daemon and witness PowerShell survived the concurrent graceful/immediate shutdown races.
  • Isolated Electron v19 profile: entered a marker through the visible PowerShell terminal, terminated only its daemon, reloaded, and confirmed the marker remained visible in scrollback and in the post-reanchor checkpoint.
  • pnpm check:reliability-gates, pnpm check:max-lines-ratchet, focused oxlint, and git diff --check passed.

AI Review Report

Reviewed both reported symptoms independently. The daemon-death path was traced from renderer graceful shutdown plus main-process forced sweep to a second Windows node-pty close. The history path was traced through cold restore to the fresh daemon's first checkpoint overwriting the recovery source.

The review checked:

  • Windows ConPTY cleanup ordering and PID-reuse safety.
  • Failure behavior when recovered history cannot be seeded.
  • Protocol compatibility and old-daemon rejection.
  • Checkpoint/log generation behavior after recovery.
  • Cross-platform scope: the kill behavior is gated by process.platform === 'win32'; filesystem paths use Node path APIs; no shortcuts, labels, or UI behavior changed. Shared daemon tests pass, but live macOS/Linux/SSH/WSL sessions were not exercised.

A final review caught formatter expansion pushing types.ts over the max-lines limit; the duplicated seed payload now reuses the existing write request shape, and focused lint/typecheck passed afterward.

Follow-up review found and fixed three issues before merge:

  • Recovery was previously seeded by a second RPC, allowing startup prompt bytes to win the race. Recovery now enters Session construction before subprocess listeners are registered.
  • Seed failure suspension previously lived only in the adapter. The daemon session now retains historySeeded: false, so a later adapter cannot promote a fresh-only snapshot.
  • The Windows harness previously proved daemon/witness survival but not victim cleanup. It now waits for every victim session and OS PID to disappear.

Both history findings were reproduced as deterministic red tests before the redesign and pass afterward.

Security Audit

  • No dependencies, network APIs, authentication behavior, or secrets were added.
  • The recovered-history field uses the existing authenticated local daemon createOrAttach request and accepts history already read from Orca's local persistence directory.
  • The Windows harness uses a unique named pipe, random token/session IDs, an isolated temporary directory, hidden child windows, request timeouts, and cleanup in finally.
  • The harness passes paths as process arguments without shell interpolation.

Notes

  • Known merge conflict: coordinate the daemon protocol version with the parallel protocol-bump PR. Do not drop the atomic recovered-history createOrAttach wire change or allow an older daemon to accept the new client silently.
  • A protocol transition means pre-upgrade daemon sessions cannot seamlessly attach to the new protocol instance; recovered history is retained by this fix.
  • Cold restore now parses recovered ANSI once more in the daemon. This occurs only during recovery, not on the terminal input hot path.
  • If seeding fails, persistence is suspended for that session rather than overwriting the last known-good history.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The daemon protocol adds history seeding for recovered scrollback. Cold restore conditionally re-anchors history or suspends the session when seeding fails, with integration coverage for recovered output and restart persistence. Windows PTY teardown avoids duplicate ConPTY closure. A Windows named-pipe harness repeatedly tests concurrent workspace-close kills, and CI workflow tests and reliability-gate metadata cover its execution.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #8048 by fixing Windows ConPTY teardown and preserving recovered scrollback after daemon restart.
Out of Scope Changes check ✅ Passed The workflow, tests, harness, and daemon protocol updates are directly tied to the reported Windows teardown and history recovery.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly describes the main Windows daemon workspace-close fix.
Description check ✅ Passed The PR description covers the required sections and includes testing, review, security, screenshots, and notes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/daemon/pty-subprocess.ts (1)

1081-1112: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Mark the Windows SIGKILL path as issued before dispose() runs

forceKill() should set nodePtyKillIssued after the successful process.kill(proc.pid, 'SIGKILL') path too. forceKillAndDisposeSubprocess() calls dispose() immediately afterward on Windows, so leaving the flag unset still lets destroy() run and double-close ConPTY.

🧹 Nitpick comments (1)
src/main/daemon/pty-subprocess.test.ts (1)

2375-2399: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Good regression test — consider also covering the reverse ordering.

This locks in "graceful kill → forceKill → dispose" on Windows. Given the asymmetry noted in pty-subprocess.ts (forceKill's primary process.kill(pid, 'SIGKILL') success path doesn't set nodePtyKillIssued), consider adding a companion case where forceKill() is called first (succeeding via the raw process.kill path) followed by kill(), to prove the second node-pty kill()/destroy() invocation is also prevented.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7b6ff6c3-0295-469d-b7e3-16113bf6f698

📥 Commits

Reviewing files that changed from the base of the PR and between 1afec6c and 3c8412c.

📒 Files selected for processing (13)
  • .github/workflows/computer-e2e.yml
  • config/reliability-gates.jsonc
  • config/scripts/computer-e2e-workflow.test.mjs
  • config/scripts/windows-daemon-workspace-close-repro.mjs
  • src/main/daemon/daemon-pty-adapter.test.ts
  • src/main/daemon/daemon-pty-adapter.ts
  • src/main/daemon/daemon-server.ts
  • src/main/daemon/history-manager.ts
  • src/main/daemon/pty-subprocess.test.ts
  • src/main/daemon/pty-subprocess.ts
  • src/main/daemon/session.ts
  • src/main/daemon/terminal-host.ts
  • src/main/daemon/types.ts

@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

Follow-up review findings addressed in 1710325.

The two history concerns were valid and reproduced before the fix:

  • startup output preceded recovered history in the first re-anchor checkpoint;
  • a simulated seed failure followed by adapter restart overwrote the known-good recovery source.

Recovery is now part of createOrAttach and is written into the daemon emulator during Session construction, before subprocess output listeners are registered. The daemon session retains the seed result across adapter restarts, so a failed seed remains non-authoritative. The probe/create race replaces its briefly-created unseeded shell before exposing the PTY.

The Windows harness now also requires every victim session and OS PID to be reaped. The rebuilt daemon passed all 25 iterations while keeping the daemon PID and witness PTY alive.

Validation: 294 focused daemon tests passed (12 platform skips), workflow contract 12/12, node typecheck, focused lint, reliability/max-lines checks, production build, and strengthened native harness.

@Jinwoo-H
Jinwoo-H merged commit 03a6737 into main Jul 10, 2026
7 checks passed
@Jinwoo-H
Jinwoo-H deleted the Jinwoo-H/windows-terminal-daemon-dies-after-closing-a-wor branch July 10, 2026 20:50
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 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.

Windows: terminal daemon dies after closing a workspace, killing all PTY sessions (+ terminal-history files are all 1 byte)

1 participant