0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00

http: handle cases where socket.server is null

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: https://github.com/nodejs/node/issues/13435
Refs: https://github.com/nodejs/node/pull/11926
PR-URL: https://github.com/nodejs/node/pull/13578
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Luigi Pinca 2017-06-09 17:29:50 +02:00 committed by Anna Henningsen
parent 12fd63d6bb
commit ba449f3bf1
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
2 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,39 @@
'use strict';
// Regression test for https://github.com/nodejs/node/issues/13435
// Tests that `socket.server` is correctly set when a socket is sent to a worker
// and the `'connection'` event is emitted manually on an HTTP server.
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(common.mustCall((socket) => {
worker.send('socket', socket);
}));
worker.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
server.close();
}));
server.listen(0, common.mustCall(() => {
net.createConnection(server.address().port);
}));
} else {
const server = http.createServer();
server.on('connection', common.mustCall((socket) => {
assert.strictEqual(socket.server, server);
socket.destroy();
cluster.worker.disconnect();
}));
process.on('message', common.mustCall((message, socket) => {
server.emit('connection', socket);
}));
}