stream: propagate destruction in duplexPair · nodejs/node@6f37f7e
@@ -50,13 +50,37 @@ class DuplexSide extends Duplex {
5050this.#otherSide.on('end', callback);
5151this.#otherSide.push(null);
5252}
53+54+55+_destroy(err, callback) {
56+const otherSide = this.#otherSide;
57+58+if (otherSide !== null && !otherSide.destroyed) {
59+// Use nextTick to avoid crashing the current execution stack (like HTTP parser)
60+process.nextTick(() => {
61+if (otherSide.destroyed) return;
62+63+if (err) {
64+// Destroy the other side, without passing the 'err' object.
65+// This closes the other side gracefully so it doesn't hang,
66+// but prevents the "Unhandled error" crash.
67+otherSide.destroy();
68+} else {
69+// Standard graceful close
70+otherSide.push(null);
71+}
72+});
73+}
74+75+callback(err);
76+}
5377}
54785579function duplexPair(options) {
5680const side0 = new DuplexSide(options);
5781const side1 = new DuplexSide(options);
5882side0[kInitOtherSide](side1);
5983side1[kInitOtherSide](side0);
60-return [ side0, side1 ];
84+return [side0, side1];
6185}
6286module.exports = duplexPair;