Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
} = require('internal/errors');

const assert = require('internal/assert');
const { UV_EADDRINUSE } = internalBinding('uv');
const { fork } = require('child_process');
const path = require('path');
const EventEmitter = require('events');
Expand Down Expand Up @@ -274,7 +275,16 @@ function queryServer(worker, message) {
`${message.fd}` + (message.port === 0 ? `:${message.index}` : '');
const cachedHandle = handles.get(key);
let handle;
if (cachedHandle && !cachedHandle.has(worker)) {
if (cachedHandle) {
if (cachedHandle.has(worker)) {
send(worker, {
errno: UV_EADDRINUSE,
key,
ack: message.seq,
data: cachedHandle.data,
});
return;
}
handle = cachedHandle;
}

Expand Down
29 changes: 29 additions & 0 deletions test/sequential/test-cluster-listen-twice-same-port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a suggestion for the test: it passes on an unpatched build under SCHED_RR (the POSIX default), so it wouldn't catch a regression on Linux/macOS. The PR fixes behavior with SCHED_NONE, so could the tests cover both SCHED_RR and SCHED_NONE policies regardless of the OS?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I've covered both policies.

if (cluster.isPrimary) {
cluster.fork().on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));
return;
}

function listen() {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.once('error', reject);
server.once('listening', () => resolve(server));
server.listen({ host: '127.0.0.1', port: common.PORT });
});
}

(async () => {
const server1 = await listen();
await assert.rejects(listen(), { code: 'EADDRINUSE' });
await new Promise((resolve) => server1.close(resolve));
cluster.worker.disconnect();
})().then(common.mustCall());
Loading