From 9733ea8eaaa898e42ae728852ab1aea96b9b9a2b Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:57:51 +0900 Subject: [PATCH] fix(storage): close an IndexedDB connection that gets versionchange during admission (05-13) The versionchange handler was installed only after the asynchronous admission finished, so another tab's upgrade that arrived in that window waited blocked until its deadline and failed. The kernel now installs a handler for the admission window that closes the connection and ends the open as CLOSED. Co-Authored-By: Claude Opus 5.5 --- src/adapters/platform/indexeddb-connection.ts | 10 ++++++ tests/unit/indexeddb-connection.test.ts | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/adapters/platform/indexeddb-connection.ts b/src/adapters/platform/indexeddb-connection.ts index b7476442..9ba02c16 100644 --- a/src/adapters/platform/indexeddb-connection.ts +++ b/src/adapters/platform/indexeddb-connection.ts @@ -427,6 +427,16 @@ export function openIndexedDbDatabase( settle(succeeded(database)); return; } + // 05-13. Admission is asynchronous and nothing else answers another + // context's upgrade until the caller adopts the connection, so that + // upgrade would wait blocked until its deadline. While admission runs, a + // versionchange closes this connection and ends the open as CLOSED. An + // `admit` that installs its own handler replaces this one. + database.onversionchange = () => { + if (settled) return; + closeQuietly(database); + settleFailure({ kind: "CLOSED" }); + }; let admission: IndexedDbAdmission | Promise; try { admission = input.admit(database); diff --git a/tests/unit/indexeddb-connection.test.ts b/tests/unit/indexeddb-connection.test.ts index 636f0bf5..d6b102fa 100644 --- a/tests/unit/indexeddb-connection.test.ts +++ b/tests/unit/indexeddb-connection.test.ts @@ -943,3 +943,35 @@ describe("IndexedDB database deletion", () => { }); }); }); + +describe("05-13 a versionchange that arrives while admission runs", () => { + it("closes the connection and ends the open instead of blocking the upgrade", async () => { + const memory = new MemoryIndexedDbFactory(); + let admitting!: () => void; + const admissionStarted = new Promise((resolve) => { + admitting = resolve; + }); + let finishAdmission!: () => void; + const pending = openIndexedDbDatabase({ + factory: memory.factory, + databaseName: "kernel", + version: 1, + translate: recordingTranslate([]), + upgrade: () => ({ kind: "APPLIED" }), + admit: async () => { + admitting(); + await new Promise((resolve) => { + finishAdmission = resolve; + }); + return { kind: "ADMIT" }; + }, + }); + await admissionStarted; + + // Another context asks to upgrade while this one is still admitting. + memory.triggerVersionChange(2); + expect(memory.isConnectionClosed()).toBe(true); + finishAdmission(); + expect(await pending).toEqual({ ok: false, error: { cause: "CLOSED" } }); + }); +});