Fix crash on race condition stopping Factorio server

If the server decides to exit on its own during the stop operation to
the FactorioServer class after the operation has started but before the
signal is sent to the process then the exit handler will have wiped the
server child process and cause the kill call to fail.  This in turn
would let the hang timer running which would bring down the Node.js
process due to an unhandled error for the same reason.

Fix by making sure the stop operation watches for the server exiting and
returning early if the server already quit before getting to the send
stop signal part.
This commit is contained in:
Hornwitser committed 2020-12-29 03:34:38 +01:00
1 parent 16525ae07c
commit aa3dfdbc0a
2 files changed
+53 -3

No files matched your search

+27
View File
@@ -5,6 +5,7 @@ const fs = require("fs-extra");
const path = require("path");
const libFactorio = require("@clusterio/lib/factorio");
const { wait } = require("@clusterio/lib/helpers");
const { testLines } = require("./lines");
@@ -243,5 +244,31 @@ describe("lib/factorio/server", function() {
assert(!await fs.pathExists(filePath), "File was not deleted");
});
});
describe(".stop()", function() {
it("should handle server quitting on its own during stop", async function() {
if (process.platform === "win32") {
this.skip();
}
server.hangTimeout = 20;
server._server = new events.EventEmitter();
server._server.kill = () => true;
server._state = "running";
server._rconReady = true;
server._rconClient = {
async end() {
server._rconClient = null;
process.nextTick(() => {
server.emit("_quitting");
server._server.emit("exit");
});
},
};
server._watchExit();
await server.stop();
await wait(21); // Wait until after hang timeout
});
});
});
});