fix(storage): let fail() win over succeed() and abort late upgrades (Q-6.5-10)

Two small holes in the IndexedDB kernel (src/adapters/platform/):

- runIndexedDbTransaction: a `fail(x)` after `succeed()` kept the success
  candidate, so the abort it triggered was reported as an anonymous
  NATIVE_EXCEPTION and `x` was lost. `fail` now replaces a success
  candidate; an earlier failure still wins.
- openIndexedDbDatabase: when the open had already settled (caller abort,
  terminal BLOCKED, blocked deadline, close) before `onupgradeneeded`
  fired, the upgrade still ran and committed the migration. The handler now
  aborts the versionchange transaction when the open is already settled.

Tests that encoded the old behavior were updated in this commit:
tests/unit/indexeddb-transaction.test.ts ("an abort after succeed...")
expected NATIVE_EXCEPTION and now expects the caller's failure, and
tests/unit/indexeddb-runtime.test.ts ("times out a blocked upgrade...")
expected the late upgrade to commit and its connection to be closed; it now
expects no schema commit. Both fail without the fix.

Verified: new tests (fail after succeed on a real fake DB does not commit,
first failure still wins, late upgrade after caller abort and after
BLOCKED is aborted without running the upgrade callback) were RED and are
GREEN. All gates pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:30:26 +09:00
co-authored by Claude Opus 5.5
parent 510cd80a5c
commit b3a68f70ca
5 changed files with 118 additions and 11 deletions
@@ -332,7 +332,9 @@ export function openIndexedDbDatabase<Failure>(
// A null `newVersion` means a delete is in progress, and no `upgrade`
// policy can be applied to a schema that is going away. It is reported
// before the callback runs so a caller never sees a half-open upgrade.
if (!transaction || newVersion === null || !input.upgrade) {
// Q-6.5-10. An open that already settled (caller abort, BLOCKED, close)
// must not commit a migration behind its caller's back either.
if (settled || !transaction || newVersion === null || !input.upgrade) {
upgradeRejection = { kind: "UPGRADE_REJECTED", oldVersion, newVersion };
abortUpgrade();
return;
@@ -26,7 +26,8 @@ export type IndexedDbDurability = "default" | "strict" | "relaxed";
*/
export type IndexedDbRequestSink<Failure> = Readonly<{
/**
* Records the failure and aborts the transaction. The first failure wins.
* Records the failure and aborts the transaction. The first failure wins,
* and it wins over an earlier `succeed`.
* This is `indexeddb-runtime.ts:1047-1054`'s `fail`.
*/
fail(failure: Failure): void;
@@ -266,7 +267,9 @@ export function runIndexedDbTransaction<Value, Failure>(
candidate ??= succeeded(value);
},
fail(failure) {
candidate ??= failed(failure);
// Q-6.5-10. A failure replaces an earlier `succeed`; only an earlier
// failure outranks it.
if (candidate?.ok !== false) candidate = failed(failure);
try {
transaction.abort();
} catch {