Skip to content

Commit

Permalink
Fix resource leak on destroy.
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS authored and RubenVerborgh committed Sep 19, 2023
1 parent 9c728c3 commit bd8c81e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
20 changes: 16 additions & 4 deletions index.js
Expand Up @@ -38,6 +38,9 @@ var WriteAfterEndError = createErrorType(
"write after end"
);

// istanbul ignore next
var destroy = Writable.prototype.destroy || noop;

// An HTTP(S) request that can be redirected
function RedirectableRequest(options, responseCallback) {
// Initialize the request
Expand Down Expand Up @@ -68,10 +71,17 @@ function RedirectableRequest(options, responseCallback) {
RedirectableRequest.prototype = Object.create(Writable.prototype);

RedirectableRequest.prototype.abort = function () {
abortRequest(this._currentRequest);
destroyRequest(this._currentRequest);
this._currentRequest.abort();
this.emit("abort");
};

RedirectableRequest.prototype.destroy = function (error) {
destroyRequest(this._currentRequest, error);
destroy.call(this, error);
return this;
};

// Writes buffered data to the current native request
RedirectableRequest.prototype.write = function (data, encoding, callback) {
// Writing is not allowed if end has been called
Expand Down Expand Up @@ -184,6 +194,7 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
self.removeListener("abort", clearTimer);
self.removeListener("error", clearTimer);
self.removeListener("response", clearTimer);
self.removeListener("close", clearTimer);
if (callback) {
self.removeListener("timeout", callback);
}
Expand All @@ -210,6 +221,7 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
this.on("abort", clearTimer);
this.on("error", clearTimer);
this.on("response", clearTimer);
this.on("close", clearTimer);

return this;
};
Expand Down Expand Up @@ -361,7 +373,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
}

// The response is a redirect, so abort the current request
abortRequest(this._currentRequest);
destroyRequest(this._currentRequest);
// Discard the remainder of the response to avoid waiting for data
response.destroy();

Expand Down Expand Up @@ -590,12 +602,12 @@ function createErrorType(code, message, baseClass) {
return CustomError;
}

function abortRequest(request) {
function destroyRequest(request, error) {
for (var event of events) {
request.removeListener(event, eventHandlers[event]);
}
request.on("error", noop);
request.abort();
request.destroy(error);
}

function isSubdomain(subdomain, domain) {
Expand Down
16 changes: 8 additions & 8 deletions test/test.js
Expand Up @@ -269,7 +269,7 @@ describe("follow-redirects", function () {
req._currentRequest.emit("connect", "r", "s", "h");
}))
.then(function (args) {
req.abort();
req.destroy();
assert.equal(args.response, "r");
assert.equal(args.socket, "s");
assert.equal(args.head, "h");
Expand Down Expand Up @@ -429,7 +429,7 @@ describe("follow-redirects", function () {
req.on("socket", function () {
assert(req.socket instanceof net.Socket);
req.setTimeout(3000, function () {
req.abort();
req.destroy();
resolve();
});
});
Expand Down Expand Up @@ -466,7 +466,7 @@ describe("follow-redirects", function () {
});
req.on("error", reject);
req.setTimeout(1000, function () {
req.abort();
req.destroy();
resolve();
});
}));
Expand All @@ -485,7 +485,7 @@ describe("follow-redirects", function () {
});
req.on("error", reject);
req.setTimeout(2000, function () {
req.abort();
req.destroy();
resolve();
});
}));
Expand All @@ -505,7 +505,7 @@ describe("follow-redirects", function () {
var callbacks = 0;
function timeoutCallback() {
if (++callbacks === 3) {
req.abort();
req.destroy();
resolve(callbacks);
}
}
Expand Down Expand Up @@ -633,7 +633,7 @@ describe("follow-redirects", function () {
var req = http.request("http://localhost:3600/a");
req.setHeader("my-header", "my value");
assert.equal(req.getHeader("my-header"), "my value");
req.abort();
req.destroy();
});

it("should provide removeHeader", function () {
Expand Down Expand Up @@ -976,7 +976,7 @@ describe("follow-redirects", function () {
return;
}
finally {
req.abort();
req.destroy();
}
throw new Error("no error");
});
Expand Down Expand Up @@ -1218,7 +1218,7 @@ describe("follow-redirects", function () {
catch (e) {
error = e;
}
req.abort();
req.destroy();
assert(error instanceof Error);
assert(error instanceof TypeError);
assert.equal(error.message, "data should be a string, Buffer or Uint8Array");
Expand Down

0 comments on commit bd8c81e

Please sign in to comment.