Skip to content

Commit d039fea

Browse files
committed
Do not deadlock on empty chunks
Refs: nodejs/node#29758 Refs: nodejs/node@f58e8eb103ff3087
1 parent 3904211 commit d039fea

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ class DuplexSocket extends Duplex {
2020
}
2121

2222
_write(chunk, encoding, callback) {
23-
this[kOtherSide][kCallback] = callback;
24-
this[kOtherSide].push(chunk);
23+
if (chunk.length) {
24+
this[kOtherSide][kCallback] = callback;
25+
this[kOtherSide].push(chunk);
26+
} else {
27+
callback();
28+
}
2529
}
2630

2731
_final(callback) {

test/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const DuplexPair = require('../');
44
const assert = require('assert');
55

66
describe('DuplexPair', function() {
7-
it('passed data through', function() {
7+
it('passes data through', function() {
88
const pair = new DuplexPair({ encoding: 'utf8' });
99
pair.socket1.write('Hello');
1010
assert.strictEqual(pair.socket1.read(), null);
@@ -18,4 +18,24 @@ describe('DuplexPair', function() {
1818
pair.socket2.end();
1919
assert.strictEqual(pair.socket1.read(), null);
2020
});
21+
22+
it('does not deadlock when writing empty chunks', function(done) {
23+
const pair = new DuplexPair({ encoding: 'utf8' });
24+
25+
pair.socket2.resume();
26+
pair.socket2.on('end', function() {
27+
pair.socket2.write('Hello');
28+
pair.socket2.write('');
29+
pair.socket2.end();
30+
});
31+
32+
pair.socket1.on('data', function(chunk) {
33+
assert.strictEqual(chunk, 'Hello');
34+
});
35+
pair.socket1.on('end', function() {
36+
done();
37+
});
38+
39+
pair.socket1.end();
40+
});
2141
});

0 commit comments

Comments
 (0)