Retry stalled SwiftPM artifact downloads (#59214)

Summary:
SwiftPM artifact setup currently fails an entire CI lane when a Maven response stalls mid-body. This change:

- aborts a download after 60 seconds without body progress;
- retries transient transport errors and HTTP 408/429/5xx responses up to three total attempts with short backoff;
- keeps deterministic HTTP client errors fail-fast; and
- removes partial `.download` files before retrying, so a retry cannot reuse truncated content.

## Changelog:

[INTERNAL] [FIXED] - Retry stalled SwiftPM artifact downloads

## Diagnosis

`main` commit `f1f516e8cb2f6c085bcc08838066b5b49c08da3b` failed `test_ios_spm_rntester / test (Debug)` in job https://github.com/react/react-native/actions/runs/39353507614/job/118412160607. The Hermes release artifact stopped at 8.0 MB of 24.2 MB and 0 KB/s for about ten minutes before Node fetch reported `terminated`.

This is a transport flake rather than a deterministic source regression:

- the Release sibling on the same SHA completed successfully;
- both SwiftPM RNTester lanes passed on each of the 19 preceding `main` workflow runs (38/38 checks);
- the tip commit only changes Fantom SelectableParagraph rendering; and
- the exact failed artifact downloaded successfully on a subsequent probe (25,372,145 bytes in 0.29 seconds).

Because the failure is nondeterministic and independent of the commit contents, a code bisect cannot identify a meaningful introducing commit. Searches of open PRs and prior CI-monitor runs found no existing fix for this stalled-download signature. PR https://github.com/react/react-native/issues/59068 mentions the downloader only in its diagnosis and changes different prebuild files.

Pull Request resolved: https://github.com/react/react-native/pull/59214

Test Plan:
- `node --check packages/react-native/scripts/spm/download-spm-artifacts.js`
- `npx -y prettier@3.9.4 --write packages/react-native/scripts/spm/download-spm-artifacts.js packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js` (no changes)
- `jest --runInBand packages/react-native/scripts/spm/__tests__/download-spm-artifacts-test.js` — 76/76 tests passed, including transient stream retry, idle-body retry, partial-file cleanup, and fail-fast HTTP 404 coverage
- Live curl probe of the exact Hermes artifact — HTTP 200, 25,372,145 bytes

## Risk and limitations

A genuinely slow transfer that makes no progress for 60 seconds is restarted, which may add up to roughly two minutes plus backoff before the final failure. The retry is bounded and final errors are still surfaced; deterministic 4xx responses are not retried. Full RNTester/Xcode CI was not reproduced locally because the failure is in remote transport rather than compilation.

Reviewed By: cipolleschi

Differential Revision: D126318143

Pulled By: cortinico

fbshipit-source-id: c413b2278c4ca735e69a1d275f33e74fcea679ca
This commit is contained in:
Nicola Corti authored and meta-codesync[bot] committed 2026-10-20 04:29:05 -07:00
1 parent 62002039c0
commit fcd141cf81
2 files changed
+185 -8

No files matched your search

@@ -11,6 +11,7 @@
'use strict';
const {
download,
exists,
extractXCFramework,
findFirst,
@@ -545,6 +546,125 @@ describe('exists', () => {
});
});
// ---------------------------------------------------------------------------
// download — retries transient transport failures without masking client errors.
// ---------------------------------------------------------------------------
describe('download', () => {
let originalFetch;
let tempDir;
let warnSpy;
beforeEach(() => {
originalFetch = globalThis.fetch;
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-download-'));
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
globalThis.fetch = originalFetch;
warnSpy.mockRestore();
fs.rmSync(tempDir, {recursive: true, force: true});
});
function downloadResponse(body, error, stall = false) {
const bytes = Buffer.from(body);
return {
ok: true,
status: 200,
statusText: 'OK',
headers: {
get: name =>
name.toLowerCase() === 'content-length' ? String(bytes.length) : null,
},
body: new ReadableStream({
start(controller) {
controller.enqueue(bytes);
if (error != null) {
controller.error(error);
} else if (!stall) {
controller.close();
}
},
}),
};
}
it('retries a transient stream failure and replaces the partial download', async () => {
globalThis.fetch = jest
.fn()
.mockResolvedValueOnce(
downloadResponse('partial', new Error('terminated')),
)
.mockResolvedValueOnce(downloadResponse('complete'));
const destination = path.join(tempDir, 'artifact.tar.gz');
const pending = download(
'https://example/artifact.tar.gz',
destination,
() => {},
);
await jest.advanceTimersByTimeAsync(1_500);
await pending;
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('retrying in 1000ms'),
);
expect(fs.readFileSync(destination, 'utf8')).toBe('complete');
expect(fs.existsSync(`${destination}.download`)).toBe(false);
});
it('retries when an otherwise healthy response stops making progress', async () => {
jest.useFakeTimers();
try {
globalThis.fetch = jest
.fn()
.mockResolvedValueOnce(downloadResponse('partial', null, true))
.mockResolvedValueOnce(downloadResponse('complete'));
const destination = path.join(tempDir, 'stalled.tar.gz');
const pending = download(
'https://example/stalled.tar.gz',
destination,
() => {},
);
await jest.advanceTimersByTimeAsync(61_500);
await pending;
expect(globalThis.fetch).toHaveBeenCalledTimes(2);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('retrying in 1000ms'),
);
expect(fs.readFileSync(destination, 'utf8')).toBe('complete');
expect(fs.existsSync(`${destination}.download`)).toBe(false);
} finally {
jest.useRealTimers();
}
});
it('does not retry a deterministic HTTP client error', async () => {
globalThis.fetch = jest.fn(async () => ({
ok: false,
status: 404,
statusText: 'Not Found',
headers: {get: () => null},
body: null,
}));
const destination = path.join(tempDir, 'missing.tar.gz');
await expect(
download('https://example/missing.tar.gz', destination, () => {}),
).rejects.toThrow(/HTTP 404/);
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
expect(warnSpy).not.toHaveBeenCalled();
expect(fs.existsSync(destination)).toBe(false);
});
});
// ---------------------------------------------------------------------------
// resolveSnapshotUrl — parses maven-metadata.xml into a timestamped URL.
// ---------------------------------------------------------------------------
+65 -8
View File
@@ -640,16 +640,26 @@ function createProgressDisplay(
type ProgressCallback = (label: string, downloaded: number, total: number, speed: number, done: boolean, elapsed: number) => void;
*/
async function download(
const DOWNLOAD_MAX_ATTEMPTS = 3;
const DOWNLOAD_RETRY_BASE_DELAY_MS = 1000;
const DOWNLOAD_IDLE_TIMEOUT_MS = 60_000;
function isRetryableDownloadError(error /*: Error */) /*: boolean */ {
// Client errors are deterministic except for request timeout / rate limiting.
// Network and stream errors do not carry an HTTP prefix and are safe to retry.
const httpStatus = /^HTTP (\d{3})\b/.exec(error.message)?.[1];
if (httpStatus == null) {
return true;
}
const status = Number(httpStatus);
return status === 408 || status === 429 || status >= 500;
}
async function downloadOnce(
url /*: string */,
destPath /*: string */,
onProgress /*:: ?: ProgressCallback */,
) /*: Promise<void> */ {
if (fs.existsSync(destPath)) {
log(` Already cached: ${path.basename(destPath)}`);
return;
}
const tmpPath = destPath + '.download';
try {
// $FlowFixMe[incompatible-call] global fetch not in Flow stubs
@@ -662,6 +672,7 @@ async function download(
let downloadedBytes = 0;
let lastPrintTime = Date.now();
let lastPrintBytes = 0;
let lastDataTime = Date.now();
const startTime = Date.now();
const fileStream = fs.createWriteStream(tmpPath);
@@ -719,14 +730,25 @@ async function download(
await new Promise((resolve, reject) => {
let progressInterval;
try {
progressInterval = setInterval(() => reportProgress(), 500);
progressInterval = setInterval(() => {
reportProgress();
if (Date.now() - lastDataTime >= DOWNLOAD_IDLE_TIMEOUT_MS) {
nodeReadable.destroy(
new Error(
`Download made no progress for ${DOWNLOAD_IDLE_TIMEOUT_MS / 1000}s: ${url}`,
),
);
}
}, 500);
nodeReadable
.on('data', chunk => {
downloadedBytes += chunk.length;
lastDataTime = Date.now();
})
.on('error', err => {
clearInterval(progressInterval);
fileStream.destroy();
reject(err);
})
.pipe(fileStream)
@@ -737,10 +759,13 @@ async function download(
})
.on('error', err => {
clearInterval(progressInterval);
nodeReadable.destroy();
reject(err);
});
} catch (err) {
if (progressInterval != null) clearInterval(progressInterval);
nodeReadable.destroy();
fileStream.destroy();
reject(err);
}
});
@@ -753,7 +778,7 @@ async function download(
fs.renameSync(tmpPath, destPath);
} catch (err) {
// Clean up partial .download temp file on failure
// Clean up partial .download temp file on failure.
try {
fs.unlinkSync(tmpPath);
} catch {
@@ -763,6 +788,37 @@ async function download(
}
}
async function download(
url /*: string */,
destPath /*: string */,
onProgress /*:: ?: ProgressCallback */,
) /*: Promise<void> */ {
if (fs.existsSync(destPath)) {
log(` Already cached: ${path.basename(destPath)}`);
return;
}
for (let attempt = 1; attempt <= DOWNLOAD_MAX_ATTEMPTS; attempt++) {
try {
await downloadOnce(url, destPath, onProgress);
return;
} catch (error) {
if (
attempt === DOWNLOAD_MAX_ATTEMPTS ||
!isRetryableDownloadError(error)
) {
throw error;
}
const delayMs = DOWNLOAD_RETRY_BASE_DELAY_MS * attempt;
warn(
` Download failed (${error.message}); retrying in ${delayMs}ms ` +
`(attempt ${attempt + 1}/${DOWNLOAD_MAX_ATTEMPTS})`,
);
await new Promise(resolve => setTimeout(resolve, delayMs));
}
}
}
/**
* Extracts a .tar.gz and returns the path to the first .xcframework found.
*/
@@ -1544,6 +1600,7 @@ module.exports = {
resolveLatestV1Version,
resolveRNCoreArtifact,
resolveRNDepsArtifact,
download,
exists,
formatBytes,
formatSpeed,