(
peers: { pid: number | null; hostPid: number | null },
putBack?: Buffer,
)
| 798 | let total = 0; |
| 799 | let settled = false; |
| 800 | const finish = ( |
| 801 | peers: { pid: number | null; hostPid: number | null }, |
| 802 | putBack?: Buffer, |
| 803 | ) => { |
| 804 | if (settled) return; |
| 805 | settled = true; |
| 806 | // PAUSE before detaching: removing the last 'data' listener does NOT |
| 807 | // stop a flowing stream, so bytes arriving (or unshifted) in the gap |
| 808 | // between this handler and the session transport attaching were emitted |
| 809 | // to zero listeners and silently DISCARDED — and the listener swap left |
| 810 | // the socket's flow state wedged, never delivering to the new listener. |
| 811 | // A proxy whose client-hello arrived glued to the initialize hit this |
| 812 | // ~1-in-5 under load: the daemon answered nothing for the whole session |
| 813 | // (the #662 test flake, and real dead sessions behind it). Paused, the |
| 814 | // unshifted tail and any new bytes buffer; SocketTransport.start() |
| 815 | // resumes explicitly. |
| 816 | try { socket.pause(); } catch { /* stream already gone */ } |
| 817 | socket.removeListener('data', onData); |
| 818 | socket.removeListener('error', onEnd); |
| 819 | socket.removeListener('close', onEnd); |
| 820 | clearTimeout(timer); |
| 821 | if (process.env.CODEGRAPH_MCP_DEBUG) { |
| 822 | process.stderr.write(`[mcp-debug] clientHello finish pid=${String(peers.pid)} putBack=${putBack ? putBack.length : 0} flowing=${String(socket.readableFlowing)}\n`); |
| 823 | } |
| 824 | if (putBack && putBack.length > 0 && !socket.destroyed) { |
| 825 | try { socket.unshift(putBack); } catch { /* stream already gone */ } |
| 826 | } |
| 827 | resolve(peers); |
| 828 | }; |
| 829 | const onData = (chunk: Buffer | string) => { |
| 830 | const buf = typeof chunk === 'string' ? Buffer.from(chunk, 'utf8') : chunk; |
| 831 | chunks.push(buf); |
no test coverage detected