Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(local-installation): cancel blocked download bodies immediately

Afloat3fe700d4cdd5
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.md
index b0a59539..03d62f20 100644
--- a/packages/local-installation/AGENTS.md
+++ b/packages/local-installation/AGENTS.md
@@ -176,6 +176,11 @@ registry probe, and a fallback to the other region rewrites the file. The
resolved region reaches every `localmodels` child and native service as
`SIMPLEX_REGION`. The contract is in `scripts/installs/README.md`.
+Verified downloads stop reading on explicit cancellation even when a response
+body ignores the fetch signal. They retain the caller's abort reason, cancel
+the reader and keep only resumable partial bytes; cancellation never waits
+for the idle timeout or moves on to another mirror.
+
Move a freshly downloaded or unpacked file or folder into place with
`renameRetrying` from `fs-retry.ts`: on Windows antivirus and indexing hold
new files for a moment, and a plain rename then fails with EPERM.
diff --git a/packages/local-installation/src/download.test.ts b/packages/local-installation/src/download.test.ts
index 87646373..fe7019cb 100644
--- a/packages/local-installation/src/download.test.ts
+++ b/packages/local-installation/src/download.test.ts
@@ -195,6 +195,71 @@ it("preserves filesystem failures outside the transport boundary", async () => {
expect(await readdir(root)).toEqual([]);
});
+it.each([false, true])(
+ "cancels a blocked response body without waiting for the idle timeout (resumable: %s)",
+ async (resumable) => {
+ const root = await mkdtemp(path.join(tmpdir(), "linger-download-"));
+ roots.push(root);
+ const abort = new AbortController();
+ const reason = new Error("Installation cancelled.");
+ const bytes = Buffer.from("trusted bytes that have not all arrived");
+ const prefix = bytes.subarray(0, 7);
+ const output = path.join(root, "kit.tgz");
+ let calls = 0;
+ let cancelled = false;
+ let forceEnd: ReturnType<typeof setTimeout> | undefined;
+ try {
+ await expect(
+ downloadVerified(
+ {
+ url: "https://primary.example/kit.tgz",
+ mirrors: ["https://mirror.example/kit.tgz"],
+ output,
+ digest: createHash("sha256").update(bytes).digest("hex"),
+ algorithm: "sha256",
+ maxBytes: 100,
+ signal: abort.signal,
+ resumable,
+ },
+ async () => {
+ calls++;
+ let pulls = 0;
+ return new Response(
+ new ReadableStream<Uint8Array>(
+ {
+ start(controller) {
+ // Bound the fixture even when cancellation is ignored.
+ forceEnd = setTimeout(() => controller.close(), 1_000);
+ },
+ pull(controller) {
+ if (pulls++ === 0) controller.enqueue(prefix);
+ else {
+ abort.abort(reason);
+ return new Promise(() => {});
+ }
+ },
+ cancel() {
+ cancelled = true;
+ },
+ },
+ { highWaterMark: 0 },
+ ),
+ );
+ },
+ { ...fast, idleTimeoutMs: 60_000 },
+ ),
+ ).rejects.toBe(reason);
+ expect(cancelled).toBe(true);
+ expect(calls).toBe(1);
+ await expect(access(output)).rejects.toThrow();
+ expect(await readdir(root)).toEqual(resumable ? ["kit.tgz.part"] : []);
+ if (resumable) expect(await readFile(`${output}.part`)).toEqual(prefix);
+ } finally {
+ clearTimeout(forceEnd);
+ }
+ },
+);
+
it("resumes an interrupted transfer with a range request", async () => {
const root = await mkdtemp(path.join(tmpdir(), "linger-download-"));
roots.push(root);
diff --git a/packages/local-installation/src/download.ts b/packages/local-installation/src/download.ts
index 74f37fee..0b34bde1 100644
--- a/packages/local-installation/src/download.ts
+++ b/packages/local-installation/src/download.ts
@@ -89,6 +89,7 @@ export async function downloadVerified(
// failure, "next" when this host cannot serve the file.
const attempt = async (url: URL): Promise<"done" | "retry" | "next"> => {
const stalled = new AbortController();
+ const signal = AbortSignal.any([options.signal, stalled.signal]);
let idle: NodeJS.Timeout | undefined;
const watch = () => {
clearTimeout(idle);
@@ -100,7 +101,7 @@ export async function downloadVerified(
try {
response = await fetcher(url, {
redirect: "error",
- signal: AbortSignal.any([options.signal, stalled.signal]),
+ signal,
...(completed ? { headers: { range: `bytes=${completed}-` } } : {}),
});
} catch {
@@ -147,16 +148,19 @@ export async function downloadVerified(
throw new Error("Download size exceeds limit.");
}
const reader = response.body.getReader();
- // Some bodies ignore the abort signal, so a stall also ends the read.
- const stall = new Promise<never>((_resolve, reject) =>
- stalled.signal.addEventListener("abort", () => reject(new Error())),
- );
- stall.catch(() => {});
+ // Some bodies ignore the fetch signal; either abort still ends the read.
+ let abortRead: (() => void) | undefined;
+ const interrupted = new Promise<never>((_resolve, reject) => {
+ abortRead = () => reject(signal.reason);
+ if (signal.aborted) abortRead();
+ else signal.addEventListener("abort", abortRead, { once: true });
+ });
+ interrupted.catch(() => {});
try {
for (;;) {
let chunk: Awaited<ReturnType<typeof reader.read>>;
try {
- chunk = await Promise.race([reader.read(), stall]);
+ chunk = await Promise.race([reader.read(), interrupted]);
} catch {
options.signal.throwIfAborted();
failure = stalled.signal.aborted ? "stalled" : "interrupted";
@@ -176,6 +180,7 @@ export async function downloadVerified(
}
}
} finally {
+ if (abortRead) signal.removeEventListener("abort", abortRead);
await reader.cancel().catch(() => {});
}
} finally {