* Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout) * and on direct-mode stop. Idempotent.
()
| 290 | * and on direct-mode stop. Idempotent. |
| 291 | */ |
| 292 | stop(): Promise<void> { |
| 293 | if (this.stopPromise) return this.stopPromise; |
| 294 | this.closed = true; |
| 295 | if (!this.cg && !this.initPromise && this.explicitProjects.size === 0 && this.writerLockRoot) { |
| 296 | releaseWriterLock(this.writerLockRoot); |
| 297 | this.writerLockRoot = null; |
| 298 | } |
| 299 | |
| 300 | // Detach + terminate the worker pool first so no tool call routes to a |
| 301 | // worker mid-teardown; outstanding pool calls resolve with graceful guidance. |
| 302 | // Stopping waits for the workers to end — the pool's, and any |
| 303 | // `codegraph_status` change count still measuring: the daemon exits right |
| 304 | // after, and exiting while a worker is still starting up can crash the |
| 305 | // process. |
| 306 | this.toolHandler.setQueryPool(null); |
| 307 | const poolDown = this.queryPool ? this.queryPool.destroy() : Promise.resolve(); |
| 308 | this.queryPool = null; |
| 309 | const measurementsDown = endFreshnessMeasurements(); |
| 310 | const drained = this.toolHandler.closeAll(); |
| 311 | this.stopPromise = Promise.all([drained, poolDown, measurementsDown]).then(async () => { |
| 312 | if (this.initPromise) await this.initPromise; |
| 313 | if (this.defaultLease) { |
| 314 | await this.defaultLease.release(); |
| 315 | this.defaultLease = null; |
| 316 | this.writerLockRoot = null; |
| 317 | } else if (this.cg) { |
| 318 | this.cg.unwatch(); |
| 319 | while (this.cg.isIndexing()) await new Promise((resolve) => setTimeout(resolve, 25)); |
| 320 | this.cg.close(); |
| 321 | } |
| 322 | this.cg = null; |
| 323 | if (this.writerLockRoot) releaseWriterLock(this.writerLockRoot); |
| 324 | this.writerLockRoot = null; |
| 325 | }); |
| 326 | return this.stopPromise; |
| 327 | } |
| 328 | |
| 329 | private releaseExplicitProject(cg: CodeGraph): void | Promise<void> { |
| 330 | const lease = this.explicitProjects.get(cg); |
nothing calls this directly
no test coverage detected