Skip to content

Commit 4d5ee94

Browse files
committed
fix(provision-kit): bound install-source download stalls, not total time
A fixed 120 s whole-transfer timeout cut large app sources that were still progressing, although the 2 GiB byte cap and the 600 s install-from-source envelope allowed them. The download now fails after 120 s without headers or body bytes, and the request envelope bounds the total. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent 2e79c55 commit 4d5ee94

4 files changed

Lines changed: 146 additions & 46 deletions

File tree

‎packages/provision-kit/src/install-source-download.test.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert/strict';
22
import dns from 'node:dns/promises';
33
import fs from 'node:fs/promises';
4-
import { Readable } from 'node:stream';
4+
import { PassThrough, Readable } from 'node:stream';
55
import { test, vi } from 'vitest';
66
import { mkdtempForTest } from './tmp-dir.fixtures.ts';
77
import { downloadInstallSource } from './install-source-download.ts';
@@ -239,6 +239,63 @@ test('download rejects non-identity content encoding and malformed lengths', asy
239239
}
240240
});
241241

242+
test('download bounds stalls, not the whole transfer', async () => {
243+
const tempRoot = await mkdtempForTest('agent-device-download-stall-');
244+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
245+
const lookup = vi
246+
.spyOn(dns, 'lookup')
247+
.mockImplementation(
248+
async () =>
249+
[{ address: '93.184.216.34', family: 4 }] as unknown as Awaited<
250+
ReturnType<typeof dns.lookup>
251+
>,
252+
);
253+
const slowBody = new PassThrough();
254+
const stalledBody = new PassThrough();
255+
const requestMock = vi
256+
.spyOn(networkTransport, 'requestApprovedUrl')
257+
.mockResolvedValueOnce({ statusCode: 200, headers: {}, body: slowBody, close: async () => {} })
258+
.mockResolvedValueOnce({
259+
statusCode: 200,
260+
headers: {},
261+
body: stalledBody,
262+
close: async () => {},
263+
});
264+
const download = (url: string) =>
265+
downloadInstallSource({
266+
tempDir: tempRoot,
267+
url,
268+
signal: new AbortController().signal,
269+
environment: process.env,
270+
});
271+
const settle = () => new Promise<void>((resolve) => setImmediate(resolve));
272+
try {
273+
const slow = download('https://example.com/slow.apk');
274+
for (const chunk of ['a', 'b', 'c']) {
275+
await vi.advanceTimersByTimeAsync(100_000);
276+
slowBody.write(chunk);
277+
await settle();
278+
}
279+
slowBody.end();
280+
assert.equal(await fs.readFile(await slow, 'utf8'), 'abc');
281+
282+
const stalled = assert.rejects(download('https://example.com/stalled.apk'), {
283+
code: 'COMMAND_FAILED',
284+
message: 'App source download made no progress for 120000ms',
285+
});
286+
await settle();
287+
stalledBody.write('a');
288+
await settle();
289+
await vi.advanceTimersByTimeAsync(120_000);
290+
await stalled;
291+
} finally {
292+
vi.useRealTimers();
293+
requestMock.mockRestore();
294+
lookup.mockRestore();
295+
await fs.rm(tempRoot, { recursive: true, force: true });
296+
}
297+
});
298+
242299
function response(
243300
statusCode: number,
244301
body: Buffer = Buffer.alloc(0),

‎packages/provision-kit/src/install-source-download.ts‎

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createWriteStream, promises as fs } from 'node:fs';
22
import path from 'node:path';
3+
import { Transform } from 'node:stream';
34
import { pipeline } from 'node:stream/promises';
45
import { AppError } from '@agent-device/kernel/errors';
56
import {
@@ -11,6 +12,11 @@ import { approveDownloadSourceUrl } from './install-source-network.ts';
1112
import * as networkTransport from './install-source-network-transport.ts';
1213

1314
const MAX_REDIRECTS = 5;
15+
/**
16+
* How long a download may go without progress: no response headers, or no body bytes. The whole
17+
* transfer has no bound of its own; the request's envelope, through `signal`, bounds it.
18+
*/
19+
const SOURCE_DOWNLOAD_STALL_TIMEOUT_MS = 120_000;
1420
const DEFAULT_USER_AGENT = 'agent-device';
1521
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
1622
const NETWORK_ERROR_CODE_RE = /^[A-Z][A-Z0-9_]{0,63}$/;
@@ -28,6 +34,10 @@ const FORBIDDEN_HEADERS = new Set([
2834
'upgrade',
2935
]);
3036

37+
/**
38+
* Downloads an install source into `tempDir`. A download that makes no progress for the stall
39+
* window fails with a timeout; an aborted `signal` rejects with the abort's own error.
40+
*/
3141
export async function downloadInstallSource(params: {
3242
tempDir: string;
3343
url: string;
@@ -37,23 +47,70 @@ export async function downloadInstallSource(params: {
3747
}): Promise<string> {
3848
let currentUrl = parseSourceUrl(params.url);
3949
let headers = withDefaultUserAgent(sanitizeHeaders(params.headers));
40-
for (let redirectCount = 0; ; redirectCount += 1) {
41-
const response = await requestHop(currentUrl, headers, params.signal, params.environment);
42-
try {
43-
const redirected = readRedirect(response, currentUrl, redirectCount);
44-
if (redirected) {
45-
if (redirected.origin !== currentUrl.origin) headers = crossOriginHeaders(headers);
46-
currentUrl = redirected;
47-
continue;
50+
const stall = createStallGuard(params.signal);
51+
try {
52+
for (let redirectCount = 0; ; redirectCount += 1) {
53+
const response = await requestHop(currentUrl, headers, stall.signal, params.environment);
54+
try {
55+
const redirected = readRedirect(response, currentUrl, redirectCount);
56+
if (redirected) {
57+
if (redirected.origin !== currentUrl.origin) headers = crossOriginHeaders(headers);
58+
currentUrl = redirected;
59+
stall.progress();
60+
continue;
61+
}
62+
assertSuccessfulResponse(response);
63+
return await writeResponse(params.tempDir, currentUrl, response, stall);
64+
} finally {
65+
await response.close();
4866
}
49-
assertSuccessfulResponse(response);
50-
return await writeResponse(params.tempDir, currentUrl, response);
51-
} finally {
52-
await response.close();
5367
}
68+
} catch (error) {
69+
if (!stall.stalled() || params.signal.aborted) throw error;
70+
throw new AppError(
71+
'COMMAND_FAILED',
72+
`App source download made no progress for ${SOURCE_DOWNLOAD_STALL_TIMEOUT_MS}ms`,
73+
{ timeoutMs: SOURCE_DOWNLOAD_STALL_TIMEOUT_MS },
74+
error,
75+
);
76+
} finally {
77+
stall.dispose();
5478
}
5579
}
5680

81+
type StallGuard = {
82+
/** Aborts when the caller aborts or the download stalls. */
83+
signal: AbortSignal;
84+
progress(): void;
85+
stalled(): boolean;
86+
dispose(): void;
87+
};
88+
89+
function createStallGuard(outer: AbortSignal): StallGuard {
90+
const controller = new AbortController();
91+
let stalled = false;
92+
let timer: ReturnType<typeof setTimeout> | undefined;
93+
const onOuterAbort = () => controller.abort(outer.reason);
94+
outer.addEventListener('abort', onOuterAbort, { once: true });
95+
const arm = () => {
96+
clearTimeout(timer);
97+
timer = setTimeout(() => {
98+
stalled = true;
99+
controller.abort();
100+
}, SOURCE_DOWNLOAD_STALL_TIMEOUT_MS);
101+
};
102+
arm();
103+
return {
104+
signal: controller.signal,
105+
progress: arm,
106+
stalled: () => stalled,
107+
dispose: () => {
108+
clearTimeout(timer);
109+
outer.removeEventListener('abort', onOuterAbort);
110+
},
111+
};
112+
}
113+
57114
async function requestHop(
58115
url: URL,
59116
headers: Record<string, string>,
@@ -122,6 +179,7 @@ async function writeResponse(
122179
tempDir: string,
123180
url: URL,
124181
response: networkTransport.InstallSourceNetworkResponse,
182+
stall: StallGuard,
125183
): Promise<string> {
126184
const encoding = readHeader(response.headers, 'content-encoding');
127185
if (encoding && encoding.toLowerCase() !== 'identity') {
@@ -139,7 +197,19 @@ async function writeResponse(
139197
),
140198
});
141199
try {
142-
await pipeline(response.body, byteLimit, createWriteStream(destinationPath, { flags: 'wx' }));
200+
const progress = new Transform({
201+
transform(chunk, _encoding, callback) {
202+
stall.progress();
203+
callback(null, chunk);
204+
},
205+
});
206+
await pipeline(
207+
response.body,
208+
progress,
209+
byteLimit,
210+
createWriteStream(destinationPath, { flags: 'wx' }),
211+
{ signal: stall.signal },
212+
);
143213
return destinationPath;
144214
} catch (error) {
145215
await fs.rm(destinationPath, { force: true }).catch(() => {});

‎packages/provision-kit/src/install-source.ts‎

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'node:fs';
22
import os from 'node:os';
33
import path from 'node:path';
44
import type { LocalInstallSource } from '@agent-device/kernel/contracts';
5-
import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors';
5+
import { createRequestCanceledError } from '@agent-device/kernel/errors';
66
import { ArchiveBudget } from '@agent-device/host-kit/archive';
77
import { expandUserHomePath } from '@agent-device/host-kit/file';
88

@@ -45,7 +45,6 @@ const INTERNAL_ARCHIVE_EXTENSIONS = ['.zip', '.tar', '.tar.gz', '.tgz'] as const
4545
* @public Archive extensions accepted by install-source resolution.
4646
*/
4747
export const ARCHIVE_EXTENSIONS = Object.freeze([...INTERNAL_ARCHIVE_EXTENSIONS] as const);
48-
const DEFAULT_SOURCE_DOWNLOAD_TIMEOUT_MS = 120_000;
4948

5049
export async function materializeInstallablePath(
5150
options: MaterializeInstallableOptions,
@@ -124,44 +123,18 @@ async function downloadToTempFile(
124123
if (requestSignal?.aborted) {
125124
throw createRequestCanceledError();
126125
}
127-
const timeoutSignal = AbortSignal.timeout(DEFAULT_SOURCE_DOWNLOAD_TIMEOUT_MS);
128-
const signal = requestSignal ? AbortSignal.any([requestSignal, timeoutSignal]) : timeoutSignal;
129126
try {
130127
return await downloadInstallSource({
131128
tempDir,
132129
url,
133130
headers,
134-
signal,
131+
signal: requestSignal ?? new AbortController().signal,
135132
environment: options.environment,
136133
});
137134
} catch (error) {
138-
throw classifyDownloadError(
139-
error,
140-
requestSignal,
141-
timeoutSignal,
142-
DEFAULT_SOURCE_DOWNLOAD_TIMEOUT_MS,
143-
);
144-
}
145-
}
146-
147-
function classifyDownloadError(
148-
error: unknown,
149-
requestSignal: AbortSignal | undefined,
150-
timeoutSignal: AbortSignal,
151-
timeoutMs: number,
152-
): unknown {
153-
if (requestSignal?.aborted) {
154-
return createRequestCanceledError(undefined, error);
155-
}
156-
if (timeoutSignal.aborted) {
157-
return new AppError(
158-
'COMMAND_FAILED',
159-
`App source download timed out after ${timeoutMs}ms`,
160-
{ timeoutMs },
161-
error,
162-
);
135+
if (requestSignal?.aborted) throw createRequestCanceledError(undefined, error);
136+
throw error;
163137
}
164-
return error;
165138
}
166139

167140
export async function validateDownloadSourceUrl(parsedUrl: URL): Promise<void> {

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ agent-device install-from-source --github-actions-artifact thymikee/RNCLI83:6635
690690
```
691691
692692
- `install-from-source <url>` installs an artifact the daemon can reach at a URL; use `install` or `reinstall` for local files. Device coverage matches `install`.
693-
- `install-from-source` allows 600 seconds overall, including up to 120 seconds for the download. The local Android and iOS install limits match `install`.
693+
- `install-from-source` allows 600 seconds overall, including the download. The download fails if it receives no data for 120 seconds. The local Android and iOS install limits match `install`.
694694
- A URL may point to an Android `.apk` or `.aab`, or, from any public host, to an archive containing one `.apk`, `.aab`, `.ipa`, or iOS `.app`.
695695
- `--github-actions-artifact <owner/repo:artifact>` lets a compatible remote daemon fetch a GitHub Actions artifact with its own credentials. A numeric artifact is treated as an id, anything else as a name.
696696
- Repeat `--header <name:value>` for authenticated or signed downloads. Sensitive headers are not forwarded to other origins.

0 commit comments

Comments
 (0)