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

Function bindFirstUsableSocket

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

878 * direct mode (#974). Exported for testing.
879 */
880export async function bindFirstUsableSocket(
881 candidates: string[],
882 listen: (socketPath: string) => Promise<net.Server>,
883 opts: { onRelocate?: (from: string, to: string, code: string) => void } = {},
884): Promise<{ server: net.Server; socketPath: string }> {
885 let lastErr: unknown;
886 for (let i = 0; i < candidates.length; i++) {
887 const socketPath = candidates[i]!; // i < length, so always defined
888 const isLast = i === candidates.length - 1;
889 try {
890 const server = await listen(socketPath);
891 return { server, socketPath };
892 } catch (err) {
893 lastErr = err;
894 const code = (err as NodeJS.ErrnoException).code;
895 if (!isLast && code !== SOCKET_BIND_CONFLICT_CODE) {
896 opts.onRelocate?.(socketPath, candidates[i + 1]!, code ?? ''); // !isLast ⇒ i+1 in range
897 continue;
898 }
899 throw err;
900 }
901 }
902 // Only reachable with an empty candidate list — a programmer error.
903 throw lastErr ?? new Error('no socket candidates to bind');
904}
905
906function resolveIdleTimeoutMs(): number {
907 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