Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: fix ClientRequest unhandled errors #36970

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const {
const { once } = require('internal/util');
const { validateNumber, validateOneOf } = require('internal/validators');

function nop() {}

const kOnKeylog = Symbol('onkeylog');
const kRequestOptions = Symbol('requestOptions');
const kRequestAsyncResource = Symbol('requestAsyncResource');
Expand Down Expand Up @@ -131,7 +133,7 @@ function Agent(options) {
// and could cause unhandled exception.

if (!socket.writable) {
socket.destroy();
socket.on('error', nop).destroy();
ronag marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down Expand Up @@ -159,7 +161,7 @@ function Agent(options) {
// the freeSockets pool, but only if we're allowed to do so.
const req = socket._httpMessage;
if (!req || !req.shouldKeepAlive || !this.keepAlive) {
socket.destroy();
socket.on('error', nop).destroy();
return;
}

Expand All @@ -173,7 +175,7 @@ function Agent(options) {
count > this.maxSockets ||
freeLen >= this.maxFreeSockets ||
!this.keepSocketAlive(socket)) {
socket.destroy();
socket.on('error', nop).destroy();
return;
}

Expand Down
16 changes: 9 additions & 7 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,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) {
ronag marked this conversation as resolved.
Show resolved Hide resolved
socket.emit('free');
} else {
finished(socket.destroy(err || req[kError]), (er) => {
lpinca marked this conversation as resolved.
Show resolved Hide resolved
_destroy(req, er || err);
});
return;
}
}

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

const common = require('../common');
const assert = require('assert');
ronag marked this conversation as resolved.
Show resolved Hide resolved
const http = require('http');

const req = http.get('http://[2604:1380:45f1:3f00::1]:4002');
ronag marked this conversation as resolved.
Show resolved Hide resolved

req.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'EHOSTUNREACH');
}));
req.abort();