From 3eff85a3d32c1f4ce4e7960fc1cfc0014a6b8467 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 21 Jul 2024 11:23:49 +0100 Subject: [PATCH] error out when we can't spawn workers - fixes #586 Setting `clusterLimit` to 0 means no workers are started, which usually breaks things. Also, some "hardening" things may prevent node from seeing how many CPUs the machine has, which has the same effect. With this commit we provide hopefully-useful error messages. --- packages/backend/src/boot/master.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index 303ba94207..879249e231 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -180,7 +180,16 @@ async function connectDb(): Promise { */ async function spawnWorkers(limit = 1) { - const workers = Math.min(limit, os.cpus().length); + const cpuCount = os.cpus().length; + const workers = Math.min(limit, cpuCount); + if (workers === 0) { + const cause = cpuCount === 0 + ? 'you seem to have no CPUs (this may be caused by some "hardening" system)' + : "`config.clusterLimit` is 0 (if you don't want to use clustering, set the environment variable `MK_DISABLE_CLUSTERING` to a non-empty value instead)"; + bootLogger.error(`Configuration error: we can't create workers, ${cause}`, null, true); + process.exit(1); + } + bootLogger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`); await Promise.all([...Array(workers)].map(spawnWorker)); bootLogger.succ('All workers started');