Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(proxy): bound pending websocket handshakes

xuzini-xzn69989c26f7e1
diff --git a/packages/anthropic-compat-proxy/src/server-websocket.test.ts b/packages/anthropic-compat-proxy/src/server-websocket.test.ts
index 97b5b733da..4a3cfbfd89 100644
--- a/packages/anthropic-compat-proxy/src/server-websocket.test.ts
+++ b/packages/anthropic-compat-proxy/src/server-websocket.test.ts
@@ -1,4 +1,4 @@
-import { ClientRequest, createServer, type IncomingMessage } from 'node:http';
+import { createServer, globalAgent, type IncomingMessage } from 'node:http';
import { connect, Socket } from 'node:net';
import type { Duplex } from 'node:stream';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -28,6 +28,21 @@ afterEach(async () => {
while (cleanups.length) await cleanups.pop()!();
});
+// Exercise the production deadline without a 15-second real wait. Real HTTP,
+// DNS-pending sockets and callbacks still run; only expiry is triggered manually.
+function captureHandshakeDeadlines() {
+ const timers = vi.spyOn(globalThis, 'setTimeout');
+ cleanups.push(() => {
+ for (let i = 0; i < timers.mock.calls.length; i += 1) {
+ if (timers.mock.calls[i][1] === 15_000) clearTimeout(timers.mock.results[i].value);
+ }
+ timers.mockRestore();
+ });
+ return () => timers.mock.calls.flatMap(([callback, delay], i) => delay === 15_000
+ ? [{ fire: callback as () => void, timer: timers.mock.results[i].value }]
+ : []);
+}
+
function upgradeRequest(
path: string,
host: string,
@@ -293,21 +308,24 @@ describe('anthropic-compat-proxy websocket upgrades', () => {
expect(upstream.received.join('')).toContain('PING');
});
- it('uses a short timeout for the websocket handshake', async () => {
- const setTimeoutSpy = vi.spyOn(ClientRequest.prototype, 'setTimeout');
+ it('removes the handshake deadline after 101 and ignores a late expiry callback', async () => {
+ const deadlines = captureHandshakeDeadlines();
+ const cleared = vi.spyOn(globalThis, 'clearTimeout');
try {
const upstream = await startUpgradeUpstream();
proxy = await createAnthropicCompatProxy({
- upstream: 'http://unused.invalid',
- transformRequest: [],
+ upstream: 'http://unused.invalid', transformRequest: [],
resolveWebSocketUpstream: () => upstream.url,
});
-
const response = await openUpgrade(proxy.url);
expect(response.head).toContain('HTTP/1.1 101 Switching Protocols');
- expect(setTimeoutSpy.mock.calls.some(([timeout]) => timeout === 15_000)).toBe(true);
+ expect(deadlines()).toHaveLength(1);
+ expect(cleared).toHaveBeenCalledWith(deadlines()[0].timer);
+ deadlines()[0].fire();
+ response.socket.write('PING');
+ expect(await waitForSocketText(response.socket, response.rest, 'PONG')).toContain('PONG');
} finally {
- setTimeoutSpy.mockRestore();
+ cleared.mockRestore();
}
});
@@ -529,41 +547,97 @@ describe('anthropic-compat-proxy websocket upgrades', () => {
);
});
- it('returns 504 instead of falling back when the websocket handshake times out', async () => {
+ it('bounds a websocket handshake even when DNS never completes', async () => {
+ const deadlines = captureHandshakeDeadlines();
+ const originalCreateConnection = globalAgent.createConnection;
+ let pendingSocket: Socket | undefined;
+ const createConnection = vi.spyOn(globalAgent, 'createConnection').mockImplementationOnce(options => {
+ // Real ClientRequest/socket, but no external DNS or network fault timing.
+ const socket = originalCreateConnection.call(globalAgent, { ...options, lookup: () => {} });
+ if (!(socket instanceof Socket)) throw new Error('fixture requires a real pending socket');
+ pendingSocket = socket;
+ sockets.add(socket);
+ return socket;
+ });
+ try {
+ const upstream = await startUpgradeUpstream();
+ proxy = await createAnthropicCompatProxy({
+ upstream: 'http://unused.invalid', transformRequest: [],
+ resolveWebSocketUpstream: ({ url }) => url === '/stall' ? 'http://pending-lookup.invalid' : upstream.url,
+ });
+ const failed = readUpgradeFailure(proxy.url, '/stall');
+ await vi.waitFor(() => expect(pendingSocket?.connecting).toBe(true));
+ expect(deadlines(), 'the handshake deadline must run before the socket connects').toHaveLength(1);
+ const healthy = await openUpgrade(proxy.url);
+ deadlines()[0].fire();
+ expect(await failed).toBe('HTTP/1.1 504 Gateway Timeout\r\nConnection: close\r\n\r\n');
+ expect(pendingSocket?.destroyed).toBe(true);
+ healthy.socket.write('PING');
+ expect(await waitForSocketText(healthy.socket, healthy.rest, 'PONG')).toContain('PONG');
+ expect(deadlines()).toHaveLength(2);
+ } finally {
+ createConnection.mockRestore();
+ }
+ });
+
+ it('returns 504 when partial response headers never complete the handshake', async () => {
+ const deadlines = captureHandshakeDeadlines();
const upstream = createServer();
+ let handshakeStarted = false;
upstream.on('upgrade', (_req, socket) => {
sockets.add(socket);
- // Intentionally leave the handshake pending. The ClientRequest timeout spy below
- // fires the production timeout callback without making this test wait 15 seconds.
+ // Network activity cannot renew the phase deadline indefinitely.
+ socket.write('HTTP/1.1 101 Switching Protocols\r\nX-Partial: waiting\r\n');
+ handshakeStarted = true;
});
const port = await listenOnAvailableLoopbackPort(upstream);
- cleanups.push(() => new Promise<void>((resolve) => {
- upstream.closeAllConnections();
- upstream.close(() => resolve());
- }));
- const setTimeoutSpy = vi.spyOn(ClientRequest.prototype, 'setTimeout');
- setTimeoutSpy.mockImplementation(function (
- this: ClientRequest,
- msecs: number,
- callback?: () => void,
- ): ClientRequest {
- if (msecs === 15_000 && callback) queueMicrotask(callback);
- return this;
+ cleanups.push(() => new Promise<void>((resolve) => upstream.close(() => resolve())));
+ proxy = await createAnthropicCompatProxy({
+ upstream: 'http://unused.invalid', transformRequest: [],
+ resolveWebSocketUpstream: () => `http://127.0.0.1:${port}`,
});
+ const failed = readUpgradeFailure(proxy.url);
+ await vi.waitFor(() => expect(handshakeStarted).toBe(true));
+ expect(deadlines()).toHaveLength(1);
+ deadlines()[0].fire();
+ expect(await failed).toBe('HTTP/1.1 504 Gateway Timeout\r\nConnection: close\r\n\r\n');
+ });
+ it('clears a cancelled handshake deadline without starting another request', async () => {
+ const deadlines = captureHandshakeDeadlines();
+ const cleared = vi.spyOn(globalThis, 'clearTimeout');
+ const upstream = createServer();
+ let requestCount = 0;
+ let upstreamEnded = false;
+ upstream.on('upgrade', (_req, socket) => {
+ sockets.add(socket);
+ requestCount += 1;
+ socket.once('end', () => { upstreamEnded = true; socket.end(); });
+ socket.resume();
+ });
+ const port = await listenOnAvailableLoopbackPort(upstream);
+ cleanups.push(() => new Promise<void>((resolve) => upstream.close(() => resolve())));
try {
proxy = await createAnthropicCompatProxy({
- upstream: 'http://unused.invalid',
- transformRequest: [],
+ upstream: 'http://unused.invalid', transformRequest: [],
resolveWebSocketUpstream: () => `http://127.0.0.1:${port}`,
});
-
- const response = await readUpgradeFailure(proxy.url);
- expect(response).toBe(
- 'HTTP/1.1 504 Gateway Timeout\r\nConnection: close\r\n\r\n',
- );
+ const endpoint = new URL(proxy.url);
+ const client = connect(Number(endpoint.port), endpoint.hostname);
+ sockets.add(client);
+ await new Promise<void>(resolve => client.once('connect', resolve));
+ client.write(upgradeRequest('/v1/responses', endpoint.host));
+ await vi.waitFor(() => expect(requestCount).toBe(1));
+ expect(deadlines()).toHaveLength(1);
+ const deadline = deadlines()[0];
+ client.destroy();
+ await vi.waitFor(() => expect(cleared).toHaveBeenCalledWith(deadline.timer));
+ await vi.waitFor(() => expect(upstreamEnded).toBe(true));
+ deadline.fire();
+ expect(requestCount).toBe(1);
+ expect(deadlines()).toHaveLength(1);
} finally {
- setTimeoutSpy.mockRestore();
+ cleared.mockRestore();
}
});
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index 0f61a016cd..93101b5ed2 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -2871,6 +2871,12 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
let settled = false;
let upstreamReqForEarlyClose: ClientRequest | null = null;
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
+ let handshakeTimer: ReturnType<typeof setTimeout> | null = null;
+ const clearHandshakeTimer = (): void => {
+ if (handshakeTimer === null) return;
+ clearTimeout(handshakeTimer);
+ handshakeTimer = null;
+ };
let upstreamAttempt = 0;
let removeAuthorizationListener: (() => void) | undefined;
let authorizeWebSocket: (() => boolean) | undefined;
@@ -2907,6 +2913,7 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
const settle = (why: string, err?: Error): void => {
if (settled) return;
settled = true;
+ clearHandshakeTimer();
removeAuthorizationListener?.();
if (reconnectTimer !== null) {
clearTimeout(reconnectTimer);
@@ -2926,6 +2933,14 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
// client 可能在 resolveOutboundForTarget 尚未返回时就离开。close 必须立即归还预占
// 槽位;若上游请求已经创建,同时终止它,避免无人接收的握手继续占网络资源。
+ // Before 101 there is no pipe to propagate FIN. An abandoned handshake
+ // can otherwise stay half-open until its upstream eventually responds.
+ clientSocket.on('end', () => {
+ if (established || settled) return;
+ settle('client-end-before-upgrade');
+ upstreamReqForEarlyClose?.destroy();
+ clientSocket.end();
+ });
clientSocket.on('close', () => {
settle('client-close');
if (!established) {
@@ -2966,6 +2981,7 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
const scheduleReconnectAttempt = (why: string, err?: Error): void => {
if (!locallyAcceptedForReconnect || settled || clientSocket.destroyed) return;
+ clearHandshakeTimer();
upstreamReqForEarlyClose = null;
const exponent = Math.max(0, Math.min(upstreamAttempt - 1, 8));
const delayMs = Math.min(
@@ -3061,6 +3077,7 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
): void => {
if (attemptSettled || established || settled) return;
attemptSettled = true;
+ clearHandshakeTimer();
if (locallyAcceptedForReconnect) {
scheduleReconnectAttempt(why, err);
return;
@@ -3069,13 +3086,16 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
writeUpgradeFailure(clientSocket, status, message);
};
- // 握手阶段必须有独立的秒级上限(上游可能既不回 101 也不回响应)。
- // **建立成功后立刻解除**,避免任何握手 timer 误杀正常长连接。
- upstreamReq.setTimeout(WEBSOCKET_UPGRADE_TIMEOUT_MS, () => {
+ // Request/socket idle timeouts start after connection and reset on traffic.
+ // Bound the entire network handshake, including DNS/TCP/TLS, instead.
+ // This deadline is removed on 101; it never limits model inference.
+ handshakeTimer = setTimeout(() => {
+ if (attemptSettled || established || settled) return;
logger.warn?.('upgrade handshake timed out', { reqId, path: upstreamPath });
failTransientAttempt('handshake-timeout', 504, 'Gateway Timeout');
upstreamReq.destroy();
- });
+ }, WEBSOCKET_UPGRADE_TIMEOUT_MS);
+ handshakeTimer.unref?.();
upstreamReq.on('upgrade', (upstreamRes, upstreamSocket: Socket, upstreamHead: Buffer) => {
if (attemptSettled) {
@@ -3083,6 +3103,7 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
return;
}
attemptSettled = true;
+ clearHandshakeTimer();
upstreamReqForEarlyClose = null;
// 解除握手超时(长连接不能被它杀掉)。
upstreamReq.setTimeout(0);
@@ -3213,6 +3234,7 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
return;
}
attemptSettled = true;
+ clearHandshakeTimer();
upstreamReqForEarlyClose = null;
settle('upstream-refused');
const status = upstreamRes.statusCode ?? 502;