Skip to content

Commit

Permalink
http: fix ClientRequest unhandled errors
Browse files Browse the repository at this point in the history
ClientRequest could someone cause an unhandled error
from socket.

Fixes: #36931

PR-URL: #36970
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
ronag authored and danielleadams committed Feb 16, 2021
1 parent b87c0d6 commit 4cdc5ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,15 @@ function onSocketNT(req, socket, err) {
req.emit('close');
}

if (!err && req.agent) {
socket?.emit('free');
} else if (socket) {
finished(socket.destroy(err || req[kError]), (er) => {
_destroy(req, er || err);
});
return;
if (socket) {
if (!err && req.agent && !socket.destroyed) {
socket.emit('free');
} else {
finished(socket.destroy(err || req[kError]), (er) => {
_destroy(req, er || err);
});
return;
}
}

_destroy(req, err || req[kError]);
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-client-abort3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

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

function createConnection() {
const socket = new net.Socket();

process.nextTick(function() {
socket.destroy(new Error('Oops'));
});

return socket;
}

{
const req = http.get({ createConnection });

req.on('error', common.expectsError({ name: 'Error', message: 'Oops' }));
req.abort();
}

{
class CustomAgent extends http.Agent {}
CustomAgent.prototype.createConnection = createConnection;

const req = http.get({ agent: new CustomAgent() });

req.on('error', common.expectsError({ name: 'Error', message: 'Oops' }));
req.abort();
}

0 comments on commit 4cdc5ea

Please sign in to comment.