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
16 changes: 15 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,24 @@ function socketErrorListener(err) {
debug('SOCKET ERROR:', err.message, err.stack);

if (req) {
const res = req.res;
const isUserDestroyError = err === req[kError] || err === res?.errored;

// For Safety. Some additional errors might fire later on
// and we need to make sure we don't double-fire the error event.
socket._hadError = true;
emitErrorEvent(req, err);
// Before a response exists, the request itself failed. Once a response
// exists, socket teardown belongs to the IncomingMessage and is finalized
// by socketCloseListener. Preserve errors explicitly used to destroy the
// request or response, which have historically been emitted on the request.
if (!res || isUserDestroyError) {
emitErrorEvent(req, err);
}

if (res) {
socket.destroy();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is redundant as the 'error' event should come from a previous socket.destroy(err) call.

@Archkon Archkon Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What if a developer uses socket.emit('error')? This may be rare, but should we do not consider that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Stream 'error' events should not be emitted manually. It causes a lot problems. I think it was documented at some point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@mcollina @jasnell What is your opinion on this ?

return;
Comment thread
Archkon marked this conversation as resolved.
}
}

const parser = socket.parser;
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-http-client-complete-response-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const BODY = Buffer.alloc(2 * 1024 * 1024);

const server = http.createServer(common.mustCall((request, response) => {
let received = 0;

request.on('data', function onData(chunk) {
received += chunk.length;
request.off('data', onData);
request.destroy();
});

response.writeHead(413, { 'content-length': 0 });
response.end();

request.on('end', common.mustNotCall());
request.on('close', common.mustCall(() => {
assert.strictEqual(request.complete, false);
assert.ok(received > 0);
}));
}));

server.listen(0, common.mustCall(() => {
let response;
const req = http.request({
method: 'POST',
port: server.address().port,
headers: {
'content-type': 'application/json',
'content-length': BODY.length,
},
}, common.mustCall((res) => {
response = res;
assert.strictEqual(res.statusCode, 413);
// Deliberately do not consume the response body. A response that has
// already been received should not be followed by a late ClientRequest
// socket error.
req.write(BODY);
req.end();
}));

req.on('error', common.mustNotCall());
req.on('close', common.mustCall(() => {
assert(response);
assert.strictEqual(response.complete, true);
server.close();
}));

req.flushHeaders();
}));
Loading