MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / bindFirstUsableSocket

Function bindFirstUsableSocket

src/mcp/daemon.ts:1004–1028  ·  view source on GitHub ↗
(
  candidates: string[],
  listen: (socketPath: string) => Promise<net.Server>,
  opts: { onRelocate?: (from: string, to: string, code: string) => void } = {},
)

Source from the content-addressed store, hash-verified

1002 * direct mode (#974). Exported for testing.
1003 */
1004export async function bindFirstUsableSocket(
1005 candidates: string[],
1006 listen: (socketPath: string) => Promise<net.Server>,
1007 opts: { onRelocate?: (from: string, to: string, code: string) => void } = {},
1008): Promise<{ server: net.Server; socketPath: string }> {
1009 let lastErr: unknown;
1010 for (let i = 0; i < candidates.length; i++) {
1011 const socketPath = candidates[i]!; // i < length, so always defined
1012 const isLast = i === candidates.length - 1;
1013 try {
1014 const server = await listen(socketPath);
1015 return { server, socketPath };
1016 } catch (err) {
1017 lastErr = err;
1018 const code = (err as NodeJS.ErrnoException).code;
1019 if (!isLast && code !== SOCKET_BIND_CONFLICT_CODE) {
1020 opts.onRelocate?.(socketPath, candidates[i + 1]!, code ?? ''); // !isLast ⇒ i+1 in range
1021 continue;
1022 }
1023 throw err;
1024 }
1025 }
1026 // Only reachable with an empty candidate list — a programmer error.
1027 throw lastErr ?? new Error('no socket candidates to bind');
1028}
1029
1030function resolveIdleTimeoutMs(): number {
1031 const raw = process.env.CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS;

Callers 2

startMethod · 0.85

Calls 1

listenFunction · 0.85

Tested by

no test coverage detected