Skip to content

Commit fff8a56

Browse files
lpincaaddaleax
authored andcommitted
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: #13435 Refs: #11926 PR-URL: #13578 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 38a1cfb commit fff8a56

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/_http_server.js

+5
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ function connectionListener(socket) {
291291

292292
httpSocketSetup(socket);
293293

294+
// Ensure that the server property of the socket is correctly set.
295+
// See https://github.com/nodejs/node/issues/13435
296+
if (socket.server === null)
297+
socket.server = this;
298+
294299
// If the user has added a listener to the server,
295300
// request, or response, then it's their responsibility.
296301
// otherwise, destroy on timeout by default
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
// Regression test for https://github.com/nodejs/node/issues/13435
4+
// Tests that `socket.server` is correctly set when a socket is sent to a worker
5+
// and the `'connection'` event is emitted manually on an HTTP server.
6+
7+
const common = require('../common');
8+
const assert = require('assert');
9+
const cluster = require('cluster');
10+
const http = require('http');
11+
const net = require('net');
12+
13+
if (cluster.isMaster) {
14+
const worker = cluster.fork();
15+
const server = net.createServer(common.mustCall((socket) => {
16+
worker.send('socket', socket);
17+
}));
18+
19+
worker.on('exit', common.mustCall((code) => {
20+
assert.strictEqual(code, 0);
21+
server.close();
22+
}));
23+
24+
server.listen(0, common.mustCall(() => {
25+
net.createConnection(server.address().port);
26+
}));
27+
} else {
28+
const server = http.createServer();
29+
30+
server.on('connection', common.mustCall((socket) => {
31+
assert.strictEqual(socket.server, server);
32+
socket.destroy();
33+
cluster.worker.disconnect();
34+
}));
35+
36+
process.on('message', common.mustCall((message, socket) => {
37+
server.emit('connection', socket);
38+
}));
39+
}

0 commit comments

Comments
 (0)