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

Function readClientHello

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

1125 * straddling a chunk boundary in the unshifted tail is never corrupted.
1126 */
1127function readClientHello(
1128 socket: net.Socket,
1129): Promise<ClientHelloPeers> {
1130 return new Promise((resolve) => {
1131 let chunks: Buffer[] = [];
1132 let total = 0;
1133 let settled = false;
1134 const finish = (
1135 peers: ClientHelloPeers,
1136 putBack?: Buffer,
1137 ) => {
1138 if (settled) return;
1139 settled = true;
1140 // PAUSE before detaching: removing the last 'data' listener does NOT
1141 // stop a flowing stream, so bytes arriving (or unshifted) in the gap
1142 // between this handler and the session transport attaching were emitted
1143 // to zero listeners and silently DISCARDED — and the listener swap left
1144 // the socket's flow state wedged, never delivering to the new listener.
1145 // A proxy whose client-hello arrived glued to the initialize hit this
1146 // ~1-in-5 under load: the daemon answered nothing for the whole session
1147 // (the #662 test flake, and real dead sessions behind it). Paused, the
1148 // unshifted tail and any new bytes buffer; SocketTransport.start()
1149 // resumes explicitly.
1150 try { socket.pause(); } catch { /* stream already gone */ }
1151 socket.removeListener('data', onData);
1152 socket.removeListener('error', onEnd);
1153 socket.removeListener('close', onEnd);
1154 clearTimeout(timer);
1155 if (process.env.CODEGRAPH_MCP_DEBUG) {
1156 process.stderr.write(`[mcp-debug] clientHello finish pid=${String(peers.pid)} putBack=${putBack ? putBack.length : 0} flowing=${String(socket.readableFlowing)}\n`);
1157 }
1158 if (putBack && putBack.length > 0 && !socket.destroyed) {
1159 try { socket.unshift(putBack); } catch { /* stream already gone */ }
1160 }
1161 resolve(peers);
1162 };
1163 const onData = (chunk: Buffer | string) => {
1164 const buf = typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk;
1165 chunks.push(buf);
1166 total += buf.length;
1167 const all = chunks.length === 1 ? buf : Buffer.concat(chunks, total);
1168 const nl = all.indexOf(0x0a); // '\n'
1169 if (nl === -1) {
1170 // No newline yet. If it's already too long to be a hello, it isn't one —
1171 // hand the bytes back as data; otherwise keep accumulating.
1172 if (total > MAX_HELLO_LINE_BYTES) finish({ pid: null, hostPid: null }, all);
1173 else chunks = [all];
1174 return;
1175 }
1176 const peers = parseClientHelloLine(all.subarray(0, nl).toString('utf8'));
1177 if (peers) {
1178 const tail = all.subarray(nl + 1);
1179 finish(peers, tail.length > 0 ? tail : undefined);
1180 } else {
1181 // First line is not a client-hello (legacy/direct client) — hand the
1182 // whole buffer back so the transport sees the message verbatim.
1183 finish({ pid: null, hostPid: null }, all);
1184 }

Callers 1

handleConnectionMethod · 0.85

Calls 2

finishFunction · 0.70
onMethod · 0.65

Tested by

no test coverage detected