Skip to content
Merged
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
25 changes: 24 additions & 1 deletion dev-packages/cloudflare-integration-tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ export function cleanupChildProcesses(): void {

process.on('exit', cleanupChildProcesses);

// Wrangler can report "Ready" before it can actually handle requests.
// This retries fetch on connection errors to handle this race condition.
async function fetchWithRetry(url: string, init: RequestInit, maxRetries = 10, retryDelayMs = 200): Promise<Response> {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await fetch(url, init);
} catch (e) {
const isConnectionError =
e instanceof Error && (e.message.includes('ECONNREFUSED') || e.message.includes('fetch failed'));

if (isConnectionError && attempt < maxRetries - 1) {
if (process.env.DEBUG) log(`Request failed, retrying (attempt ${attempt + 1}/${maxRetries})...`);
await new Promise(r => setTimeout(r, retryDelayMs));
continue;
}

throw e;
}
}

throw new Error('fetchWithRetry: unreachable');
}

function deferredPromise<T = void>(
done?: () => void,
): { resolve: (val: T) => void; reject: (reason?: unknown) => void; promise: Promise<T> } {
Expand Down Expand Up @@ -316,7 +339,7 @@ export function createRunner(...paths: string[]) {
if (process.env.DEBUG) log('making request', method, url, headers, body);

try {
const res = await fetch(url, { headers, method, body });
const res = await fetchWithRetry(url, { headers, method, body });

if (!res.ok) {
if (!expectError) {
Expand Down
Loading