From 665809e1bc369a3087a18877f3005d20035c6022 Mon Sep 17 00:00:00 2001 From: nlf Date: Thu, 24 Feb 2022 10:13:19 -0800 Subject: [PATCH] fix: ensure we abort a request that emits error on the response body --- lib/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/index.js b/lib/index.js index 2ffcba8..482b967 100644 --- a/lib/index.js +++ b/lib/index.js @@ -216,6 +216,13 @@ const fetch = (url, opts) => { signal && signal.removeEventListener('abort', abortAndFinalize)) const body = new Minipass() + // if an error occurs, either on the response stream itself, on one of the + // decoder streams, or a response length timeout from the Body class, we + // forward the error through to our internal body stream. If we see an + // error event on that, we call finalize to abort the request and ensure + // we don't leave a socket believing a request is in flight. + // this is difficult to test, so lacks specific coverage. + body.on('error', finalize) // exceedingly rare that the stream would have an error, // but just in case we proxy it to the stream in use. res.on('error', /* istanbul ignore next */ er => body.emit('error', er))