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: refactor ClientRequest destroy #36863

Closed
wants to merge 1 commit into from
Closed
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
59 changes: 26 additions & 33 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const {
DTRACE_HTTP_CLIENT_RESPONSE
} = require('internal/dtrace');

const { addAbortSignal } = require('stream');
const { addAbortSignal, finished } = require('stream');

const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/;
const kError = Symbol('kError');
Expand Down Expand Up @@ -376,38 +376,12 @@ ClientRequest.prototype.destroy = function destroy(err) {
this.res._dump();
}

// In the event that we don't have a socket, we will pop out of
// the request queue through handling in onSocket.
if (this.socket) {
_destroy(this, this.socket, err);
} else if (err) {
this[kError] = err;
}
this[kError] = err;
this.socket?.destroy(err);

return this;
};

function _destroy(req, socket, err) {
// TODO (ronag): Check if socket was used at all (e.g. headersSent) and
// re-use it in that case. `req.socket` just checks whether the socket was
// assigned to the request and *might* have been used.
if (socket && (!req.agent || req.socket)) {
socket.destroy(err);
} else {
if (socket) {
socket.emit('free');
}
if (!req.aborted && !err) {
err = connResetException('socket hang up');
}
if (err) {
req.emit('error', err);
}
req._closed = true;
req.emit('close');
}
}

function emitAbortNT(req) {
req.emit('abort');
}
Expand Down Expand Up @@ -819,11 +793,30 @@ ClientRequest.prototype.onSocket = function onSocket(socket, err) {
};

function onSocketNT(req, socket, err) {
if (req.destroyed) {
_destroy(req, socket, req[kError]);
} else if (err) {
if (req.destroyed || err) {
req.destroyed = true;
_destroy(req, null, err);

function _destroy(req, err) {
if (!req.aborted && !err) {
err = connResetException('socket hang up');
}
if (err) {
req.emit('error', err);
}
req._closed = true;
req.emit('close');
}

if (!err && req.agent) {
socket?.emit('free');
} else if (socket) {
finished(socket.destroy(err || req[kError]), (er) => {
ronag marked this conversation as resolved.
Show resolved Hide resolved
_destroy(req, er || err);
});
return;
}

_destroy(req, err || req[kError]);
Copy link
Member

Choose a reason for hiding this comment

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

A bunch of these can be slightly niftier code-style wise (??s etc) but that's pretty dumb to block on.

} else {
tickOnSocket(req, socket);
}
Expand Down