From e828bb917383331b66eae3732a82dbecec944add Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sun, 10 Sep 2023 19:30:29 +0200 Subject: [PATCH] Add warning about worker count limited to one when using Bun 1.0 and newer with shimmed (not native) clustering module. --- config.json | 2 +- svr.js | 48 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/config.json b/config.json index f52cc9f..4d3c590 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "port": 80, "pubport": 80, "page404": "404.html", - "timestamp": 1694365365939, + "timestamp": 1694366855651, "blacklist": [], "nonStandardCodes": [], "enableCompression": true, diff --git a/svr.js b/svr.js index e5979c3..7929ad2 100644 --- a/svr.js +++ b/svr.js @@ -151,6 +151,7 @@ if (!singlethreaded) { cluster.isMaster = !process.env.NODE_UNIQUE_ID; cluster.isPrimary = cluster.isMaster; cluster.isWorker = !cluster.isMaster; + cluster.__shimmed__ = true; if (cluster.isWorker) { // Shim the cluster.worker object for worker processes @@ -329,6 +330,7 @@ var SVRJSInitialized = false; var exiting = false; var reallyExiting = false; var crashed = false; +var threadLimitWarned = false; function SVRJSFork() { // Log @@ -336,27 +338,47 @@ function SVRJSFork() { // Fork new worker var newWorker = {}; try { - newWorker = cluster.fork(); + if (cluster.__shimmed__ && process.isBun && process.versions.bun && process.versions.bun[0] != "0" && !threadLimitWarned) { + threadLimitWarned = true; + serverconsole.locwarnmessage("SVR.JS limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer."); + } + if (!(cluster.__shimmed__ && process.isBun && process.versions.bun && process.versions.bun[0] != "0" && Object.keys(cluster.workers) > 0)) { + newWorker = cluster.fork(); + } else { + if (SVRJSInitialized) serverconsole.locwarnmessage("SVR.JS limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer."); + } } catch (err) { if(err.name == "NotImplementedError") { // If cluster.fork throws a NotImplementedError, shim cluster module cluster.bunShim(); - newWorker = cluster.fork(); + if (cluster.__shimmed__ && process.isBun && process.versions.bun && process.versions.bun[0] != "0" && !threadLimitWarned) { + threadLimitWarned = true; + serverconsole.locwarnmessage("SVR.JS limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer."); + } + if (!(cluster.__shimmed__ && process.isBun && process.versions.bun && process.versions.bun[0] != "0" && Object.keys(cluster.workers) > 0)) { + newWorker = cluster.fork(); + } else { + if (SVRJSInitialized) serverconsole.locwarnmessage("SVR.JS limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer."); + } } else { throw err; } } - newWorker.on("error", function (err) { - if(!reallyExiting) serverconsole.locwarnmessage("There was a problem when handling SVR.JS worker! (from master process side) Reason: " + err.message); - }); - newWorker.on("exit", function () { - if (!exiting && Object.keys(cluster.workers).length == 0) { - crashed = true; - SVRJSFork(); - } - }); - newWorker.on("message", bruteForceListenerWrapper(newWorker)); - newWorker.on("message", listenConnListener); + + // Add event listeners + if(newWorker.on) { + newWorker.on("error", function (err) { + if(!reallyExiting) serverconsole.locwarnmessage("There was a problem when handling SVR.JS worker! (from master process side) Reason: " + err.message); + }); + newWorker.on("exit", function () { + if (!exiting && Object.keys(cluster.workers).length == 0) { + crashed = true; + SVRJSFork(); + } + }); + newWorker.on("message", bruteForceListenerWrapper(newWorker)); + newWorker.on("message", listenConnListener); + } } var http = require("http");