nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Commands started by execute_bash keep running after nanocoder exits (closed terminal, timeout, SIGTERM)

Open diyaavirmani opened this issue 1 month ago • 1 comments

Description

When nanocoder exits while execute_bash is running a command, the command can keep running in the background and keep writing to the project. I found three ways this happens on macOS:

  1. Closing the terminal (or losing an SSH session). nanocoder gets SIGHUP and exits, but the command keeps running. This happens with any command, for example a dev server or a watch loop.
  2. The 2 minute timeout, followed by the run finishing. In nanocoder run, the model is told Error: Command timed out after 120000ms and the run exits 0, but a command that ignores SIGTERM is still running.
  3. SIGTERM or SIGINT sent to nanocoder run while a command that ignores SIGTERM is running. nanocoder prints Aborted and exits 0, and the command keeps going.

#1657 covers the Windows side of this and mentions that Linux and macOS are handled through process groups. That holds while nanocoder stays alive, but the cleanup gets lost when nanocoder itself exits first.

Environment

  • OS: macOS (Darwin 25.6.0, x86_64)
  • Node version: 22.23.3
  • Nanocoder version: main at 0925b881, and also v1.31.0
  • Provider: a local OpenAI-compatible test server with scripted responses
  • Model: n/a (scripted)

Steps to Reproduce

Case 1, closing the terminal:

  1. Start nanocoder in a scratch folder (--mode yolo makes it quicker).
  2. Ask it to run while :; do date +%s >> tick.txt; sleep 1; done.
  3. Close the terminal window.
  4. From another terminal, run wc -l tick.txt a few times, or look for the sleep process with ps.

Case 2, timeout in run:

  1. nanocoder run --mode yolo "..." where the model runs trap '' TERM; while :; do date +%s >> tick.txt; sleep 1; done.
  2. Wait for the 2 minute timeout. The model gets Error: Command timed out after 120000ms and the run exits 0.
  3. Check tick.txt again after the run has exited.

Case 3: same command as case 2, then send kill -TERM <nanocoder pid> (or kill -INT) while it runs.

Expected Behavior

Once nanocoder reports a command as timed out or aborted, or exits, nothing it started should still be running.

Actual Behavior

  • Case 1: nanocoder exits, and tick.txt keeps growing (2 lines at exit, 7 lines five seconds later). Same result in the interactive TUI and in run --plain.
  • Case 2: the run exits 0 after about 122 seconds, and tick.txt goes from 119 to 124 lines in the 5 seconds after exit. The sh and sleep processes are still alive with PPID 1.
  • Case 3: nanocoder prints Aborted, exits 0, and the loop keeps writing.

Two controls point at the cause:

  • If the next model turn after the timeout is slowed down by 5 seconds, nanocoder is still alive when its 2 second SIGKILL fallback fires, and the command does stop.
  • SIGTERM on a normal command (one that doesn't ignore SIGTERM) stops it as expected.

Logs/Screenshots

What I think is going on:

  • The command is spawned with detached: true (source/services/bash-executor.ts:154). It gets its own session, so it never receives the terminal's SIGHUP.
  • Cancel and timeout send SIGTERM (bash-executor.ts:385). The SIGKILL fallback is a 2 second timer that is unref()ed (bash-executor.ts:413-420), so it's dropped if the process exits sooner.
  • ShutdownManager only listens for SIGTERM and SIGINT (source/utils/shutdown/shutdown-manager.ts:124-125). Nothing in source/ handles SIGHUP.

Additional Context

Proposed fix (happy to adjust):

  • In the shutdown path, stop any command groups that are still running before process.exit: SIGTERM, a short grace period, then SIGKILL, instead of relying on the unref()ed timer.
  • Treat SIGHUP the same way as SIGTERM.
  • Only report "timed out" or "cancelled" once the process group is actually gone.

One open question: should background jobs the agent started on purpose (cmd &) also be stopped on exit? They're in the same process group.

For tests, I'd add a spec that starts a loop that ignores SIGTERM and checks the group is gone after a timeout, a cancel, and a shutdown (SIGTERM and SIGHUP).

  • [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 could you take a look when you have a moment? I'd like to work on this one, so please assign it to me if that works for you.

diyaavirmani avatar Oct 18 '26 10:10 diyaavirmani

Thanks @diyaavirmani, your analysis matches the code. The group is spawned detached, the SIGKILL fallback timer is unref()ed so it is lost if we exit within 2s, and ShutdownManager handles only SIGTERM/SIGINT and never stops active bash executions.

Your plan sounds right: stop active groups on shutdown (SIGTERM, short grace, SIGKILL), treat SIGHUP like SIGTERM, and report "timed out"/"cancelled" only once the group is gone. I'd also stop cmd & jobs on exit. Please add POSIX-only specs for timeout, cancel and shutdown.

Go ahead and open a PR. I'll assign this to you.

addyCooks avatar Oct 19 '26 07:10 addyCooks