Skip to content

Commit

Permalink
stream: fix duplicate logic in stream destroy
Browse files Browse the repository at this point in the history
Fix duplicate logic in stream destroy as the same logic is being shared
across methods and thus can be encapsulated into a single method.

PR-URL: #35727
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Ricky Zhou <[email protected]>
  • Loading branch information
yashLadha authored and gireeshpunathil committed Oct 24, 2020
1 parent fdaf9cb commit 4d54449
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ const { Symbol } = primordials;
const kDestroy = Symbol('kDestroy');
const kConstruct = Symbol('kConstruct');

function checkError(err, w, r) {
if (err) {
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
err.stack;

if (w && !w.errored) {
w.errored = err;
}
if (r && !r.errored) {
r.errored = err;
}
}
}

// Backwards compat. cb() is undocumented and unused in core but
// unfortunately might be used by modules.
function destroy(err, cb) {
Expand All @@ -24,20 +38,10 @@ function destroy(err, cb) {
return this;
}

if (err) {
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
err.stack;

if (w && !w.errored) {
w.errored = err;
}
if (r && !r.errored) {
r.errored = err;
}
}

// We set destroyed to true before firing error callbacks in order
// to make it re-entrance safe in case destroy() is called within callbacks
checkError(err, w, r);

if (w) {
w.destroyed = true;
Expand Down Expand Up @@ -66,17 +70,7 @@ function _destroy(self, err, cb) {

called = true;

if (err) {
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
err.stack;

if (w && !w.errored) {
w.errored = err;
}
if (r && !r.errored) {
r.errored = err;
}
}
checkError(err, w, r);

if (w) {
w.closed = true;
Expand Down

0 comments on commit 4d54449

Please sign in to comment.