Skip to content

fix(tools): stop cmd.exe when a Windows PTY Bash call is cancelled - #486

Merged
BunsDev merged 6 commits into
mainfrom
fix/373-pty-windows-kill-on-drop
Nov 16, 2026
Merged

BunsDev merged 6 commits into
mainfrom
fix/373-pty-windows-kill-on-drop

Conversation

@BunsDev

@BunsDev BunsDev commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • On Windows, cancelling a foreground call through the PTY Bash tool, which is Windows' default Bash tool, now stops the cmd.exe it started. Its cmd /C fallback lacked kill_on_drop, so a dropped call left cmd.exe running. The plain Bash tool's fallback already set it, and both already kill cmd.exe on timeout.
  • The PowerShell tool had the same gap on every platform. A cancelled call left powershell (or pwsh) running to finish the command, for example a Remove-Item after a Start-Sleep. It now stops with the call.
  • Adds the first Rust test job on Windows in CI. It runs foreground_windows.rs against all three shells, and both fixes failed first there, as shown below.

Context

  • Related issue/user request: Complete foreground shell descendant cancellation and Windows cleanup #373 (foreground shell descendant cancellation and Windows cleanup). This is the part that does not wait on the decision about tree cleanup.
  • Scope:
    • crates/tools/src/pty_bash.rs and crates/tools/src/powershell.rs: one kill_on_drop(true) each, with a comment;
    • crates/tools/tests/foreground_windows.rs;
    • a windows-latest job in .github/workflows/rust-ci.yml.
  • Non-goals: killing the whole process tree. Grandchildren of cmd.exe or powershell still outlive a cancel or timeout. That needs the Job Object or process-group decision recorded in Complete foreground shell descendant cancellation and Windows cleanup #373; this test file is where its failing case would go.

Changes

  • run_windows_fallback (PTY Bash) and the PowerShell tool spawn their shell with kill_on_drop(true), like BashTool::execute_windows.
  • foreground_windows.rs (#![cfg(windows)]) runs a loop that only the shell itself keeps going, appending a line to a file:
    • cmd.exe runs a builtin for /L, waiting in ping;
    • powershell runs a for loop with Add-Content, waiting in Start-Sleep.
  • For each tool, the test checks that the file stops growing after a cancel (aborting the call) and after a timeout. For the two Bash tools, it also checks that a finished command returns its stdout and stderr.
  • The CI job uses Rust 1.100.0, the version this repo's gates use and ci: pin the Rust toolchain to 1.100.0 in CI and release builds #474 pins. It has its own cache key and a 20-minute limit.

Validation

  • Failing first, on hosted Windows:
    • Run 43607659461 on f5c5b379 (tests only): 5 passed and cancelled_pty_fallback_stops_its_cmd failed. Three seconds after the cancel, the tick count had gone from 4 to 7.
    • Run 43608772300 on 926e9ddd (PowerShell tests, before that fix): 7 passed and cancelled_powershell_stops_its_process failed, with 5 ticks, then 8.
    • Run 43608132146 on c0671480 (the PTY fix): 6 of 6 passed.
  • Hosted CI on this head (1357470): 15 of 15 checks passed. In Rust CI run 43611746750, the Windows job passed all 8 tests in foreground_windows.rs. It passed 8 of 8 on a651089 too, in run 43610074288.
  • Local on macOS, Rust 1.100.0, head a651089 (the last commit only lengthens a Windows-only test timeout), from src-rust/: cargo fmt --all -- --check clean; cargo clippy --workspace --all-targets --locked -- -D warnings clean on 1.100.0 and 1.99.1; cargo clippy --package claurst-tools --all-targets --features computer-use --locked -- -D warnings clean; cargo test --workspace --locked --no-fail-fast (scratch HOME): 3253 passed, 0 failed, 66 ignored.
    • The PTY fix and the new tests are Windows-only, so macOS shows only that nothing else broke. The PowerShell change compiles on every platform.
  • actionlint .github/workflows/rust-ci.yml: clean. git diff --check: clean.

An independent review checked the cmd quoting, the redirect, ping with null stdin, reads that collide with the shell's writes, and the timing. It found the PowerShell gap and a tight timeout on a cold powershell.exe start; both are fixed. It found nothing above a nit at the final head.

Known limits and follow-ups

  • Grandchildren of either shell are not reached (Complete foreground shell descendant cancellation and Windows cleanup #373's decision).
  • Two other foreground spawns drop a child without stopping it:
    • EnterWorktree's post_create_command (crates/tools/src/worktree.rs, cmd /C … .output()) has no kill_on_drop and no timeout.
    • The formatter's 30 s timeout (crates/tools/src/formatter.rs) leaves a hung formatter running, on every OS.
    • They are hooks rather than shell tools, so they are left for a follow-up.
  • Existing and not changed here: both cmd /C fallbacks pass the command through Rust's Windows argument quoting. That writes an inner " as \", which cmd.exe does not unescape, so a command such as git commit -m "fix bug" arrives mangled. The test uses no quotes.
  • This job's test file compiles only on Windows, so a change to ToolContext breaks it only here. Making the job a required check would catch that.

PR Readiness

  • Diff is limited to the intended files.
  • Generated files were not edited by hand.
  • User-facing docs/help were updated or are not needed. Not needed: cancelling now does what it already did on timeout.
  • No secrets, credentials, local paths, or unrelated logs are included.
  • Remaining risks or follow-up work are listed above.

…n CI

Both foreground Bash tools fall back to `cmd /C` on Windows, and no CI
job ran Rust tests there. A new windows-latest job runs
foreground_windows.rs: a loop only cmd.exe keeps going is cancelled,
timed out and left to finish, through each tool. A cancelled call must
stop its cmd.exe the way a timed-out call already does.

Refs #373
@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Nov 16, 2026 4:28am UTC

Request Review

The PTY Bash tool, the default Bash tool on Windows, falls back to
`cmd /C` there without kill_on_drop, so dropping a cancelled call left
cmd.exe running. BashTool's fallback already set it, and both kill
cmd.exe on timeout. Children of cmd.exe still outlive a cancel or a
timeout until #373's tree-cleanup decision.

Refs #373
The PowerShell tool is a foreground shell too, and it spawns
powershell without kill_on_drop. Its loop runs inside powershell.exe
itself, so the file stops growing once that process is gone. The
Windows job's limit drops to 20 minutes; a cold build took about 2.

Refs #373
The PowerShell tool spawned powershell (pwsh off Windows) without
kill_on_drop, so dropping a cancelled call left the shell running and
able to finish the command. It now stops with the call, as it already
did on timeout. Children of the shell are still not reached (#373).

Refs #373
The timeout cases need one tick before the call times out. At 2.5 s a
cold powershell.exe on a busy runner, beside seven other tests, could
miss it; 6 s leaves room.

Refs #373

This branch was successfully deployed

1 active deployment
Preview — 6abc095e Deployed Nov 16, 2026 by vercel[bot]
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