fix(storage): hold a per-transaction lock so reconcile skips a live OPFS put (05-01)

Reconcile cleaned up any incomplete journal row, including a put still
running in another tab, and the put then reported success for deleted files.
Put and delete now hold a Web Lock per transaction from begin to return;
reconcile takes those locks with ifAvailable, re-reads the page under them
and reports the rows it skipped (D-3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:09:30 +09:00
co-authored by Claude Opus 5.5
parent 926f448fce
commit 8fc816ee25
5 changed files with 661 additions and 205 deletions
+51
View File
@@ -666,6 +666,57 @@ describe("OPFS byte-store coordinator", () => {
}
});
it("05-01 decides a reconciled row from what the journal holds once its lock is taken", async () => {
// Listed while its owner still ran, the row was PREPARING. By the time
// the owner let go it had committed and only finalization failed, so the
// staging receipt names the committed generation. Cleaning it up from the
// stale snapshot would delete a committed object.
const journal = createJournal();
const row: OpfsJournalTransaction = {
transactionId: "transaction_12345678",
fencingToken: "fencing_12345678",
mutation: "PUT",
scope,
phase: "PREPARING",
objectId: "object_12345678",
expectedGeneration: null,
targetGeneration: 1,
targetByteLength: 1,
targetStoragePolicy: storagePolicy,
budgetReservation: {
namespace: scope.namespace,
reservedBytes: 1,
hardBudgetBytes: storagePolicy.hardBudgetBytes,
},
startedAtEpochMs: 100,
};
journal.transactions.set(row.transactionId, {
...row,
phase: "COMMITTED",
preparedObject: committedObject("object_12345678", storagePolicy, 100),
});
vi.spyOn(journal, "listIncomplete").mockResolvedValueOnce(
browserDataSuccess({ transactions: [row], moreAvailable: false }),
);
const worker = createWorker();
const cleanupTransaction = vi.spyOn(worker, "cleanupTransaction");
const finalizePut = vi.spyOn(worker, "finalizePut");
const adapter = createOpfsByteStoreAdapter({
journal,
worker,
scope,
storagePolicy,
now: () => 100,
});
expect(await adapter.maintenance.reconcile()).toMatchObject({
ok: true,
value: { inspectedTransactions: 1, rolledBackTransactions: 0 },
});
expect(cleanupTransaction).not.toHaveBeenCalled();
expect(finalizePut).toHaveBeenCalledTimes(1);
});
it("does not roll back journal after an unknown worker mutation effect", async () => {
const journal = createJournal();
const worker = createWorker({
+101
View File
@@ -13,6 +13,7 @@ import type {
} from "../../src/application/ports/browser-file-storage/shared.ts";
import { browserDataSuccess } from "../../src/adapters/browser-file-storage/result.ts";
import { createOpfsByteStoreAdapter } from "../../src/adapters/storage/opfs/opfs-byte-store-adapter.ts";
import { createIndexedDbOpfsJournal } from "../../src/adapters/storage/opfs/indexeddb-opfs-journal.ts";
import {
resolveOpfsRuntimePolicy,
} from "../../src/adapters/storage/opfs/opfs-policy.ts";
@@ -33,6 +34,7 @@ import {
type OpfsWorkerMessageHost,
type OpfsWorkerRuntime,
} from "../../src/adapters/storage/opfs/opfs-worker-runtime.ts";
import { MemoryIndexedDbFactory } from "../helpers/memory-indexeddb.ts";
const scopeA: OpfsStorageScope = Object.freeze({
namespace: "durable-objects",
@@ -2083,3 +2085,102 @@ describe("NS-05 a bootstrap failure answers the request it belongs to", () => {
});
}
});
/**
* Two runtimes ("tabs") share one OPFS root, one exclusive worker lock and
* one journal database, the way two tabs of one origin do.
*/
function sharedRootTabs(policy = runtimePolicy) {
const root = new MemoryDirectory();
const leases = exclusiveLeases();
const idb = new MemoryIndexedDbFactory();
const tab = (tag: string) => {
const runtime = createOpfsWorkerRuntime({
root: root as unknown as FileSystemDirectoryHandle,
crypto: globalThis.crypto,
policy,
leaseManager: leases,
dedicatedWorker: true,
supportsSynchronousAccessHandles: false,
});
let sequence = 0;
return createOpfsByteStoreAdapter({
journal: createIndexedDbOpfsJournal({
authorityToken: scopeA.authorityToken,
factory: idb.factory,
keyRange: idb.keyRange,
}),
worker: loopbackGateway(runtime, policy),
scope: scopeA,
storagePolicy,
policy,
createTransactionId: () => `transaction_${tag}_${++sequence}`,
});
};
return { root, leases, idb, tab };
}
async function openedBytes(
adapter: ReturnType<typeof createOpfsByteStoreAdapter>,
objectId: string,
): Promise<Readonly<{ generation: number; bytes: number[] }> | string> {
const opened = await adapter.objects.open({ objectId });
if (!opened.ok) return opened.error.code;
const bytes: number[] = [];
for await (const chunk of opened.value.source.stream(
new AbortController().signal,
)) {
if (!chunk.ok) return chunk.error.code;
bytes.push(...chunk.value);
}
return { generation: opened.value.descriptor.generation, bytes };
}
describe("05-01 reconcile leaves a live put's journal row alone", () => {
it.each([
["a first put", null],
["an overwrite", 1],
] as const)(
"keeps %s that reconcile overlapped in another tab",
async (_label, expectedGeneration) => {
const { tab } = sharedRootTabs();
const tabA = tab("a");
const tabB = tab("b");
if (expectedGeneration !== null) {
expect(
await tabA.objects.put({
objectId: "object_12345678",
expectedGeneration: null,
mediaType: "application/octet-stream",
source: oneByteSource(),
}),
).toMatchObject({ ok: true, value: { generation: 1 } });
}
const stalled = stalledSource();
const putA = tabA.objects.put({
objectId: "object_12345678",
expectedGeneration,
mediaType: "application/octet-stream",
source: stalled.source,
});
// Tab A holds the worker lock and its journal row is PREPARING.
await stalled.streaming;
const reconcileB = tabB.maintenance.reconcile();
await new Promise((resolve) => setTimeout(resolve, 20));
stalled.release();
const [put, reconciled] = await Promise.all([putA, reconcileB]);
const generation = (expectedGeneration ?? 0) + 1;
expect(put).toMatchObject({ ok: true, value: { generation } });
expect(reconciled).toMatchObject({
ok: true,
value: { skippedLiveTransactions: 1, rolledBackTransactions: 0 },
});
expect(await openedBytes(tabA, "object_12345678")).toEqual({
generation,
bytes: [5],
});
},
);
});