()
| 522 | retryTimer.unref?.(); |
| 523 | }; |
| 524 | const retryDaemon = async (): Promise<void> => { |
| 525 | if (shuttingDown || daemonStatus !== 'failed') return; |
| 526 | // While a writing engine owns writer.pid no daemon can start (#2277). Hand |
| 527 | // the lock back first, once the calls it is serving have answered, and |
| 528 | // buffer new calls until a daemon answers or we resume in-process. One |
| 529 | // writer at a time: the engine is fully stopped before a daemon is spawned. |
| 530 | const handover = engine !== null && !engine.isReadOnly(); |
| 531 | if (handover) { |
| 532 | daemonStatus = 'connecting'; |
| 533 | await retireEngine(); |
| 534 | } |
| 535 | const socket = await connectDaemon(); |
| 536 | if (shuttingDown) return; |
| 537 | if (socket) { |
| 538 | process.stderr.write('[CodeGraph MCP] Shared daemon reachable; proxying this session to it instead of serving in-process.\n'); |
| 539 | attachDaemon(socket, true); |
| 540 | // A read-only engine holds no lock: let its calls finish, then close it. |
| 541 | if (!handover) void retireEngine(); |
| 542 | return; |
| 543 | } |
| 544 | daemonStatus = 'failed'; |
| 545 | serveBufferedLocally(); |
| 546 | scheduleDaemonRetry(); |
| 547 | }; |
| 548 | |
| 549 | const attachDaemon = (socket: net.Socket, replayInitialize: boolean): void => { |
| 550 | daemonSocket = socket; |
no test coverage detected