Skip to content

Commit

Permalink
Do not deadlock on empty chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Sep 2, 2023
1 parent 3904211 commit d039fea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ class DuplexSocket extends Duplex {
}

_write(chunk, encoding, callback) {
this[kOtherSide][kCallback] = callback;
this[kOtherSide].push(chunk);
if (chunk.length) {
this[kOtherSide][kCallback] = callback;
this[kOtherSide].push(chunk);
} else {
callback();
}
}

_final(callback) {
Expand Down
22 changes: 21 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});
});

0 comments on commit d039fea

Please sign in to comment.