From 60b8635fbf1ce5b33fd90b79cb151a15d5192b52 Mon Sep 17 00:00:00 2001 From: Wayner Barrios Date: Sun, 2 Aug 2026 16:03:06 -0400 Subject: [PATCH] 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. --- scripts/opencode-smoke.mjs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/scripts/opencode-smoke.mjs b/scripts/opencode-smoke.mjs index 29da1ae..70c374b 100644 --- a/scripts/opencode-smoke.mjs +++ b/scripts/opencode-smoke.mjs @@ -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}`);