Report why the server exited from sendRcon

Waiting for exit alongside rcon-ready made sendRcon reject with the flat
"RCON connection lost" for every case, including the unexpected exits
that used to reject with the EnvironmentError saying why the server went
away, as events.once rejects on error events.

Have the paired wait capture the error emitted with the exit and hand it
back, so sendRcon throws that when there is one and falls through to the
old message when there is not, which is the kill path where no error is
emitted. stop() ignores the error as its job is done once the process is
gone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5 committed 2026-09-13 17:09:51 +00:00
1 parent 49e8d6ecc5
commit 229bbf8eb8
2 files changed
+55 -9

No files matched your search

+24 -8
View File
@@ -1318,18 +1318,29 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
}
// Not using events.once here to avoid throwing on error events, which are
// not emitted when the process exits while stopping.
// Not using events.once here to avoid throwing on error events, as they are
// not emitted when the process exits while stopping. Returns the error
// emitted with the exit, if there was one, for the caller to report.
async _waitForRconReadyOrExit() {
await new Promise<void>(resolve => {
const onRconReady = () => {
return await new Promise<Error | undefined>(resolve => {
let exitError: Error | undefined;
const cleanup = () => {
this.off("error", onError);
this.off("rcon-ready", onRconReady);
this.off("exit", onExit);
resolve();
};
const onError = (err: Error) => {
exitError = err;
};
const onRconReady = () => {
cleanup();
resolve(undefined);
};
const onExit = () => {
this.off("rcon-ready", onRconReady);
resolve();
cleanup();
resolve(exitError);
};
this.on("error", onError);
this.once("rcon-ready", onRconReady);
this.once("exit", onExit);
});
@@ -1350,7 +1361,11 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
async sendRcon(message: string, expectEmpty?: boolean) {
this._check(["running", "stopping"]);
if (!this._rconReady) {
await this._waitForRconReadyOrExit();
// Prefer the reason the server gave for exiting over the flat message below.
const exitError = await this._waitForRconReadyOrExit();
if (exitError) {
throw exitError;
}
}
if (!this._rconClient) {
throw new Error("RCON connection lost");
@@ -1421,6 +1436,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
// If RCON is not yet fully connected that operation needs to
// complete before the RCON connection can be used
if (!this._rconReady) {
// An exit error is not reported here as stopping is done once the process is gone.
await this._waitForRconReadyOrExit();
}