diff --git a/src/utils/api-request.ts b/src/utils/api-request.ts index fbbd49c8a7..3d25e66143 100644 --- a/src/utils/api-request.ts +++ b/src/utils/api-request.ts @@ -628,9 +628,23 @@ class AsyncRequestCall { const busboy = require('@fastify/busboy'); const multipartParser = new busboy.Dicer({ boundary }); const responseBuffer: Buffer[] = []; + + // A dropped connection or a truncated body surfaces as an 'error' event on + // the response, the parser or a part. Without handlers the request would + // never settle, and the unhandled 'error' event would be thrown. Destroy + // the request too, so a parse error doesn't keep downloading the body. + const onError = (err: Error): void => { + this.enhanceAndReject(err, null, response.request); + response.request?.destroy(); + }; + respStream.on('error', onError); + multipartParser.on('error', onError); + multipartParser.on('part', (part: any) => { const tempBuffers: Buffer[] = []; + part.on('error', onError); + part.on('data', (partData: Buffer) => { tempBuffers.push(partData); }); diff --git a/test/unit/utils/api-request.spec.ts b/test/unit/utils/api-request.spec.ts index 45075798f8..f20914bfef 100644 --- a/test/unit/utils/api-request.spec.ts +++ b/test/unit/utils/api-request.spec.ts @@ -38,6 +38,8 @@ import { import { deepCopy } from '../../../src/utils/deep-copy'; import { Agent } from 'http'; import * as zlib from 'zlib'; +import * as http from 'http'; +import { AddressInfo } from 'net'; import { getMetricsHeader } from '../../../src/utils'; chai.should(); @@ -350,6 +352,42 @@ describe('HttpClient', () => { }); }); + it('should be rejected when the connection drops during a multipart response', async () => { + // The server sends the headers and part of the body, then resets the + // connection (#2105). + const server = http.createServer((req, res) => { + res.writeHead(200, { 'content-type': 'multipart/mixed; boundary=boundary' }); + res.write('--boundary\r\nContent-type: application/json\r\n\r\n{"foo": 1}\r\n'); + setTimeout(() => res.socket?.destroy(), 10); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + const { port } = server.address() as AddressInfo; + try { + // Retries are off so the test sees the first failure. + const client = new HttpClient(null); + await client.send({ + method: 'GET', + url: `http://127.0.0.1:${port}${mockPath}`, + }).should.eventually.be.rejectedWith(/ECONNRESET/); + } finally { + await new Promise((resolve) => server.close(resolve)); + } + }); + + it('should be rejected when a multipart response ends early', () => { + const scope = nock('https://' + mockHost) + .get(mockPath) + .reply(200, '--boundary\r\nContent-type: application/json\r\n\r\n{"foo": 1}', { + 'content-type': 'multipart/mixed; boundary=boundary', + }); + mockedRequests.push(scope); + const client = new HttpClient(); + return client.send({ + method: 'GET', + url: mockUrl, + }).should.eventually.be.rejected; + }); + it('should be fulfilled for a 2xx response with any multipart payload', () => { const scope = nock('https://' + mockHost) .get(mockPath)