A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Web: an IndexedDB abort at commit fails the autosave instead of hanging it (#3167)
Eyal Golan06b1bbb05605
diff --git a/changelog.d/3136.md b/changelog.d/3136.mdnew file mode 100644index 000000000..7c3d456be--- /dev/null+++ b/changelog.d/3136.md@@ -0,0 +1,5 @@+### Fixed++- On Web, an autosave whose IndexedDB write fails at the last step, as a full disk or storage quota can make it, now+ says "Your game could not be saved." (Refs #3136). Before, that failure went unreported and the game showed nothing.+ Saves that succeed are reported as before.diff --git a/tools/tests/test_autosave_persistence.py b/tools/tests/test_autosave_persistence.pyindex d149b9ddd..cfad4b38c 100644--- a/tools/tests/test_autosave_persistence.py+++ b/tools/tests/test_autosave_persistence.py@@ -184,3 +184,109 @@ calls[1].callback(null);assert.deepEqual(messages[1], ["GameRoot", "OnSavePersistenceResult", "ok delete"]);assert.deepEqual(logs, []);''')+++# Unity 6000.3.25f1's IDBFS as shipped in the Web build's framework.js, cut down to what decides the callback: syncfs ends in+# IDBFS.reconcile, which opens one readwrite transaction, calls back on the first request error or on `complete`, and+# sets no `onabort`. Unity's queuePersist (same file) starts a flush from a 0 ms timer and restarts it on 'again'.+UNITY_IDBFS = r'''+const txs = [], timers = [], requestErrors = [], completions = [];+class FakeTransaction {+ constructor() { this.error = null; this.aborts = []; }+ addEventListener(type, listener) { if (type === "abort") this.aborts.push(listener); }+ complete() { this.oncomplete({ target: this }); }+ abort(error = null) { // a commit that fails after every put succeeded fires only `abort`+ this.error = error;+ for (const listener of this.aborts) listener({ target: this });+ if (this.onabort) this.onabort({ target: this });+ }+}+class FakeDatabase { transaction() { const tx = new FakeTransaction(); txs.push(tx); return tx; } }+const db = new FakeDatabase();+IDBFS.syncfs = function (target, populate, callback) {+ calls.push({ target, populate, callback });+ IDBFS.reconcile({ type: "local", entries: {} }, { type: "remote", db, entries: {} }, callback);+};+IDBFS.reconcile = function (src, dst, callback) {+ let errored = false;+ const transaction = (src.type === "remote" ? src.db : dst.db).transaction(["FILE_DATA"], "readwrite");+ function done(err) { if (err && !errored) { errored = true; return callback(err); } }+ transaction.onerror = e => { done(e.target.error); };+ transaction.oncomplete = () => { if (!errored) callback(null); };+ requestErrors.push(done); // storeRemoteEntry's try/catch reports a throwing put through done+};+function queuePersist() { // what a closed or renamed file under /idbfs calls+ function onPersistComplete(error) {+ completions.push(error);+ if (mount.idbPersistState === "again") startPersist(); else mount.idbPersistState = 0;+ }+ function startPersist() { mount.idbPersistState = "idb"; IDBFS.syncfs(mount, false, onPersistComplete); }+ if (!mount.idbPersistState) { mount.idbPersistState = "timer"; timers.push(startPersist); }+ else if (mount.idbPersistState === "idb") mount.idbPersistState = "again";+}+function autosave(value) { queuePersist(); save(value); } // SaveStore's rename queues the flush, then GameStart asks+'''+++def test_an_abort_at_commit_fails_the_save_and_releases_unitys_persist_queue():+ run(UNITY_IDBFS + r'''+autosave(one);+timers.shift()();+assert.equal(txs.length, 1);+txs[0].abort(); // a script abort leaves transaction.error null; a real quota abort carries QuotaExceededError+assert.deepEqual(errors, ["AUTOSAVE FAIL " + one]);+assert.deepEqual(messages, [["UI", "OnSavePersistenceResult", "fail save " + one]]);+assert.equal(completions.length, 1);+assert.ok(completions[0] instanceof Error);+assert.equal(mount.idbPersistState, 0); // without a callback Unity stays at 'idb' and never flushes again+autosave(two);+assert.equal(timers.length, 1);+timers.shift()();+txs[1].complete();+assert.deepEqual(logs, ["AUTOSAVE OK " + two]);+assert.deepEqual(messages[1], ["UI", "OnSavePersistenceResult", "ok save " + two]);+assert.equal(Object.prototype.hasOwnProperty.call(db, "transaction"), false); // the database is left as found+''')+++def test_an_abort_carries_the_transactions_own_error_and_a_queued_followup_still_runs():+ run(UNITY_IDBFS + r'''+autosave(one);+timers.shift()();+autosave(two); // written while the first snapshot is in flight: Unity marks 'again'+const quota = new Error("QuotaExceededError");+txs[0].abort(quota);+assert.deepEqual(errors, ["AUTOSAVE FAIL " + one]);+assert.deepEqual(completions, [quota]);+assert.equal(txs.length, 2); // Unity's 'again' restarted the flush for the newer bytes+txs[1].complete();+assert.deepEqual(logs, ["AUTOSAVE OK " + two]);+assert.deepEqual(completions, [quota, null]);+''')+++def test_an_abort_after_idbfs_already_failed_the_flush_does_not_call_back_twice():+ run(UNITY_IDBFS + r'''+autosave(one);+timers.shift()();+const thrown = new Error("put threw");+requestErrors[0](thrown); // the path that already worked: a throwing put calls back at once+txs[0].abort(thrown); // then the transaction aborts as well+assert.deepEqual(completions, [thrown]);+assert.deepEqual(errors, ["AUTOSAVE FAIL " + one]);+assert.equal(messages.length, 1);+''')+++def test_a_committed_flush_reports_once_and_a_late_abort_listener_stays_silent():+ run(UNITY_IDBFS + r'''+autosave(one);+timers.shift()();+txs[0].complete();+assert.deepEqual(logs, ["AUTOSAVE OK " + one]);+assert.deepEqual(completions, [null]);+assert.equal(txs[0].aborts.length, 1);+txs[0].aborts[0]({ target: txs[0] }); // IndexedDB never fires both, but a stray abort must not add a FAIL+assert.deepEqual(errors, []);+assert.deepEqual(completions, [null]);+''')diff --git a/unity/Assets/Scripts/UI/AutosavePersistence.jslib b/unity/Assets/Scripts/UI/AutosavePersistence.jslibindex 09971cbb6..2573af822 100644--- a/unity/Assets/Scripts/UI/AutosavePersistence.jslib+++ b/unity/Assets/Scripts/UI/AutosavePersistence.jslib@@ -42,6 +42,37 @@ mergeInto(LibraryManager.library, {}});};+ // Unity 6000.3's IDBFS.reconcile calls back on a request error or on `complete`, but sets no `onabort`. A write+ // transaction that aborts at commit (quota or disk failure after every put succeeded) then never called back: no+ // FAIL, and Unity's persist queue stayed in 'idb' (#3136). IndexedDB fires exactly one of `complete` and `abort`,+ // so report an abort as the error unless IDBFS has already called back.+ var reconcile = IDBFS.reconcile;+ if (typeof reconcile === "function") {+ IDBFS.reconcile = function (src, dst, callback) {+ var db = src.type === "remote" ? src.db : dst.db;+ if (!db) return reconcile.call(this, src, dst, callback);+ var called = false;+ function once(error) {+ if (called) return;+ called = true;+ callback(error);+ }+ // reconcile opens its one transaction synchronously; see it without replacing IDBFS's own handlers.+ var open = db.transaction, own = Object.prototype.hasOwnProperty.call(db, "transaction");+ db.transaction = function () {+ var transaction = open.apply(this, arguments);+ transaction.addEventListener("abort", function () {+ once(transaction.error || new Error("IndexedDB transaction aborted"));+ });+ return transaction;+ };+ try { return reconcile.call(this, src, dst, once); }+ finally {+ if (own) db.transaction = open;+ else delete db.transaction;+ }+ };+ }saves.installed = true;}// Multiple writes before the queued flush overwrite the same autosave path: only its latest bytes can be durable.