Skip to content
Closed
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
src: unconsume stream fix
When emitting a 'connection' event on a httpServer, the function connectionListener is called.
Then, a new parser is created, and 'consume' method is called on the
socket's externalStream. However, if this stream was already consumed and unconsumed,
the process crashes with a cpp assert from the 'Consume' method in
stream_base.h. This commit makes sure that no SIGABRT will be raised
and the process will stay alive (after emitting the socket).
  • Loading branch information
Kasher committed Jan 29, 2017
commit 00a634097653a31e8762bcfcb3c2a8d754a83255
4 changes: 3 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,11 @@ function connectionListener(socket) {
// Override on to unconsume on `data`, `readable` listeners
socket.on = socketOnWrap;

// We only consume the socket if it has never been consumed before.
var external = socket._handle._externalStream;
if (external) {
if (!socket._handle._consumed && external) {
parser._consumed = true;
socket._handle._consumed = true;
parser.consume(external);
}
parser[kOnExecute] =
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-http-server-unconsume-consume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common');
const http = require('http');

const testServer = http.createServer((req, res) => {
common.fail('Should not be called');
res.end();
});
testServer.on('connect', common.mustCall((req, socket, head) => {
socket.write('HTTP/1.1 200 Connection Established' + '\r\n' +
'Proxy-agent: Node-Proxy' + '\r\n' +
'\r\n');
// This shouldn't raise an assertion in StreamBase::Consume.
testServer.emit('connection', socket);
testServer.close();
}));
testServer.listen(0, common.mustCall(() => {
http.request({
port: testServer.address().port,
method: 'CONNECT'
}, (res) => {}).end();
}));