Skip to content

fix(shell): bound bash-tool hangs via scope teardown instead of multiple timeouts#35245

Open
Levosilimo wants to merge 1 commit into
anomalyco:devfrom
Levosilimo:feat/shell-hang-guard
Open

fix(shell): bound bash-tool hangs via scope teardown instead of multiple timeouts#35245
Levosilimo wants to merge 1 commit into
anomalyco:devfrom
Levosilimo:feat/shell-hang-guard

Conversation

@Levosilimo

@Levosilimo Levosilimo commented Jul 3, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #25664

Type of change

  • Bug fix

What does this PR do?

The bash tool waited on Node's close event for the spawned subprocess. When that process forks grandchildren that inherit stdio, close never fires — and the tool hangs indefinitely.

V2 (the core bash tool) wraps the read loop in Effect.scoped and bounds it with Effect.timeoutOrElse. V1 still had the V0 raceAll/handle.kill shape.

This PR mirrors V2 in V1:

  • wrap the whole read loop in Effect.scoped(handle.exitCode)
  • bound it with Effect.timeoutOrElse(duration, ...) so the wait can't run past the configured timeout
  • race against abort via Effect.raceFirst(waitForAbort(ctx.abort)) so a user-cancelled abort propagates as a failure instead of being silently mapped to a fake exit

The spawner's acquireRelease finalizer already sends SIGTERM on scope cleanup. When Effect.timeoutOrElse cancels, the Deferred.await is interrupted cleanly. Pipes free, error propagates, bug-shape can't occur.

The two previous symptom fixes in this code path (Effect.catch fail-open on handle.exitCode, explicit handle.kill({forceKillAfter}) after each branch) are no longer needed and are dropped.

I also created a companion plugin at https://github.com/Levosilimo/opencode-shell-hang-guard that rewrites pkill -f to a basename-guarded variant. Useful until this PR ships, not needed afterward.

How did you verify your code works?

bun test test/tool/shell.test.ts from packages/opencode: 25 pass, 0 fail.

Two new regression tests in describe("tool.shell hangs", ...):

  • returns when spawned process forks a grandchild that inherits stdio — writes a 5-line node script that does spawn(grandchild, {stdio: "inherit"}); g.unref(); process.exit(0), runs it through the bash tool with timeout: 1500, asserts the tool returns within 5000ms with exit null or 0.
  • propagates signal-kill errors instead of failing open — runs kill -TERM $$, asserts the underlying Effect rejects (the previous Effect.catch would have silently mapped this to code: null).

All 23 pre-existing shell tests still pass.

Screenshots / recordings

N/A.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…ple timeouts

The `bash` tool waits on Node's `close` event to resolve its exit
Deferred. When a spawned process forks grandchildren that inherit
stdio, `close` never fires. The current code bounds this with three
timeouts (raceAll against a sleep; Effect.catch fail-open; explicit
kill after each branch) layered on top of the same unfixed root
cause.

Match the V2 AppProcess shape: cancel the whole read via
Effect.timeoutOrElse around Effect.scoped(handle.exitCode), wrap in
Effect.raceFirst(waitForAbort) so signal-kill propagates instead of
silently returning a fake exit. The spawner's acquireRelease finalizer
sends SIGTERM on scope cleanup; the Deferred.await it is waiting on
is interrupted by the cancellation. Pipes free, error propagates.

Two tests in test/tool/shell.test.ts:
- orphan grandchild with inherited stdio returns within the timeout
- signal-kill (`kill -TERM $$`) propagates as a failure, not a fake
  clean exit

The previous spawner-side fixes (`resolve on exit`, `5s bound on
post-SIGKILL await`) are not needed when the cancellation cascade
above takes over; both reverted.
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.

pkill -f command causes tool call hang in opencode TUI

1 participant