nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Cancelling or timing out execute_bash on Windows leaves the command's child process running

Open addyCooks opened this issue 1 month ago • 2 comments

Description

On Windows, BashExecutor runs commands as cmd /c <command>. When a command is cancelled or times out, killProcessTree (source/services/bash-executor.ts, win32 branch around lines 354-366) only calls proc.kill() on that cmd.exe wrapper. The real program that cmd.exe started keeps running. The tool reports "cancelled" or "timed out" and the agent moves on, but the process is still alive, so a dev server that hits the 2-minute default timeout keeps holding its port.

On Linux and macOS this is handled with a process group (see the tests around bash-executor.spec.ts:581). Those tests are skipped on Windows ("no process groups"), so nothing covers it.

Environment

  • OS: Windows 11 (10.0.26300)
  • Node version: 22.22.3
  • Nanocoder version: 1.31.0 (main at 5c2e693)
  • Provider / Model: not relevant (reproduced by calling the executor directly)

Steps to Reproduce

  1. On Windows, create sleeper.js containing setInterval(() => {}, 1000);.
  2. Run node sleeper.js MARKER1 through bashExecutor.execute(...) with an AbortSignal.
  3. Wait 2 seconds, then abort the signal.
  4. Wait 4 seconds and look for the process: Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like '*MARKER1*' -and $_.Name -eq 'node.exe' }.
  5. Repeat with {timeoutMs: 1500} instead of the signal.

Expected Behavior

The whole process tree is stopped, so no node.exe is left.

Actual Behavior

The tool result says Cancelled via AbortSignal (or Command timed out after 1500ms), but 4 seconds later the node.exe is still running:

[abort] 4s after cancel: node processes still running = ["38172"]
[timeout] 4s after timeout: node processes still running = ["18456"]

Suggested Fix

  • Use the same approach as source/services/lifecycle-hooks.ts (the killProcessTree around lines 388-412), which already runs taskkill /pid <pid> /T /F on Windows. I checked that taskkill /T /F on the cmd.exe pid stops the child that the current code leaves behind.
  • Best is to move that helper into a shared util and use it in bash-executor.ts.
  • Add a Windows-only test (cancel by AbortSignal and by timeout, then check that no process is left), and add bash-executor.spec.ts to the Windows CI job.
  • source/custom-tools/handler.ts:176-205 has the same gap and documents it as a known limitation. It could use the same helper.

Additional Context

  • Closed #541 listed taskkill /T /F for Windows in its suggested fix, but the merged fix (#547) only covers POSIX process groups.
  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

addyCooks avatar Oct 08 '26 17:10 addyCooks

@addyCooks can i go for it

Dhirenderchoudhary avatar Oct 09 '26 10:10 Dhirenderchoudhary

Go for it @Dhirenderchoudhary !

addyCooks avatar Oct 09 '26 10:10 addyCooks