fix(shell): bound bash-tool hangs via scope teardown instead of multiple timeouts#35245
Open
Levosilimo wants to merge 1 commit into
Open
fix(shell): bound bash-tool hangs via scope teardown instead of multiple timeouts#35245Levosilimo wants to merge 1 commit into
Levosilimo wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #25664
Type of change
What does this PR do?
The
bashtool waited on Node'scloseevent for the spawned subprocess. When that process forks grandchildren that inherit stdio,closenever fires — and the tool hangs indefinitely.V2 (the core bash tool) wraps the read loop in
Effect.scopedand bounds it withEffect.timeoutOrElse. V1 still had the V0 raceAll/handle.kill shape.This PR mirrors V2 in V1:
Effect.scoped(handle.exitCode)Effect.timeoutOrElse(duration, ...)so the wait can't run past the configured timeoutEffect.raceFirst(waitForAbort(ctx.abort))so a user-cancelled abort propagates as a failure instead of being silently mapped to a fake exitThe spawner's
acquireReleasefinalizer already sendsSIGTERMon scope cleanup. WhenEffect.timeoutOrElsecancels, theDeferred.awaitis interrupted cleanly. Pipes free, error propagates, bug-shape can't occur.The two previous symptom fixes in this code path (
Effect.catchfail-open onhandle.exitCode, explicithandle.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 -fto 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.tsfrompackages/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-linenodescript that doesspawn(grandchild, {stdio: "inherit"}); g.unref(); process.exit(0), runs it through the bash tool withtimeout: 1500, asserts the tool returns within 5000ms withexitnull or 0.propagates signal-kill errors instead of failing open— runskill -TERM $$, asserts the underlying Effect rejects (the previousEffect.catchwould have silently mapped this tocode: null).All 23 pre-existing shell tests still pass.
Screenshots / recordings
N/A.
Checklist