From 10a857c4adf7bbe1b1cc6007928f203e98020b21 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sat, 14 Sep 2024 16:09:17 +0200 Subject: [PATCH] Gracefully handle RCON connection dropping Handle the edge cases of RCON authentication failing and RCON connection failing during startup so that server stop still works in these cases. --- packages/host/src/server.ts | 32 ++++++++++++++++++++++++++++---- test/integration/server.js | 33 ++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/packages/host/src/server.ts b/packages/host/src/server.ts index e34a7d48..626dc644 100644 --- a/packages/host/src/server.ts +++ b/packages/host/src/server.ts @@ -700,16 +700,39 @@ export class FactorioServer extends events.EventEmitter { }; this._rconClient = new Rcon(config); - this._rconClient.on("error", () => { /* Ignore */ }); - this._rconClient.on("authenticated", () => { this._rconReady = true; this.emit("rcon-ready"); }); + this._rconClient.on("error", () => { + // Errors before being authenticated might not emit end + this._rconClient = null; + if (!this._rconReady) { + this._rconReady = true; + this.emit("rcon-ready"); + } + }); + this._rconClient.on("authenticated", () => { + this._rconReady = true; + this.emit("rcon-ready"); + }); this._rconClient.on("end", () => { this._rconClient = null; + if (!this._rconReady) { + this._rconReady = true; + this.emit("rcon-ready"); + } }); // XXX: Workaround to suppress bogus event listener warning if (this._rconClient.emitter instanceof events.EventEmitter) { this._rconClient.emitter.setMaxListeners(this.maxConcurrentCommands + 5); } - await this._rconClient.connect(); + try { + await this._rconClient.connect(); + } catch (err: any) { + this._logger.error(`Failed to start RCON connection:\n${err?.stack ?? err?.message ?? err}`); + this._rconClient = null; + if (!this._rconReady) { + this._rconReady = true; + this.emit("rcon-ready"); + } + } } /** Maximum number of RCON commands transmitted in parallel on the RCON connection */ @@ -1081,7 +1104,8 @@ export class FactorioServer extends events.EventEmitter { // If RCON is not yet fully connected that operation needs to // complete before the RCON connection can be used if (!this._rconReady) { - await events.once(this, "rcon-ready"); + // Not using events.once here to avoid throwing on error events. + await new Promise(resolve => this.once("rcon-ready", resolve)); } // The Factorio server may have decided to get ahead of us and diff --git a/test/integration/server.js b/test/integration/server.js index 074a1de3..1035c16e 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -233,7 +233,7 @@ describe("Integration of host/src/server", function() { await server.stop(); server.shutdownTimeoutMs = 0; }); - it("should not hang if RCON fails to connect", async function() { + it("should not hang if RCON fails to connect while stopping", async function() { slowTest(this); const origWaitForReady = server._waitForReady; @@ -247,14 +247,36 @@ describe("Integration of host/src/server", function() { if (server._rconReady) { assert.fail("Test failed, RCON managed to connect"); } - await assert.rejects( - server.stop(), - { code: "ECONNREFUSED" } - ); + await server.stop(); } finally { if (server._state !== "init") { await server.kill(); + assert.fail("Server did not stop"); + } + } + }); + it("should not hang if RCON fails to connect before stopping", async function() { + slowTest(this); + + const origWaitForReady = server._waitForReady; + server._waitForReady = () => { + server._waitForReady = origWaitForReady; + server.rconPassword = "wrong"; // Mangle password so it fails to authenticate. + return server._waitForReady(); + }; + try { + await server.start("test.zip"); + if (server._rconReady) { + assert.fail("Test failed, RCON managed to connect"); + } + await events.once(server, "rcon-ready"); + await server.stop(); + + } finally { + if (server._state !== "init") { + await server.kill(); + assert.fail("Server did not stop"); } } }); @@ -272,6 +294,7 @@ describe("Integration of host/src/server", function() { } finally { if (server._state !== "init") { await server.kill(); + assert.fail("Server did not stop"); } } });