`jarvis gui` leaks the Vite dev server, then refuses to start because of its own leak
Component: src/openjarvis/cli/gui_cmd.py, src/openjarvis/cli/daemon_cmd.py (stop)
Severity: graphical mode becomes unusable after the first non-clean exit, with an error that points at the wrong culprit
Environment
| OS | Windows 11 Home Single Language, build 26200 |
| Node | v22 (npm 10), Vite 8.3.0 |
| Commit | 8acafed2 |
Windows is where this bites hardest because of the cmd.exe shim described
below, but the orphaned-child half of it is not Windows-specific.
What happens
Start graphical mode, then close the terminal (or let the machine sleep, or Ctrl-Break, or anything that is not a clean Ctrl-C). Every later run fails:
PS C:\Users\Sagar> jarvis gui --no-browser
Error: Frontend port 5173 is unavailable. Choose another with --frontend-port.Nothing the user can see is using 5173. jarvis stop does not help, a reboot
does. --frontend-port 5174 "works" and leaks another server, so after a few
days there is a row of abandoned Vite processes.
The message is actively misleading: it reads as "something else took your port", so the user goes looking for the other application. The process holding the port is the one OpenJarvis started and failed to clean up.
Root cause
Three separate defects stack up.
1. Only the middle process of the tree is killed
gui starts the frontend with:
process = subprocess.Popen([npm, "run", "dev", "--", ...])
...
finally:
process.terminate()On Windows npm is npm.cmd, so the tree is npm → cmd.exe → node, and
only the node leaf ever binds the port. process.terminate() reaps the
cmd.exe that Popen started and leaves node running, reparented and
invisible. The same shape exists on POSIX via the npm shell wrapper whenever
npm does not forward the signal.
The repository already has the right primitive for this —
openjarvis.core.utils.terminate_process() does taskkill /PID and escalates
to taskkill /F /T /PID, which takes the whole tree — but gui_cmd does not
use it.
2. Nothing runs on an abrupt exit
process.terminate() sits in a finally: that only covers KeyboardInterrupt.
A console-close, a taskkill, or a SIGTERM skips it entirely, so the normal
way people stop a dev server is exactly the way that leaks one.
3. jarvis stop does not stop the frontend
The frontend is a sibling of the daemon, not a child, and stop only knows
about the daemon PID. So jarvis stop leaves a GUI that still serves pages but
fails every request against a backend that is gone — and leaves the port held
for the next jarvis gui.
Secondary: gui cannot reuse an API that is already running
Related, and the reason the state above is hard to escape: gui unconditionally
runs jarvis start, which exits non-zero when a daemon is already registered.
gui turns that into:
Error: Could not start the OpenJarvis API server.even when a perfectly healthy API is answering on port 8000. Between this and the port error, a user whose frontend leaked cannot get back to a working state by any obvious route.
Suggested fix
The proposed fix does four things:
Kill the tree, not the shim. Use the existing
terminate_process()in both theguicleanup path and the new teardown instop.Record the frontend. Write
{"pid", "port"}to~/.openjarvis/frontend.jsononce the dev server is listening. On startup, if the requested port is busy and it is the port this command recorded, reclaim it; otherwise keep failing, but say so accurately:Error: Frontend port 5173 is unavailable and was not started by OpenJarvis. It is held by PID 28532. Stop it, or choose another port with --frontend-port.The recorded PID is what distinguishes "my own orphan, safe to kill" from "someone else's server". Killing whatever holds the port would be wrong.
Clean up on abrupt exits too. Register the teardown with
atexitand handleSIGTERM/SIGBREAKby exiting soatexitstill runs.Adopt a healthy API instead of failing, and while the frontend runs, watch that the API is still there — a frontend outliving its backend is the confusing "serves pages, every request fails" state.
One caveat worth reviewing: the API watch must not be trigger-happy. A single loopback health probe to a busy local server intermittently stalls for seconds (measured here: one probe timing out at 2.09s while the next four returned 200 in ~30ms). Tearing down a working GUI on one timeout is a worse bug than the one being fixed, so the patch requires three consecutive failures and corroboration that the registered daemon PID is gone before it acts.
Verification
A 20-assertion script covering cold start, orphaning the frontend by killing
the launcher outright, relaunching over the orphan, 40s of watchdog stability
against a healthy API, and jarvis stop tearing down both processes: 20/20 on
the patched build, with the pre-patch build failing the relaunch and the
stop cases.
Note for anyone reproducing this: Python block-buffers stderr when it is
redirected to a file rather than a tty, so assertions against the launcher's
log read empty until the process exits unless you set PYTHONUNBUFFERED=1.
A fix is proposed in #1058.
Source: open-jarvis/OpenJarvis