Reset crash restart attempts on a manual start

A stop is always followed by a start, so the start is the one place that
needs it. The crash restart calls _startSave directly to keep its count.
Rename _runningSinceMs to _startedAtMs to match InstanceDetails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-21 16:29:03 +00:00
1 parent 565e931a96
commit 079340e5dd
5 files changed
+19 -16

No files matched your search

+1 -1
View File
@@ -560,7 +560,7 @@ Defaults to false.
### instance.crash_retry_attempts ### instance.crash_retry_attempts
Number of times the host restarts this instance after a crash before giving up and logging an error. Number of times the host restarts this instance after a crash before giving up and logging an error.
The count resets when the instance is stopped or restarted by a user, or the host is restarted. The count resets when a user starts or restarts the instance, or the host is restarted.
Set to 0 for no limit. Set to 0 for no limit.
Defaults to 3. Defaults to 3.
+1 -1
View File
@@ -351,7 +351,7 @@ export default class Host extends lib.Link {
_shuttingDown = false; _shuttingDown = false;
/** Instances waiting on {@link Instance.fromConfig} */ /** Instances waiting on {@link Instance.fromConfig} */
_startingInstances = new Set<number>(); _startingInstances = new Set<number>();
/** Restarts after a crash per instance ID since the last manual stop */ /** Restarts after a crash per instance ID since the last manual start */
_crashRetryAttempts = new Map<number, number>(); _crashRetryAttempts = new Map<number, number>();
startedAtMs = Date.now(); startedAtMs = Date.now();
+10 -8
View File
@@ -197,7 +197,7 @@ export default class Instance extends lib.Link {
_playerAutosaveSlot = 1; _playerAutosaveSlot = 1;
_expectedUserUpdates: { action: string, name: string, reason: string }[] = []; _expectedUserUpdates: { action: string, name: string, reason: string }[] = [];
_settleWaiters: (() => void)[] = []; _settleWaiters: (() => void)[] = [];
_runningSinceMs: number | null = null; _startedAtMs: number | null = null;
_killRequested = false; _killRequested = false;
constructor( constructor(
@@ -600,7 +600,7 @@ end`.replace(/\r?\n/g, " ");
notifyStatus(status: Instance["_status"]) { notifyStatus(status: Instance["_status"]) {
this._status = status; this._status = status;
if (status === "running") { if (status === "running") {
this._runningSinceMs = Date.now(); this._startedAtMs = Date.now();
} }
if (!transitoryStatuses.has(status)) { if (!transitoryStatuses.has(status)) {
const waiters = this._settleWaiters; const waiters = this._settleWaiters;
@@ -679,7 +679,7 @@ end`.replace(/\r?\n/g, " ");
this._saveStats().catch(err => this.logger.error(`Error saving stats:\n${err.stack}`)); this._saveStats().catch(err => this.logger.error(`Error saving stats:\n${err.stack}`));
if (crashed && this.config.get("instance.restart_after_crash") && !this._host._shuttingDown) { if (crashed && this.config.get("instance.restart_after_crash") && !this._host._shuttingDown) {
this._restartAfterCrash(Date.now() - this._runningSinceMs!); this._restartAfterCrash(Date.now() - this._startedAtMs!);
} }
} }
@@ -698,7 +698,7 @@ end`.replace(/\r?\n/g, " ");
const of = limit > 0 ? ` of ${limit}` : ""; const of = limit > 0 ? ` of ${limit}` : "";
this.logger.warn(`Restarting instance ${this.name} after crash, attempt ${attempts + 1}${of}`); this.logger.warn(`Restarting instance ${this.name} after crash, attempt ${attempts + 1}${of}`);
this._host._connectInstance(this.id) this._host._connectInstance(this.id)
.then(connection => connection.instance.handleInstanceStartRequest(new lib.InstanceStartRequest())) .then(connection => connection.instance._startSave())
.catch(err => this.logger.error(`Error restarting instance ${this.name} after crash:\n${err.stack}`)); .catch(err => this.logger.error(`Error restarting instance ${this.name} after crash:\n${err.stack}`));
} }
@@ -1428,7 +1428,11 @@ end`.replace(/\r?\n/g, " ");
} }
async handleInstanceStartRequest(request: lib.InstanceStartRequest) { async handleInstanceStartRequest(request: lib.InstanceStartRequest) {
let saveName = request.save; this._host._crashRetryAttempts.delete(this.id);
await this._startSave(request.save);
}
async _startSave(saveName?: string) {
if (saveName !== undefined) { if (saveName !== undefined) {
checkRequestSaveName(saveName); checkRequestSaveName(saveName);
} }
@@ -1457,6 +1461,7 @@ end`.replace(/\r?\n/g, " ");
} }
async handleInstanceLoadScenarioRequest(request: lib.InstanceLoadScenarioRequest) { async handleInstanceLoadScenarioRequest(request: lib.InstanceLoadScenarioRequest) {
this._host._crashRetryAttempts.delete(this.id);
if (this.config.get("factorio.enable_save_patching")) { if (this.config.get("factorio.enable_save_patching")) {
this.notifyExit(); this.notifyExit();
throw new lib.ConflictError("Load scenario cannot be used with save patching enabled"); throw new lib.ConflictError("Load scenario cannot be used with save patching enabled");
@@ -1573,7 +1578,6 @@ end`.replace(/\r?\n/g, " ");
} }
async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) { async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) {
this._host._crashRetryAttempts.delete(this.id);
await this._stopWhenSettled(); await this._stopWhenSettled();
// Stopping invalidates this instance object, a new one must be created by the host // Stopping invalidates this instance object, a new one must be created by the host
// To do this we simulate a waking an instance and sending a start request // To do this we simulate a waking an instance and sending a start request
@@ -1582,12 +1586,10 @@ end`.replace(/\r?\n/g, " ");
} }
async handleInstanceStopRequest() { async handleInstanceStopRequest() {
this._host._crashRetryAttempts.delete(this.id);
await this._stopWhenSettled(); await this._stopWhenSettled();
} }
async handleInstanceKillRequest() { async handleInstanceKillRequest() {
this._host._crashRetryAttempts.delete(this.id);
await this.kill(); await this.kill();
} }
+1 -1
View File
@@ -641,7 +641,7 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
initialValue: false, initialValue: false,
}, },
"instance.crash_retry_attempts": { "instance.crash_retry_attempts": {
description: "Restarts after a crash before giving up, 0 for no limit. Reset when the instance is stopped", description: "Restarts after a crash before giving up, 0 for no limit. Reset when the instance is started",
type: "number", type: "number",
initialValue: 3, initialValue: 3,
}, },
+6 -5
View File
@@ -321,13 +321,13 @@ describe("class Instance", function() {
instance._saveStats = async () => {}; instance._saveStats = async () => {};
instance._host._connectInstance = async id => { instance._host._connectInstance = async id => {
starts.push(id); starts.push(id);
return { instance: { handleInstanceStartRequest: async () => {} } }; return { instance: { _startSave: async () => {} } };
}; };
instance.config.set("instance.restart_after_crash", true); instance.config.set("instance.restart_after_crash", true);
}); });
function crash(uptimeMinutes) { function crash(uptimeMinutes) {
instance.notifyStatus("running"); instance.notifyStatus("running");
instance._runningSinceMs = Date.now() - uptimeMinutes * 60e3; instance._startedAtMs = Date.now() - uptimeMinutes * 60e3;
instance.notifyExit(); instance.notifyExit();
} }
@@ -360,7 +360,7 @@ describe("class Instance", function() {
it("should not restart after a stop or kill", async function() { it("should not restart after a stop or kill", async function() {
instance.server.kill = async () => {}; instance.server.kill = async () => {};
instance.notifyStatus("running"); instance.notifyStatus("running");
instance._runningSinceMs = 0; instance._startedAtMs = 0;
await instance.handleInstanceKillRequest(); await instance.handleInstanceKillRequest();
instance.notifyExit(); instance.notifyExit();
instance.notifyStatus("stopping"); instance.notifyStatus("stopping");
@@ -373,10 +373,11 @@ describe("class Instance", function() {
crash(11); crash(11);
assert.deepEqual(starts, []); assert.deepEqual(starts, []);
}); });
it("should reset the attempts on a manual stop", async function() { it("should reset the attempts on a manual start", async function() {
instance.config.set("instance.crash_retry_attempts", 1); instance.config.set("instance.crash_retry_attempts", 1);
instance._startSave = async () => {};
crash(11); crash(11);
await instance.handleInstanceStopRequest(); await instance.handleInstanceStartRequest(new lib.InstanceStartRequest());
crash(11); crash(11);
assert.equal(starts.length, 2); assert.equal(starts.length, 2);
}); });