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" } }); + }); +});