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

stream: don't deadlock duplexpair #29836

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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 lib/internal/streams/duplexpair.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
8 changes: 6 additions & 2 deletions test/common/duplexpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ class DuplexSocket extends Duplex {
_write(chunk, encoding, callback) {
assert.notStrictEqual(this[kOtherSide], null);
assert.strictEqual(this[kOtherSide][kCallback], null);
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
80 changes: 80 additions & 0 deletions test/parallel/test-http-generic-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const MakeDuplexPair = require('../common/duplexpair');
res.on('data', common.mustCall((data) => {
assert.strictEqual(data, testData);
}));
res.on('end', common.mustCall());
}));
req.end();
}
Expand Down Expand Up @@ -58,3 +59,82 @@ const MakeDuplexPair = require('../common/duplexpair');
doRequest();
});
}

// Test 3: Connection: close request/response with chunked
{
const testData = 'Hello, World!\n';
const server = http.createServer(common.mustCall((req, res) => {
req.setEncoding('utf8');
req.resume();
req.on('data', common.mustCall(function test3_req_data(data) {
assert.strictEqual(data, testData);
}));
req.once('end', function() {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.write(testData);
res.end();
});
}));

const { clientSide, serverSide } = MakeDuplexPair();
server.emit('connection', serverSide);
clientSide.on('end', common.mustCall());
serverSide.on('end', common.mustCall());

const req = http.request({
createConnection: common.mustCall(() => clientSide),
method: 'PUT',
headers: { 'Connection': 'close' }
}, common.mustCall((res) => {
res.setEncoding('utf8');
res.on('data', common.mustCall(function test3_res_data(data) {
assert.strictEqual(data, testData);
}));
res.on('end', common.mustCall());
}));
req.write(testData);
req.end();
}

// Test 4: Connection: close request/response with Content-Length
// The same as Test 3, but with Content-Length headers
{
const testData = 'Hello, World!\n';
const server = http.createServer(common.mustCall((req, res) => {
assert.strictEqual(req.headers['content-length'], testData.length + '');
req.setEncoding('utf8');
req.on('data', common.mustCall(function test4_req_data(data) {
assert.strictEqual(data, testData);
}));
req.once('end', function() {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', testData.length);
res.write(testData);
res.end();
});

}));

const { clientSide, serverSide } = MakeDuplexPair();
server.emit('connection', serverSide);
clientSide.on('end', common.mustCall());
serverSide.on('end', common.mustCall());

const req = http.request({
createConnection: common.mustCall(() => clientSide),
method: 'PUT',
headers: { 'Connection': 'close' }
}, common.mustCall((res) => {
res.setEncoding('utf8');
assert.strictEqual(res.headers['content-length'], testData.length + '');
res.on('data', common.mustCall(function test4_res_data(data) {
assert.strictEqual(data, testData);
}));
res.on('end', common.mustCall());
}));
req.setHeader('Content-Length', testData.length);
req.write(testData);
req.end();
}