Fix opencode-smoke race: drop Connection:close, add retry

Pairing Connection:close with the now-large /command response (grows
with every skill in the pack) reproducibly reset the socket mid-body
in undici once the pack passed ~50 skills. Verified locally: crashed
3/3 before, passed 3/3 after.
This commit is contained in:
Wayner Barrios
2026-08-02 16:03:06 -04:00
parent ab04919087
commit 60b8635fbf
+21 -2
View File
@@ -52,10 +52,29 @@ async function waitForServer(url, child, stderr) {
throw new Error(`Timed out waiting for OpenCode:\n${stderr.join("")}`);
}
export async function fetchJson(url, timeoutMs = 10_000) {
export async function fetchJson(url, timeoutMs = 10_000, retries = 5) {
for (let attempt = 0; ; attempt++) {
try {
return await fetchJsonOnce(url, timeoutMs);
} catch (error) {
// The health endpoint can report ready slightly before OpenCode finishes
// registering every plugin's skills as native commands. With enough
// skills, that window is long enough for a socket to be reset mid-response.
// Retry with backoff instead of failing on the first transient close.
const transient = error.cause?.code === "UND_ERR_SOCKET" || error.name === "TimeoutError";
if (!transient || attempt >= retries) throw error;
await new Promise((resolve) => setTimeout(resolve, 500 * (attempt + 1)));
}
}
}
async function fetchJsonOnce(url, timeoutMs) {
try {
// No `Connection: close` header here: pairing it with a large, still-being-
// generated response body (the command list grows with every skill in the
// pack) reproducibly triggers a premature socket reset in undici. Let the
// client negotiate keep-alive normally instead.
const response = await fetch(url, {
headers: { Connection: "close" },
signal: AbortSignal.timeout(timeoutMs),
});
if (!response.ok) throw new Error(`${url} returned HTTP ${response.status}`);