Skip to content

Commit 21cae86

Browse files
committed
child_process: fix setSimultaneousAccepts
1 parent 5fbf33e commit 21cae86

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

lib/internal/child_process.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,10 @@ function setupChannel(target, channel, serializationMode) {
691691

692692
const obj = handleConversion[message.type];
693693

694-
// Update simultaneous accepts on Windows
695-
if (process.platform === 'win32') {
696-
handle.setSimultaneousAccepts(false);
694+
// Call setSimultaneousAccepts if it is a function
695+
// currently, it is only defined in tcp_wrap.cc and on win32
696+
if (obj.simultaneousAccepts && typeof handle.setSimultaneousAccepts === 'function') {
697+
handle.setSimultaneousAccepts(true);
697698
}
698699

699700
// Convert handle object
@@ -816,8 +817,11 @@ function setupChannel(target, channel, serializationMode) {
816817
if (!handle)
817818
message = message.msg;
818819

819-
// Update simultaneous accepts on Windows
820-
if (obj.simultaneousAccepts && process.platform === 'win32') {
820+
// Call setSimultaneousAccepts if it is a function
821+
// currently, it is only defined in tcp_wrap.cc and on win32
822+
if (handle &&
823+
obj.simultaneousAccepts &&
824+
typeof handle.setSimultaneousAccepts === 'function') {
821825
handle.setSimultaneousAccepts(true);
822826
}
823827
} else if (this._handleQueue &&

lib/net.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,8 @@ if (isWindows) {
18901890
process.env.NODE_MANY_ACCEPTS !== '0');
18911891
}
18921892

1893-
if (handle._simultaneousAccepts !== simultaneousAccepts) {
1893+
if (handle._simultaneousAccepts !== simultaneousAccepts &&
1894+
typeof handle.setSimultaneousAccepts === 'function') {
18941895
handle.setSimultaneousAccepts(!!simultaneousAccepts);
18951896
handle._simultaneousAccepts = simultaneousAccepts;
18961897
}

src/tcp_wrap.cc

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ void TCPWrap::SetSimultaneousAccepts(const FunctionCallbackInfo<Value>& args) {
207207
ASSIGN_OR_RETURN_UNWRAP(&wrap,
208208
args.Holder(),
209209
args.GetReturnValue().Set(UV_EBADF));
210+
if (wrap->provider_type() != ProviderType::PROVIDER_TCPSERVERWRAP) {
211+
return;
212+
}
210213
bool enable = args[0]->IsTrue();
211214
int err = uv_tcp_simultaneous_accepts(&wrap->handle_, enable);
212215
args.GetReturnValue().Set(err);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const child_process = require('child_process');
6+
const tmpdir = require('../common/tmpdir');
7+
8+
const CODE = `
9+
const net = require('net');
10+
process.on('message', (message, handle) => {
11+
// net.Socket or net.Server
12+
handle instanceof net.Socket ? handle.destroy() : handle.close();
13+
process.send('accepted');
14+
})
15+
`;
16+
17+
const child = child_process.fork(process.execPath, { execArgv: ['-e', CODE ] });
18+
19+
let tcpServer;
20+
let pipeServer;
21+
const sockets = [];
22+
let accepted = 0;
23+
24+
child.on('message', (message) => {
25+
assert.strictEqual(message, 'accepted');
26+
if (++accepted === 4) {
27+
child.kill();
28+
tcpServer.close();
29+
pipeServer.close();
30+
sockets.forEach((socket) => {
31+
socket.destroy();
32+
});
33+
}
34+
});
35+
36+
// Pipe
37+
tmpdir.refresh();
38+
pipeServer = net.createServer(common.mustCall((socket) => {
39+
child.send(null, socket);
40+
sockets.push(socket);
41+
}));
42+
pipeServer.listen(common.PIPE, common.mustCall(() => {
43+
net.connect(common.PIPE);
44+
child.send(null, pipeServer);
45+
}));
46+
47+
// TCP
48+
tcpServer = net.createServer(common.mustCall((socket) => {
49+
const setSimultaneousAccepts = socket._handle.setSimultaneousAccepts;
50+
if (typeof setSimultaneousAccepts === 'function') {
51+
socket._handle.setSimultaneousAccepts = common.mustCall((...args) => {
52+
const ret = setSimultaneousAccepts.call(socket._handle, ...args);
53+
assert.strictEqual(ret, undefined);
54+
});
55+
}
56+
child.send(null, socket._handle);
57+
sockets.push(socket);
58+
}));
59+
tcpServer.listen(0, common.mustCall(() => {
60+
net.connect(tcpServer.address().port);
61+
const setSimultaneousAccepts = tcpServer._handle.setSimultaneousAccepts;
62+
if (typeof setSimultaneousAccepts === 'function') {
63+
tcpServer._handle.setSimultaneousAccepts = common.mustCall((...args) => {
64+
const ret = setSimultaneousAccepts.call(tcpServer._handle, ...args);
65+
assert.strictEqual(ret, 0);
66+
});
67+
}
68+
child.send(null, tcpServer);
69+
}));

0 commit comments

Comments
 (0)