From 256b455315bba73bc3f0f2b0f8234813dd15505c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 23 Jan 2024 16:07:55 +0100 Subject: [PATCH] Fix host hang on stopping during create save If during the startup of an instance the Factorio server is started up to create a new save and the host shutdown in initiated then the instance will be commanded to stop while the FactorioServer state is "create". This was was not handled by Instance.stop leaving the child_process running after the host is done shutting down. Kill the FactorioServer on stop if the current state of it is stopping or create to make sure the server actually stops. --- CHANGELOG.md | 1 + packages/host/src/Instance.ts | 5 +++++ packages/host/src/server.ts | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa1c742..1bab77a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Fixed installer incorrectly claiming the options --factorio-dir and --no-download-headless conflicts with each other. - Fixed a memory leak on every request sent over the WebSocket. - Fixed assertion error on closing or invalidating a link with messages forwarded from a virtual link. +- Fixed host hanging if stopped while an instance is in the process of creating a save during startup. ## Version 2.0.0-alpha.14 diff --git a/packages/host/src/Instance.ts b/packages/host/src/Instance.ts index c4e3d473..98d4d0eb 100644 --- a/packages/host/src/Instance.ts +++ b/packages/host/src/Instance.ts @@ -955,6 +955,11 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); await lib.invokeHook(this.plugins, "onStop"); await this.server.stop(); await this.sendSaveListUpdate(); + } else if ( + this.server._state === "stopping" + || this.server._state === "create" + ) { + await this.server.kill(); } } diff --git a/packages/host/src/server.ts b/packages/host/src/server.ts index b34af3b9..60089036 100644 --- a/packages/host/src/server.ts +++ b/packages/host/src/server.ts @@ -865,7 +865,9 @@ export class FactorioServer extends events.EventEmitter { try { let [code, signal] = await events.once(this._server, "exit"); - void signal; + if (signal) { + throw new Error(`Factorio exited with signal ${signal}`); + } if (code !== 0) { throw new Error(`Factorio exited with status ${code}`); } @@ -1092,7 +1094,7 @@ export class FactorioServer extends events.EventEmitter { * process. */ async kill(unexpected = false) { - this._check(["running", "stopping"]); + this._check(["running", "stopping", "create"]); this._killed = true; if (!unexpected) { this._state = "stopping";