fix(daemon): prevent self-PID detection causing immediate exit#1467
Open
AleksandarStaikov wants to merge 1 commit intoruvnet:mainfrom
Open
fix(daemon): prevent self-PID detection causing immediate exit#1467AleksandarStaikov wants to merge 1 commit intoruvnet:mainfrom
AleksandarStaikov wants to merge 1 commit intoruvnet:mainfrom
Conversation
The foreground command wrote daemon.pid with its own PID before calling startDaemon(). WorkerDaemon.start() then called checkExistingDaemon(), which read that file, found the current process PID, called process.kill(pid, 0) on itself (always succeeds), and concluded a daemon was already running - skipping all worker scheduling entirely. With no timers scheduled, the Node.js event loop drained and the process exited silently with code 0. This caused daemon status to always show STOPPED immediately after start, on Linux, macOS, and Windows alike. Fix (two-part, belt and suspenders): 1. Remove the premature daemon.pid write from the foreground command. WorkerDaemon.start() via writePidFile() is the sole owner of this file. The cleanup handler still works as a safety net for crashes. 2. Add a self-PID guard in checkExistingDaemon(): if the stored PID equals process.pid, return null - we cannot conflict with ourselves. This defends against any future race where the file is written early. Fixes symptoms reported in ruvnet#1039. Related to ruvnet#984.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
daemon startreports success butdaemon statusimmediately shows STOPPED.The daemon process exits silently with code 0. Affects Linux, macOS, and Windows.
Reported in #1039, symptoms also visible in #984.
Root Cause
The foreground command writes
daemon.pidwith the current process PID beforecalling
startDaemon(). WhenWorkerDaemon.start()runscheckExistingDaemon(),it reads that file, finds the current process's own PID, calls
process.kill(pid, 0)on itself (which always succeeds), and concludes a daemon is already running —
returning early without scheduling any workers.
With no timers scheduled, the Node.js event loop has nothing to do and the process
exits with code 0. The daemon never ran.
Fix (belt and suspenders)
Root cause (
daemon.ts): Remove the prematuredaemon.pidwrite.WorkerDaemon.writePidFile()insidestart()is the correct and only writer.The
process.on('exit', cleanup)handler remains as a crash safety net.Defensive guard (
worker-daemon.ts): IncheckExistingDaemon(), skipthe conflict check when the stored PID equals
process.pid. A process cannotconflict with itself.
Testing
Verified on Ubuntu 24.04 via SSH. After fix:
daemon start→ process stays alive (confirmed viaps aux)daemon status→ shows● RUNNING (background)