Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not deadlock on empty chunks #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 === 0) {
process.nextTick(callback);
} else {
this[kOtherSide].push(chunk);
this[kOtherSide][kCallback] = 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();
});
});