From 7cc4dbf4d2f76d4c540182414c78a1ec77e78810 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Thu, 10 Aug 2023 02:03:23 +0200 Subject: [PATCH] Fix cluster NotImplementedError when running SVR.JS on newer versions of Bun. --- svr.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/svr.js b/svr.js index 9bb0e4c..0468e63 100644 --- a/svr.js +++ b/svr.js @@ -146,8 +146,10 @@ if (!singlethreaded) { } // Cluster & IPC shim for Bun - if (process.isBun && cluster.isMaster === undefined) { + + cluster.bunShim = function () { cluster.isMaster = !process.env.NODE_UNIQUE_ID; + cluster.isPrimary = cluster.isMaster; cluster.isWorker = !cluster.isMaster; if (cluster.isWorker) { @@ -274,6 +276,10 @@ if (!singlethreaded) { }; } + if (process.isBun && cluster.isMaster === undefined) { + cluster.bunShim(); + } + // Shim cluster.isPrimary field if (cluster.isPrimary === undefined && cluster.isMaster !== undefined) cluster.isPrimary = cluster.isMaster; } @@ -298,7 +304,18 @@ function SVRJSFork() { //Log if (SVRJSInitialized) serverconsole.locmessage("Starting next thread, because previous one hung up/crashed..."); //Fork new worker - var newWorker = cluster.fork(); + var newWorker = {}; + try { + newWorker = cluster.fork(); + } catch (err) { + if(err.name == "NotImplementedError") { + // If cluster.fork throws a NotImplementedError, shim cluster module + cluster.bunShim(); + newWorker = cluster.fork(); + } 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); });