Skip to content

Commit

Permalink
http: handle cases where socket.server is null
Browse files Browse the repository at this point in the history
Fixes a regression that caused an error to be thrown when trying to
emit the 'timeout' event on the server referenced by `socket.server`.

Fixes: #13435
Refs: #11926
  • Loading branch information
lpinca committed Jun 9, 2017
1 parent 27de369 commit 6f2e806
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ function connectionListener(socket) {

httpSocketSetup(socket);

// Ensure that the server property of the socket is correctly set.
// See https://github.com/nodejs/node/issues/13435
if (socket.server === null)
socket.server = this;

// If the user has added a listener to the server,
// request, or response, then it's their responsibility.
// otherwise, destroy on timeout by default
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-regress-GH-13435.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const http = require('http');
const net = require('net');

if (cluster.isMaster) {
const worker = cluster.fork();
const server = net.createServer({
pauseOnConnect: true
}, common.mustCall((socket) => {
worker.send('socket', socket);
}));

worker.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
server.close();
}));

server.listen(0, common.mustCall(() => {
net.createConnection(server.address().port);
}));
} else {
const server = http.createServer();

server.setTimeout(100, common.mustCall((socket) => {
socket.destroy();
cluster.worker.kill();
}));

process.on('message', common.mustCall((message, socket) => {
server.emit('connection', socket);
socket.resume();
}));
}

0 comments on commit 6f2e806

Please sign in to comment.