* Bind the socket, refresh the ownership record, kick off engine init, and * register signal handlers. The promise resolves once the server is listening * — the daemon then sticks around until idle/shutdown.
()
| 249 | * — the daemon then sticks around until idle/shutdown. |
| 250 | */ |
| 251 | async start(): Promise<DaemonStartResult> { |
| 252 | // #1740: claim the project writer lock before opening/watching so a |
| 253 | // concurrent direct-mode serve --mcp cannot start a second watcher. |
| 254 | assertNoRebuild(this.projectRoot); |
| 255 | let writer = tryAcquireWriterLock(this.projectRoot, 'daemon'); |
| 256 | // The launcher that stopped an older daemon has held the slot for us since |
| 257 | // (#2335); take it over without letting it fall free. |
| 258 | if (writer.kind === 'taken' && this.handoverFrom !== null && |
| 259 | writer.existing?.pid === this.handoverFrom && writer.existing.mode === 'handover') { |
| 260 | const info: WriterLockInfo = { pid: process.pid, mode: 'daemon', startedAt: Date.now(), ready: false }; |
| 261 | if (swapWriterLock(this.projectRoot, this.handoverFrom, info)) { |
| 262 | writer = { kind: 'acquired', pidPath: writer.pidPath, info }; |
| 263 | process.stderr.write(`[CodeGraph daemon] Took over the writer lock from launcher pid ${this.handoverFrom}, which stopped an older daemon.\n`); |
| 264 | } |
| 265 | } |
| 266 | if (writer.kind === 'taken') { |
| 267 | const msg = writerLockHeldMessage(writer.existing, writer.pidPath); |
| 268 | process.stderr.write(`[CodeGraph daemon] ${msg}\n`); |
| 269 | this.cleanupLockfile(); |
| 270 | throw new Error(msg); |
| 271 | } |
| 272 | |
| 273 | let initialLockContents: string; |
| 274 | try { |
| 275 | initialLockContents = fs.readFileSync(this.pidPath, 'utf8'); |
| 276 | if (decodeLockInfo(initialLockContents)?.pid !== process.pid) { |
| 277 | throw new Error('daemon lock belongs to another process'); |
| 278 | } |
| 279 | } catch { |
| 280 | releaseWriterLock(this.projectRoot); |
| 281 | throw new Error('Lost daemon lock ownership before startup.'); |
| 282 | } |
| 283 | |
| 284 | // Walk the ordered socket candidates and bind the first that works. The |
| 285 | // in-project path comes first; the deterministic tmpdir path is the fallback |
| 286 | // for a filesystem that can't host an AF_UNIX node at all (ExFAT/FAT external |
| 287 | // volumes, some network mounts, WSL2 DrvFs → ENOTSUP/EACCES; #997, #974). The |
| 288 | // `listen` closure clears a stale socket (left by a SIGKILL'd previous daemon) |
| 289 | // before each attempt — safe because we hold the lockfile, so no live daemon |
| 290 | // owns it; without it `listen` would wedge on EADDRINUSE. |
| 291 | const candidates = getDaemonSocketCandidates(this.projectRoot); |
| 292 | const listen = (socketPath: string): Promise<net.Server> => |
| 293 | new Promise<net.Server>((resolve, reject) => { |
| 294 | if (process.platform !== 'win32') { |
| 295 | try { fs.unlinkSync(socketPath); } catch { /* not-exists is fine */ } |
| 296 | } |
| 297 | const server = net.createServer((socket) => this.handleConnection(socket)); |
| 298 | server.once('error', reject); |
| 299 | server.listen(socketPath, () => { |
| 300 | // POSIX: tighten permissions to user-only — the socket lives under |
| 301 | // `.codegraph/` (git-ignored, maybe a shared FS) or tmpdir. |
| 302 | if (process.platform !== 'win32') { |
| 303 | try { fs.chmodSync(socketPath, 0o600); } catch { /* best-effort */ } |
| 304 | } |
| 305 | resolve(server); |
| 306 | }); |
| 307 | }); |
| 308 |
no test coverage detected