Bug Description:
In src/cli.ts lines 45-55, there's a race condition between checking if a process exists and cleaning up the PID file. Multiple processes could simultaneously detect a stale PID and try to clean it up.
Location:
- File:
src/cli.ts
- Lines: 45-55
Code:
function getDaemonPid(): number | null {
if (\!existsSync(PID_FILE)) return null;
const pid = parseInt(readFileSync(PID_FILE, "utf-8").trim());
try {
process.kill(pid, 0);
return pid;
} catch {
unlinkSync(PID_FILE); // Race condition here
return null;
}
}
Issues:
- Race Condition: Multiple processes can try to
unlinkSync(PID_FILE) simultaneously
- TOCTOU Bug: File could be deleted between
existsSync and readFileSync
- Error Handling:
parseInt could return NaN for corrupted PID files
- Signal 0 Limitation:
process.kill(pid, 0) doesn't work on Windows
Potential Scenarios:
- Two CLI calls happen simultaneously with stale PID file
- One process deletes PID file while another tries to read it
- Corrupted PID file causes
parseInt(NaN) to be passed to process.kill
Recommended Fixes:
- Add proper error handling for file operations
- Validate PID before using it
- Use atomic operations or file locking
- Handle Windows compatibility
Severity: Low-Medium - Could cause sporadic failures in daemon management
Bug Description:
In
src/cli.tslines 45-55, there's a race condition between checking if a process exists and cleaning up the PID file. Multiple processes could simultaneously detect a stale PID and try to clean it up.Location:
src/cli.tsCode:
Issues:
unlinkSync(PID_FILE)simultaneouslyexistsSyncandreadFileSyncparseIntcould return NaN for corrupted PID filesprocess.kill(pid, 0)doesn't work on WindowsPotential Scenarios:
parseInt(NaN)to be passed toprocess.killRecommended Fixes:
Severity: Low-Medium - Could cause sporadic failures in daemon management