Fix stop hanging if Factorio exits before RCON connects

stop() waits for the rcon-ready event when RCON has not connected yet,
but nothing emits it once the process is gone, so the promise never
settles and the stop request stays pending until the session closes.
Wait for exit as well and drop the other listener when either fires.

Repro: send InstanceExportDataRequest to a stopped instance, then
InstanceStopRequest while the status is exporting_data. Instance.stop()
sees the server as running and calls server.stop(), then exportData
kills the process before RCON is up. The export finishes and the
instance reports stopped, but the stop request never gets a response.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5 committed 2026-09-13 01:52:22 +00:00
1 parent f4aa894fe7
commit 073ddf1421
2 files changed
+75 -1

No files matched your search

+13 -1
View File
@@ -1405,7 +1405,19 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
// complete before the RCON connection can be used
if (!this._rconReady) {
// Not using events.once here to avoid throwing on error events.
await new Promise<void>(resolve => this.once("rcon-ready", resolve));
// The process may also exit before RCON connects.
await new Promise<void>(resolve => {
const onRconReady = () => {
this.off("exit", onExit);
resolve();
};
const onExit = () => {
this.off("rcon-ready", onRconReady);
resolve();
};
this.once("rcon-ready", onRconReady);
this.once("exit", onExit);
});
}
// The Factorio server may have decided to get ahead of us and