Skip to content
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();
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) {
socket.emit('free');
} else {
finished(socket.destroy(err || req[kError]), (er) => {
_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');
const http = require('http');

const req = http.get('http://[2604:1380:45f1:3f00::1]:4002');

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