fix(infra): avoid req.destroy(err) in request body limiters

This commit is contained in:
Peter Steinberger
2026-02-15 03:19:16 +01:00
parent 4a44da7d91
commit 444a910d9e
2 changed files with 41 additions and 6 deletions

View File

@@ -96,7 +96,9 @@ export async function readRequestBodyWithLimit(
if (declaredLength !== null && declaredLength > maxBytes) {
const error = new RequestBodyLimitError({ code: "PAYLOAD_TOO_LARGE" });
if (!req.destroyed) {
req.destroy(error);
// Limit violations are expected user input; destroying with an Error causes
// an async 'error' event which can crash the process if no listener remains.
req.destroy();
}
throw error;
}
@@ -131,7 +133,7 @@ export async function readRequestBodyWithLimit(
const timer = setTimeout(() => {
const error = new RequestBodyLimitError({ code: "REQUEST_BODY_TIMEOUT" });
if (!req.destroyed) {
req.destroy(error);
req.destroy();
}
fail(error);
}, timeoutMs);
@@ -145,7 +147,7 @@ export async function readRequestBodyWithLimit(
if (totalBytes > maxBytes) {
const error = new RequestBodyLimitError({ code: "PAYLOAD_TOO_LARGE" });
if (!req.destroyed) {
req.destroy(error);
req.destroy();
}
fail(error);
return;
@@ -294,7 +296,9 @@ export function installRequestBodyLimitGuard(
finish();
respond(error);
if (!req.destroyed) {
req.destroy(error);
// Limit violations are expected user input; destroying with an Error causes
// an async 'error' event which can crash the process if no listener remains.
req.destroy();
}
};