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

http: handle cases where socket.server is null #13578

Closed
wants to merge 2 commits into from
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
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
39 changes: 39 additions & 0 deletions test/parallel/test-cluster-send-socket-to-worker-http-server.js
Original file line number Diff line number Diff line change
@@ -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);
}));
}