fix: do not log a visitor who stops a cached download as a 500

The shared cache sends blobs with pipeline(stream, res). When the client
goes away, pipeline rejects with ERR_STREAM_PREMATURE_CLOSE. The
response is destroyed but req.aborted is false (the POST body was read),
so isResponseCancellation did not match and handleError logged a
code-less Error with httpStatus 500.

Watch the response and the source before piping. If the response closed
unfinished before the source failed, rethrow as response_cancelled, which
handleError already skips. A source that errors or closes without ending
still surfaces as a fault.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 09:05:51 +00:00
1 parent a4f65e7d30
commit 1b20ffc2f0
3 files changed
+138 -6

No files matched your search

+14 -6
View File
@@ -1,4 +1,5 @@
import { recordResponseFailure } from "./request-evidence";
import { watchClientClose } from "./response-cancellation";
import { createGzip } from "zlib";
import { createHash } from "crypto";
import { Readable, PassThrough } from "stream";
@@ -53,12 +54,19 @@ async function sendCached(content: Readable, res: Response,
// Install evidence before pipeline's listeners destroy the response.
const localFailure = () => { if (!res.destroyed) recordResponseFailure(res, "unknown", "local_abort"); };
stream.once("error", localFailure);
if (record.encoding === "identity" && !res.req.acceptsEncodings("identity")) {
res.setHeader("Content-Encoding", "gzip"); res.removeHeader("Content-Length");
const gzip = createGzip();
gzip.once("error", localFailure);
await pipeline(stream, gzip, res);
} else await pipeline(stream, res);
const clientClosed = watchClientClose(res, stream);
try {
if (record.encoding === "identity" && !res.req.acceptsEncodings("identity")) {
res.setHeader("Content-Encoding", "gzip"); res.removeHeader("Content-Length");
const gzip = createGzip();
gzip.once("error", localFailure);
await pipeline(stream, gzip, res);
} else await pipeline(stream, res);
} catch (error) {
// A visitor who stops a download is not a server fault.
if (clientClosed()) throw Object.assign(new Error("response_cancelled"), { cause: error });
throw error;
}
return true;
};
const decision = results.decision(key);
+16
View File
@@ -14,3 +14,19 @@ export function isResponseCancellation(error: unknown, response?: { destroyed?:
}
return false;
}
/**
* pipeline() rejects with ERR_STREAM_PREMATURE_CLOSE both when the client goes
* away and when the source ends early. Call before piping; the returned check
* is true only when the response closed unfinished before the source failed.
*/
export function watchClientClose(response: { writableFinished?: boolean; once(event: "close", listener: () => void): unknown },
source: { readableEnded?: boolean; once(event: "close" | "error", listener: () => void): unknown }): () => boolean {
let sourceFailed = false;
let clientClosed = false;
source.once("error", () => { sourceFailed = true; });
// A source that closes without ending was cut short.
source.once("close", () => { if (!source.readableEnded) sourceFailed = true; });
response.once("close", () => { if (!sourceFailed && !response.writableFinished) clientClosed = true; });
return () => clientClosed;
}