Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/live-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const LIVE_FILES = [
// appended, NOT alphabetical: slot = index drives each file's zone port
// band (preload-zone-slot), so mid-list inserts renumber every later band
"tests/storage-durability.test.ts",
"tests/server-close-all-connections.test.ts",
];

/** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry;
Expand Down
28 changes: 28 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,34 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
return n;
};
(server as RestartCapableServer)[RESTART_FORCE_CLOSE_KEY] = forceCloseLingeringSockets;
// #4380: the linger wrap above defers socket.destroy() for
// completed-response sockets, which silently defeats
// server.closeAllConnections() — the API's contract is "cut every
// tracked connection NOW", but idle keep-alive pooled sockets sit in
// the #2895 off-parser class node's own iteration never reaches: they
// only die when the peer's pool expires (~3s measured, linger budget
// 5s default). Every startServer/close cycle in embedded use (test
// teardown, graceful shutdown) paid that per stop — measured as
// minutes of pure wall time across the CI suite. Cut exactly the
// linger-protected class (completed at least one response, never
// errored): stamp the #2895 intentional-cut marker and destroy via the
// ledger, then delegate to the original for anything node tracks that
// the ledger never recorded. Sockets that never completed a response
// (held CONNECT tunnels, claimed upgrades) stay untouched here — plain
// closeAllConnections() still cannot cut those (#2895 semantics,
// pinned by the auto-restart repro); the restart force-close seam owns
// them, as before.
const origCloseAllConnections = server.closeAllConnections?.bind(server);
if (origCloseAllConnections !== undefined) {
server.closeAllConnections = () => {
for (const [socket, rec] of [...connRecords]) {
if (rec.lastResponseEndAt === null || rec.errored !== null) continue;
rec.restartForced = true;
socket.destroy();
}
origCloseAllConnections();
};
}
// #1452: Node's default client-error disposition (no listener) writes a
// bare `HTTP/1.1 400 Bad Request` / Connection: close reply and then
// destroys the socket (measured Linux/Node 25). Depending on platform
Expand Down
92 changes: 92 additions & 0 deletions tests/server-close-all-connections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// #4380: server.closeAllConnections() must cut through the post-response
// linger (#1982) — before the fix, idle keep-alive pooled sockets sat out the
// linger budget (3-5s) after an explicit closeAllConnections() call, taxing
// every embedded startServer/close cycle (test teardown, graceful shutdown).
import test from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
import { startServer, restartForceCloseOf } from "../src/server.ts";
import { defaultConfig } from "acp-kernel";
import type { ProxyOptions } from "../src/config.ts";

const UPSTREAM_BODY = JSON.stringify({
id: "linger-cut", role: "assistant",
content: [{ type: "text", text: "answer" }],
usage: { input_tokens: 5, output_tokens: 1 },
});

async function harness(): Promise<{ proxy: http.Server; origin: string; upstream: http.Server; upstreamUrl: string }> {
const upstream = http.createServer((_req, res) => {
res.writeHead(200, { "content-type": "application/json" });
res.end(UPSTREAM_BODY);
});
upstream.listen(0, "127.0.0.1");
await once(upstream, "listening");
const upstreamUrl = `http://127.0.0.1:${(upstream.address() as import("node:net").AddressInfo).port}`;
const proxy = await startServer({
port: 0, host: "127.0.0.1", upstream: upstreamUrl,
routes: { [upstreamUrl]: { models: { "model-test": { context: 64_000 } } } },
modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000),
compress: { injectTool: false, injectNudge: false },
promptCache: { routing: "auto" }, sessionHeader: "x-acp-session",
log: false, debug: false, passthrough: false, autoUpdate: false,
mitm: { enabled: false, domains: [] },
} as ProxyOptions);
await once(proxy, "listening");
const origin = `http://127.0.0.1:${(proxy.address() as import("node:net").AddressInfo).port}`;
return { proxy, origin, upstream, upstreamUrl };
}

async function sendOne(origin: string, upstreamUrl: string): Promise<void> {
const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, {
method: "POST",
headers: { "content-type": "application/json", "x-acp-session": "linger-cut" },
body: JSON.stringify({ model: "model-test", messages: [{ role: "user", content: "hi" }], max_tokens: 8 }),
});
const bodyText = await r.text();
assert.equal(r.status, 200, bodyText);
}

test("closeAllConnections() cuts through the linger: server closes immediately after an explicit cut (#4380)", { timeout: 30_000 }, async () => {
const { proxy, origin, upstream, upstreamUrl } = await harness();
try {
await sendOne(origin, upstreamUrl);
proxy.close();
proxy.closeAllConnections();
const closedAt = Date.now();
// Pre-fix behavior: the idle pooled socket sat out the linger (~3s
// until undici's pool expires, up to the 5s budget). 2.5s stays far
// under the linger floor while tolerating slow-runner noise.
await once(proxy, "close");
assert.ok(Date.now() - closedAt < 2_500, `close took ${Date.now() - closedAt}ms — linger still defeating closeAllConnections (#4380)`);
} finally {
upstream.close();
upstream.closeIdleConnections?.();
upstream.closeAllConnections?.();
}
});

test("plain close() keeps the linger: an idle pooled socket is NOT cut without the explicit call (#4380 negative)", { timeout: 30_000 }, async () => {
const { proxy, origin, upstream, upstreamUrl } = await harness();
try {
await sendOne(origin, upstreamUrl);
proxy.close();
// Without closeAllConnections() the linger protection must stay
// armed: the idle keep-alive socket survives past the point where a
// defeated linger would have let node close immediately.
await new Promise((r) => setTimeout(r, 1_200));
assert.equal(proxy.listening, false, "listener stopped (close() did run)");
// Still no 'close' event => connection still held: the linger is intact.
let fired = false;
proxy.once("close", () => { fired = true; });
assert.equal(fired, false, "close event must not have fired within the linger window — linger protection would be gone");
// Finish via the sanctioned force-close seam.
restartForceCloseOf(proxy)?.();
await once(proxy, "close");
} finally {
upstream.close();
upstream.closeIdleConnections?.();
upstream.closeAllConnections?.();
}
});
Loading