[Bug] Commands started by execute_bash keep running after nanocoder exits (closed terminal, timeout, SIGTERM)
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:
- 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.
- The 2 minute timeout, followed by the run finishing. In
nanocoder run, the model is toldError: Command timed out after 120000msand the run exits 0, but a command that ignores SIGTERM is still running. - SIGTERM or SIGINT sent to
nanocoder runwhile a command that ignores SIGTERM is running. nanocoder printsAbortedand 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:
- Start nanocoder in a scratch folder (
--mode yolomakes it quicker). - Ask it to run
while :; do date +%s >> tick.txt; sleep 1; done. - Close the terminal window.
- From another terminal, run
wc -l tick.txta few times, or look for thesleepprocess withps.
Case 2, timeout in run:
nanocoder run --mode yolo "..."where the model runstrap '' TERM; while :; do date +%s >> tick.txt; sleep 1; done.- Wait for the 2 minute timeout. The model gets
Error: Command timed out after 120000msand the run exits 0. - Check
tick.txtagain 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.txtkeeps growing (2 lines at exit, 7 lines five seconds later). Same result in the interactive TUI and inrun --plain. - Case 2: the run exits 0 after about 122 seconds, and
tick.txtgoes from 119 to 124 lines in the 5 seconds after exit. Theshandsleepprocesses 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 isunref()ed (bash-executor.ts:413-420), so it's dropped if the process exits sooner. ShutdownManageronly listens for SIGTERM and SIGINT (source/utils/shutdown/shutdown-manager.ts:124-125). Nothing insource/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 theunref()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.
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.