[Bug] daemon stop can kill an unrelated process that reused the lockfile's PID
Description
daemon status, start and stop trust the PID in .nanocoder/daemon.json and only check that some process with that PID exists. After a crash or reboot the lockfile can be stale, and nanocoder daemon stop then terminates whatever program now owns that PID.
Environment
- OS: Windows 11 Home 10.0.26300
- Node version: v22.22.3
- Nanocoder version: 1.32.0 (built from main at f92cce9c)
- Provider: Not provider-specific
- Model: Not model-specific
Steps to Reproduce
- Create a project folder with
.nanocoder/daemon.jsonholding the PID of any unrelated long-running process and asocketPaththat nothing listens on. - Run
runDaemonCli('status', {projectRoot}), then'start', then'stop'. - Check whether the unrelated process is still running.
Expected Behavior
Because nothing answers on the socket, the lockfile is reported as stale and removed. No signal is sent to the unrelated process.
Actual Behavior
The daemon is reported as running, and stop kills the unrelated process.
Logs/Screenshots
status: Running. pid <PID>
start: Daemon already running (pid <PID>).
stop: Daemon stopped (was pid <PID>)
-> the unrelated process exited
Additional Context
- [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 (main)
Suggested fix:
source/daemon/lockfile.ts:151-168(isProcessAlive,readLiveLockfile): also check that the IPC endpoint answers (a ping), or compare the process start time withstartedAt. Treat the lock as stale otherwise.source/daemon/cli.ts:317(status/start) and:456-469(stop): send the signal only after the daemon has answered over IPC.- Add a test with a live unrelated PID and a dead socket path.
Related: PR #1457 (introduced status/stop).
Hi @addyCooks , I would like to pick this up! Because this involves accidentally terminating unrelated host processes, I will prioritize and resolve this immediately.
Root Cause
In source/daemon/lockfile.ts, isProcessAlive(pid) only checks process.kill(pid, 0) against the OS process table. After a daemon crash, reboot, or PID reuse:
readLiveLockfiletreats any recycled PID as alive.daemon statusanddaemon startfalsely report the daemon as running (pid <PID>).- In
source/daemon/cli.ts:468,stop()attempts IPC shutdown, and upon failure immediately falls back toprocess.kill(live.pid, 'SIGTERM')—terminating an unrelated host process (especially destructive on Windows whereSIGTERMtranslates to an unconditionalTerminateProcess).
Proposed Solution
- Liveness Verification via IPC Probe:
- Update
readLiveLockfile(or introduce a fast probe helper) to ping the daemon's IPC socket usingDaemonIpcClient.ping()with a short timeout (~500ms–1s). - If the PID exists in the OS table but the socket does not answer
ping, treat the lockfile as stale, clean it up (removeLockfile), and returnnull.
- Update
- Safe
stopExecution:- In
source/daemon/cli.ts, never send a signal to an unconfirmed PID if IPC is dead. Treat it as stale, clean up the lockfile, and avoid killing innocent processes.
- In
- Automated Tests:
- Add unit/integration tests in
source/daemon/lockfile.spec.tsandsource/daemon/cli.spec.tssimulating a stale lockfile holding a live unrelated PID (e.g.process.pid) and an unreachable socket path. - Verify that:
statusidentifies the lockfile as stale and removes it.startcleans up the stale lockfile and boots cleanly instead of failing with "Daemon already running".stopnever signals or terminates the unrelated process.
- Add unit/integration tests in
- Changeset: Add a patch changeset.
Could you please assign this issue to me? I can start working on the fix right away.
Hi @VishakhaSainani, thanks for the offer and for the analysis. I've assigned this to myself because it's high priority, so I'll fix it directly.
For next time: please wait until a maintainer assigns you to an issue before opening a PR. Comment to ask first, then start once it's assigned. This avoids duplicate work like #2229.
Please close #2229. There are other open issues, so feel free to ask for one.
@addyCooks sure w'll take care