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

Function bindFirstUsableSocket

src/mcp/daemon.ts:1017–1041  ·  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

1015 * direct mode (#974). Exported for testing.
1016 */
1017export async function bindFirstUsableSocket(
1018 candidates: string[],
1019 listen: (socketPath: string) => Promise<net.Server>,
1020 opts: { onRelocate?: (from: string, to: string, code: string) => void } = {},
1021): Promise<{ server: net.Server; socketPath: string }> {
1022 let lastErr: unknown;
1023 for (let i = 0; i < candidates.length; i++) {
1024 const socketPath = candidates[i]!; // i < length, so always defined
1025 const isLast = i === candidates.length - 1;
1026 try {
1027 const server = await listen(socketPath);
1028 return { server, socketPath };
1029 } catch (err) {
1030 lastErr = err;
1031 const code = (err as NodeJS.ErrnoException).code;
1032 if (!isLast && code !== SOCKET_BIND_CONFLICT_CODE) {
1033 opts.onRelocate?.(socketPath, candidates[i + 1]!, code ?? ''); // !isLast ⇒ i+1 in range
1034 continue;
1035 }
1036 throw err;
1037 }
1038 }
1039 // Only reachable with an empty candidate list — a programmer error.
1040 throw lastErr ?? new Error('no socket candidates to bind');
1041}
1042
1043function resolveIdleTimeoutMs(): number {
1044 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