nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] daemon stop can kill an unrelated process that reused the lockfile's PID

Open addyCooks opened this issue 2 weeks ago • 3 comments

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

  1. Create a project folder with .nanocoder/daemon.json holding the PID of any unrelated long-running process and a socketPath that nothing listens on.
  2. Run runDaemonCli('status', {projectRoot}), then 'start', then 'stop'.
  3. 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 with startedAt. 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).

addyCooks avatar Nov 14 '26 06:11 addyCooks

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:

  1. readLiveLockfile treats any recycled PID as alive.
  2. daemon status and daemon start falsely report the daemon as running (pid <PID>).
  3. In source/daemon/cli.ts:468, stop() attempts IPC shutdown, and upon failure immediately falls back to process.kill(live.pid, 'SIGTERM')—terminating an unrelated host process (especially destructive on Windows where SIGTERM translates to an unconditional TerminateProcess).

Proposed Solution

  1. Liveness Verification via IPC Probe:
    • Update readLiveLockfile (or introduce a fast probe helper) to ping the daemon's IPC socket using DaemonIpcClient.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 return null.
  2. Safe stop Execution:
    • 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.
  3. Automated Tests:
    • Add unit/integration tests in source/daemon/lockfile.spec.ts and source/daemon/cli.spec.ts simulating a stale lockfile holding a live unrelated PID (e.g. process.pid) and an unreachable socket path.
    • Verify that:
      • status identifies the lockfile as stale and removes it.
      • start cleans up the stale lockfile and boots cleanly instead of failing with "Daemon already running".
      • stop never signals or terminates the unrelated process.
  4. Changeset: Add a patch changeset.

Could you please assign this issue to me? I can start working on the fix right away.

VishakhaSainani avatar Nov 15 '26 13:11 VishakhaSainani

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 avatar Nov 16 '26 09:11 addyCooks

@addyCooks sure w'll take care

VishakhaSainani avatar Nov 16 '26 09:11 VishakhaSainani