mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
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.
This commit is contained in:
1 parent
e4aeafaf14
commit
10a857c4ad
2 files changed
+56
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user