Does not work with clustering. #304
-
When a child binds its port the other children fail const cluster = require("cluster");
const numCPUs = require("os").cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
console.log("Master starting " + new Date().toISOString());
cluster.on("exit", () => {
process.exit(1);
});
} else {
// worker task
require("./server");
}
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
It does work with clustering, but focus lies on WorkerThreads: https://github.com/uNetworking/uWebSockets.js/blob/master/examples/WorkerThreads.js |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help, ill give it a shot |
Beta Was this translation helpful? Give feedback.
-
It seems to not work either. Any insight? @alexhultman const uWS = require('uWebSockets.js');
const port = 9001;
const { Worker, isMainThread, threadId } = require('worker_threads');
const os = require('os');
if (isMainThread) {
/* Main thread loops over all CPUs */
/* In this case we only spawn two (hardcoded) */
/*os.cpus()*/[0, 1].forEach(() => {
/* Spawn a new thread running this source file */
new Worker(__filename);
});
/* I guess main thread joins by default? */
} else {
/* Here we are inside a worker thread */
const app = uWS.App({
}).get('/*', (res, req) => {
res.end('Hello Worker!');
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port + ' from thread ' + threadId);
} else {
console.log('Failed to listen to port ' + port + ' from thread ' + threadId);
}
});
} $ node worker-threads.js
Listening to port 9001 from thread 1
Failed to listen to port 9001 from thread 2
uv loop at [0x700008fd0af8] has open handles:
[0x10a00a6d0] prepare (active)
Close callback: 0x128008000
Data: 0x10a00a570
(First field): 0x10a00a7d0
[0x10a00a750] check (active)
Close callback: 0x0
Data: 0x10a00a570
(First field): 0x10a00a7d0
[0x10a00a8a0] timer (active)
Close callback: 0x0
Data: 0x10a00a7d0
[0x10a00bdc0] async (active)
Close callback: 0x0
Data: 0x10a00bcf0
uv loop at [0x700008fd0af8] has 4 open handles in total
node[34486]: ../src/debug_utils.cc:322:void node::CheckedUvLoopClose(uv_loop_t *): Assertion `0 && "uv_loop_close() while having open handles"' failed.
1: 0x101224565 node::Abort() (.cold.1) [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
2: 0x10009d6d9 node::Abort() [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
3: 0x10009d541 node::Assert(node::AssertionInfo const&) [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
4: 0x10002d18e node::CheckedUvLoopClose(uv_loop_s*) [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
5: 0x100132976 node::worker::WorkerThreadData::~WorkerThreadData() [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
6: 0x10012f451 node::worker::Worker::Run() [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
7: 0x100132af9 node::worker::Worker::StartThread(v8::FunctionCallbackInfo<v8::Value> const&)::$_3::__invoke(void*) [/Users/jtwhissel/.nvm/versions/node/v14.1.0/bin/node]
8: 0x7fff6f023109 _pthread_start [/usr/lib/system/libsystem_pthread.dylib]
9: 0x7fff6f01eb8b thread_start [/usr/lib/system/libsystem_pthread.dylib]
Abort trap: 6 |
Beta Was this translation helpful? Give feedback.
-
From the path in your stack trace, it seems you're using macOS. Your example code omits two important lines: /* This example spawns two worker threads, each with their own
* server listening to the same port (Linux feature). */ Note the "Linux feature" parenthetical remark. @alexhultman can confirm, but I believe |
Beta Was this translation helpful? Give feedback.
From the path in your stack trace, it seems you're using macOS.
Your example code omits two important lines:
Note the "Linux feature" parenthetical remark.
@alexhultman can confirm, but I believe
SO_REUSEPORT
on Linux uniquely supports (as of 3.9) TCP and UDP sockets, whereasSO_REUSEPORT
on macOS only supports UDP sockets. macOS does not know what to do with two threads that request to be bound to the same TCP port.