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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:57:51 +09:00
co-authored by Claude Opus 5.5
parent e505a65c80
commit 9733ea8eaa
2 changed files with 42 additions and 0 deletions
@@ -427,6 +427,16 @@ export function openIndexedDbDatabase<Failure>(
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<IndexedDbAdmission>;
try {
admission = input.admit(database);
+32
View File
@@ -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<void>((resolve) => {
admitting = resolve;
});
let finishAdmission!: () => void;
const pending = openIndexedDbDatabase<TestFailure>({
factory: memory.factory,
databaseName: "kernel",
version: 1,
translate: recordingTranslate([]),
upgrade: () => ({ kind: "APPLIED" }),
admit: async () => {
admitting();
await new Promise<void>((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" } });
});
});