Skip to content

fix(core): resolve spawned process exit before stdio close#30327

Draft
Hona wants to merge 2 commits into
anomalyco:devfrom
Hona:shell-surgical-prototype
Draft

fix(core): resolve spawned process exit before stdio close#30327
Hona wants to merge 2 commits into
anomalyco:devfrom
Hona:shell-surgical-prototype

Conversation

@Hona
Copy link
Copy Markdown
Member

@Hona Hona commented Jun 2, 2026

Summary

  • model spawned-process lifetime separately from inherited stdio lifetime
  • resolve exitCode and isRunning from the foreground process exit event
  • preserve close for graceful process-group termination while avoiding post-escalation waits on shared stdio
  • finalize shell output deterministically at foreground exit with an owned bounded queue
  • coalesce progressive metadata publication without delaying output capture

Why

Node distinguishes process lifetime from stdio lifetime: exit is emitted when the spawned process ends, while close is emitted only after its stdio streams close. Descendants can inherit stdout/stderr and keep those handles open indefinitely after the foreground command exits.

Waiting only for close hangs shell calls. Returning immediately on exit drops bytes already buffered or waiting behind slow persistence and metadata callbacks. Idle-window timers are also incorrect because a quiet inherited pipe can emit more descendant output later.

This PR uses an explicit timer-free boundary: foreground exit stops further inherited output capture, then already accepted chunks drain through an owned bounded queue before the shell result returns.

Domain model

  • ProcessLifecycle.exited: foreground process ended
  • ProcessLifecycle.closed: shared stdio closed
  • terminateProcessGroup: graceful close with the existing opt-in escalation policy
  • reapFailedProcessGroup: best-effort cleanup after a failed foreground process
  • ShellCompletion: tagged Exited, Aborted, and TimedOut outcomes
  • shell output phases: capture, persistence, metadata publication, finalization

Tests

  • bun test test/effect/cross-spawn-spawner.test.ts from packages/core: 29 passed
  • bun test test/tool/shell.test.ts from packages/opencode: 94 passed
  • bun typecheck from packages/core
  • bun typecheck from packages/opencode
  • WSL lifecycle subset covering detached stdio, process-group cleanup, escaped descendants, and invalid commands: 10 passed
  • WSL cancel interrupts shell and resolves cleanly: passed
  • repository-wide bun turbo typecheck via the push hook
  • git diff --check

Notes

This deliberately does not use a fixed post-exit sleep or idle timer. Output emitted by inherited background descendants after foreground exit is outside the shell result boundary.

Copilot AI review requested due to automatic review settings June 2, 2026 04:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the CrossSpawnSpawner implementation in packages/core to distinguish process lifetime (exit) from stdio lifetime (close), so callers can observe exitCode/isRunning without being blocked by stdio staying open (e.g., via a detached descendant inheriting the parent’s stdio).

Changes:

  • Track process exit separately from stdio close by introducing distinct exited and closed deferreds in the spawner.
  • Resolve exitCode and isRunning from the exit event rather than close, while keeping kill waiting on close.
  • Add a regression test covering the “detached descendant keeps inherited stdio open” scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/core/src/cross-spawn-spawner.ts Splits exit vs close tracking; updates exitCode/isRunning to use exit, and kill paths to await close.
packages/core/test/effect/cross-spawn-spawner.test.ts Adds a regression test ensuring exitCode resolves promptly even when stdio remains open due to a detached descendant.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Hona Hona marked this pull request as draft June 2, 2026 04:57
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.

2 participants