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

Function readClientHello

src/mcp/daemon.ts:978–1049  ·  view source on GitHub ↗

* Read the optional client-hello line a proxy sends after the daemon hello. * Always resolves (never rejects) — fail-safe by design, since every connection * funnels through here. Resolves with the peer pids when the first line is a * client-hello; otherwise resolves with null pids and unshifts t

(
  socket: net.Socket,
)

Source from the content-addressed store, hash-verified

976 * straddling a chunk boundary in the unshifted tail is never corrupted.
977 */
978function readClientHello(
979 socket: net.Socket,
980): Promise<{ pid: number | null; hostPid: number | null }> {
981 return new Promise((resolve) => {
982 let chunks: Buffer[] = [];
983 let total = 0;
984 let settled = false;
985 const finish = (
986 peers: { pid: number | null; hostPid: number | null },
987 putBack?: Buffer,
988 ) => {
989 if (settled) return;
990 settled = true;
991 // PAUSE before detaching: removing the last 'data' listener does NOT
992 // stop a flowing stream, so bytes arriving (or unshifted) in the gap
993 // between this handler and the session transport attaching were emitted
994 // to zero listeners and silently DISCARDED — and the listener swap left
995 // the socket's flow state wedged, never delivering to the new listener.
996 // A proxy whose client-hello arrived glued to the initialize hit this
997 // ~1-in-5 under load: the daemon answered nothing for the whole session
998 // (the #662 test flake, and real dead sessions behind it). Paused, the
999 // unshifted tail and any new bytes buffer; SocketTransport.start()
1000 // resumes explicitly.
1001 try { socket.pause(); } catch { /* stream already gone */ }
1002 socket.removeListener('data', onData);
1003 socket.removeListener('error', onEnd);
1004 socket.removeListener('close', onEnd);
1005 clearTimeout(timer);
1006 if (process.env.CODEGRAPH_MCP_DEBUG) {
1007 process.stderr.write(`[mcp-debug] clientHello finish pid=${String(peers.pid)} putBack=${putBack ? putBack.length : 0} flowing=${String(socket.readableFlowing)}\n`);
1008 }
1009 if (putBack && putBack.length > 0 && !socket.destroyed) {
1010 try { socket.unshift(putBack); } catch { /* stream already gone */ }
1011 }
1012 resolve(peers);
1013 };
1014 const onData = (chunk: Buffer | string) => {
1015 const buf = typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk;
1016 chunks.push(buf);
1017 total += buf.length;
1018 const all = chunks.length === 1 ? buf : Buffer.concat(chunks, total);
1019 const nl = all.indexOf(0x0a); // '\n'
1020 if (nl === -1) {
1021 // No newline yet. If it's already too long to be a hello, it isn't one —
1022 // hand the bytes back as data; otherwise keep accumulating.
1023 if (total > MAX_HELLO_LINE_BYTES) finish({ pid: null, hostPid: null }, all);
1024 else chunks = [all];
1025 return;
1026 }
1027 const peers = parseClientHelloLine(all.subarray(0, nl).toString('utf8'));
1028 if (peers) {
1029 const tail = all.subarray(nl + 1);
1030 finish(peers, tail.length > 0 ? tail : undefined);
1031 } else {
1032 // First line is not a client-hello (legacy/direct client) — hand the
1033 // whole buffer back so the transport sees the message verbatim.
1034 finish({ pid: null, hostPid: null }, all);
1035 }

Callers 1

handleConnectionMethod · 0.85

Calls 2

finishFunction · 0.70
onMethod · 0.65

Tested by

no test coverage detected