* Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout) * and on direct-mode stop. Idempotent.
()
| 318 | * and on direct-mode stop. Idempotent. |
| 319 | */ |
| 320 | stop(): Promise<void> { |
| 321 | if (this.stopPromise) return this.stopPromise; |
| 322 | this.closed = true; |
| 323 | if (!this.cg && !this.initPromise && this.explicitProjects.size === 0 && this.writerLockRoot) { |
| 324 | releaseWriterLock(this.writerLockRoot); |
| 325 | this.writerLockRoot = null; |
| 326 | } |
| 327 | |
| 328 | // Detach + terminate the worker pool first so no tool call routes to a |
| 329 | // worker mid-teardown; outstanding pool calls resolve with graceful guidance. |
| 330 | // Stopping waits for the workers to end — the pool's, and any |
| 331 | // `codegraph_status` change count still measuring: the daemon exits right |
| 332 | // after, and exiting while a worker is still starting up can crash the |
| 333 | // process. |
| 334 | this.toolHandler.setQueryPool(null); |
| 335 | const poolDown = this.queryPool ? this.queryPool.destroy() : Promise.resolve(); |
| 336 | this.queryPool = null; |
| 337 | const measurementsDown = endFreshnessMeasurements(); |
| 338 | const drained = this.toolHandler.closeAll(); |
| 339 | this.stopPromise = Promise.all([drained, poolDown, measurementsDown]).then(async () => { |
| 340 | if (this.initPromise) await this.initPromise; |
| 341 | if (this.defaultLease) { |
| 342 | await this.defaultLease.release(); |
| 343 | this.defaultLease = null; |
| 344 | this.writerLockRoot = null; |
| 345 | } else if (this.cg) { |
| 346 | this.cg.unwatch(); |
| 347 | while (this.cg.isIndexing()) await new Promise((resolve) => setTimeout(resolve, 25)); |
| 348 | this.cg.close(); |
| 349 | } |
| 350 | this.cg = null; |
| 351 | if (this.writerLockRoot) releaseWriterLock(this.writerLockRoot); |
| 352 | this.writerLockRoot = null; |
| 353 | }); |
| 354 | return this.stopPromise; |
| 355 | } |
| 356 | |
| 357 | private releaseExplicitProject(cg: CodeGraph): void | Promise<void> { |
| 358 | const lease = this.explicitProjects.get(cg); |
nothing calls this directly
no test coverage detected