Skip to content

fix(desktop): wait for buffered stdout when login shell exits early - #29

Open
yuearc wants to merge 1 commit into
w01fb0ss:mainfrom
yuearc:fix/login-shell-path-stdout-drain
Open

yuearc wants to merge 1 commit into
w01fb0ss:mainfrom
yuearc:fix/login-shell-path-stdout-drain

Conversation

@yuearc

@yuearc yuearc commented Nov 28, 2026

Copy link
Copy Markdown

What and why

Cause

readLoginShellPath() in apps/desktop/src/main/brain/login-shell-path.ts previously parsed output synchronously inside child.once("exit"). In Node.js, "exit" can fire as soon as the child process terminates, before child.stdout finishes emitting its last buffered "data" chunk. When the closing marker (__PIBOT_PATH_END__) arrives in a trailing chunk after "exit", the regex match fails and the function silently returns undefined (login_shell_path_unavailable).

Changes

  • Extract collectLoginShellPath() (keeping readLoginShellPath(platform, env) unchanged) with an idempotent settlement state machine:
    • If exit === 0 and __PIBOT_PATH_END__ has already been buffered, parse and resolve immediately (preserving fast return when detached shell helpers such as gitstatusd inherit and hold stdout open).
    • If exit === 0 arrives before the closing marker, continue waiting for remaining stdout "data" / "end" / "close" events (bounded by TIMEOUT_MS).
    • Release all child and stdout event listeners on settlement and destroy stdout if settled before stream end.
    • Ensure maxOutput overflow kills the process group and settles at most once.
  • Add offline unit tests in apps/desktop/src/main/brain/login-shell-path.test.ts reproducing the exit-before-stdout race (including a closing marker split across chunks), immediate resolution with an unclosed helper pipe, marker-less drain, maxOutput single-kill settlement, and win32 no-op.

Local verification

  • biome@2.5.15 check on both files: passed (0 diagnostics).
  • typescript@7.0.2 (strict, noUncheckedIndexedAccess, verbatimModuleSyntax, declaration: true) against @types/node@26.6.4 and vitest@5.0.3: passed.
  • vitest@5.0.3 run on login-shell-path.test.ts: 5/5 passed.
  • node tools/dev/run-as-node-check.ts: passed.
  • Smoke-tested readLoginShellPath(process.platform, process.env) on macOS /bin/zsh.

Checks

  • One topic; larger changes were agreed in an issue first
  • pnpm verify passes locally (CI runs pnpm check on macOS, Windows and Linux; ran targeted Biome, TypeScript 7 strict declaration check, and Vitest 5 locally)
  • New UI text in Chinese and English; colours from theme tokens (N/A — no UI changes)
  • No secrets, .env files or local paths
  • Every commit is signed off (git commit -s, see CONTRIBUTING.md)

In readLoginShellPath(), parsing output inside child.once('exit') races with Node's stdout pipe flush: 'exit' can fire before the final stdout chunk carrying __PIBOT_PATH_END__ is delivered, silently returning undefined and dropping the user's login-shell PATH.

Extract collectLoginShellPath() to wait for buffered stdout chunks/end/close after exit 0 when the closing marker has not yet arrived, while still resolving immediately once both exit 0 and the closing marker are observed so detached shell helpers (e.g. gitstatusd) holding stdout open do not stall until timeout. Also enforce idempotent settlement, release listeners and destroy stdout on early completion, and add offline Vitest coverage.

Signed-off-by: Yue Arc <135290367+yuearc@users.noreply.github.com>
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