Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/utils/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Comment on lines +636 to +639

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If a parsing error occurs on the multipartParser or one of its parts (e.g., due to malformed multipart data), the network stream (respStream) and the request may still be active. Without explicitly destroying the request, the client will continue to download the remaining data from the server, wasting bandwidth and system resources. Calling response.request?.destroy() on error ensures that the connection is immediately terminated and resources are freed.

Suggested change
const onError = (err: Error): void => {
this.enhanceAndReject(err, null, response.request);
};
const onError = (err: Error): void => {
response.request?.destroy();
this.enhanceAndReject(err, null, response.request);
};

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);
});
Expand Down
38 changes: 38 additions & 0 deletions test/unit/utils/api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<void>((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)
Expand Down
Loading