From 8b5a8cf371f290ffd93c20042b9aa94f7e67ae4f Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 2 Sep 2023 17:10:11 +0200 Subject: [PATCH] Do not deadlock on empty chunks Refs: https://github.com/nodejs/node/issues/29758 Refs: https://github.com/nodejs/node/commit/f58e8eb103ff3087 --- index.js | 8 ++++++-- test/index.js | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c0f1459..a5551aa 100644 --- a/index.js +++ b/index.js @@ -20,8 +20,12 @@ class DuplexSocket extends Duplex { } _write(chunk, encoding, callback) { - this[kOtherSide][kCallback] = callback; - this[kOtherSide].push(chunk); + if (chunk.length === 0) { + process.nextTick(callback); + } else { + this[kOtherSide].push(chunk); + this[kOtherSide][kCallback] = callback; + } } _final(callback) { diff --git a/test/index.js b/test/index.js index f5b800f..3150524 100644 --- a/test/index.js +++ b/test/index.js @@ -4,7 +4,7 @@ const DuplexPair = require('../'); const assert = require('assert'); describe('DuplexPair', function() { - it('passed data through', function() { + it('passes data through', function() { const pair = new DuplexPair({ encoding: 'utf8' }); pair.socket1.write('Hello'); assert.strictEqual(pair.socket1.read(), null); @@ -18,4 +18,24 @@ describe('DuplexPair', function() { pair.socket2.end(); assert.strictEqual(pair.socket1.read(), null); }); + + it('does not deadlock when writing empty chunks', function(done) { + const pair = new DuplexPair({ encoding: 'utf8' }); + + pair.socket2.resume(); + pair.socket2.on('end', function() { + pair.socket2.write('Hello'); + pair.socket2.write(''); + pair.socket2.end(); + }); + + pair.socket1.on('data', function(chunk) { + assert.strictEqual(chunk, 'Hello'); + }); + pair.socket1.on('end', function() { + done(); + }); + + pair.socket1.end(); + }); });