fix(storage): refresh chunk mtime when a put reuses it (R-5.2)
writeImmutableChunk returned early when a chunk with the same digest already existed, so the chunk kept its old lastModified. Orphan GC decides "not referenced" from the page journal and deletes in the worker; another tab's GC holding a stale answer could delete a day-old chunk right after this put reused it and left staging, and the committed object then failed INTEGRITY_FAILED on open. A dedup hit now rewrites the identical bytes through the atomic writable path, which refreshes lastModified so DELETE_ORPHAN_CHUNK's grace check skips it. Readers of the committed chunk see either copy, never a partial file. The mutation lock scope is unchanged. Verified: new test "R-5.2 a dedup-reused chunk is protected by the grace period again" ages a committed chunk, reuses it from a second put and finalizes. Before the fix LIST_ORPHAN_CANDIDATES still returned the reused digest; now it is neither listed nor deleted and the second object verifies. Gates: check:architecture, check:types:app/test/web-worker, lint, test:component pass; test:unit passes except removal-fixture.test.ts, which needs gitignored release artifacts absent from a fresh worktree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d758934e9c
commit
e28e9f75fb
@@ -1219,6 +1219,21 @@ export function createOpfsWorkerRuntime(
|
||||
);
|
||||
await directory.removeEntry(fileName);
|
||||
} else {
|
||||
// R-5.2. A dedup hit must restart the chunk's grace period. Orphan GC
|
||||
// reads "not referenced" from the page journal and deletes in the
|
||||
// worker, so another tab's GC can hold a stale answer for a chunk that
|
||||
// this put is about to reference once staging is gone. An atomic
|
||||
// rewrite of the identical bytes refreshes lastModified, and readers of
|
||||
// the committed chunk see either copy, never a partial file.
|
||||
const { directory, fileName } = await resolveFileParent(
|
||||
dependencies.root,
|
||||
path,
|
||||
false,
|
||||
);
|
||||
await writeWithAtomicWritable(
|
||||
await directory.getFileHandle(fileName),
|
||||
bytes,
|
||||
);
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -697,6 +697,128 @@ describe("R-5.1 orphan GC pages past referenced chunks", () => {
|
||||
});
|
||||
});
|
||||
|
||||
function memoryFileAt(root: MemoryDirectory, path: readonly string[]): MemoryFile {
|
||||
let current: MemoryNode = root;
|
||||
for (const segment of path) {
|
||||
if (!(current instanceof MemoryDirectory)) break;
|
||||
current = current.children.get(segment)!;
|
||||
}
|
||||
if (!(current instanceof MemoryFile)) throw new Error("No such file.");
|
||||
return current;
|
||||
}
|
||||
|
||||
describe("R-5.2 a dedup-reused chunk is protected by the grace period again", () => {
|
||||
it("refreshes the chunk mtime on reuse so orphan GC neither lists nor deletes it", async () => {
|
||||
const root = new MemoryDirectory();
|
||||
const runtime = createOpfsWorkerRuntime({
|
||||
root: root as unknown as FileSystemDirectoryHandle,
|
||||
crypto: globalThis.crypto,
|
||||
policy: runtimePolicy,
|
||||
leaseManager: immediateLeases({ count: 0 }),
|
||||
dedicatedWorker: true,
|
||||
supportsSynchronousAccessHandles: false,
|
||||
});
|
||||
async function putAndFinalize(
|
||||
transactionId: string,
|
||||
physicalGenerationId: OpfsPhysicalGenerationId,
|
||||
): Promise<OpfsPreparedObject> {
|
||||
await runtime.handleRequest(
|
||||
beginRequest(
|
||||
`request_begin_${transactionId}`,
|
||||
transactionId,
|
||||
scopeA,
|
||||
physicalGenerationId,
|
||||
),
|
||||
);
|
||||
await runtime.handleRequest({
|
||||
requestId: `request_append_${transactionId}`,
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "APPEND_CHUNK",
|
||||
scope: scopeA,
|
||||
transactionId,
|
||||
sequence: 0,
|
||||
bytes: new Uint8Array([77]).buffer,
|
||||
});
|
||||
const prepared = preparedValue(
|
||||
await runtime.handleRequest({
|
||||
requestId: `request_finish_${transactionId}`,
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "FINISH_PUT",
|
||||
scope: scopeA,
|
||||
transactionId,
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
await runtime.handleRequest({
|
||||
requestId: `request_finalize_${transactionId}`,
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "FINALIZE_PUT",
|
||||
transactionId,
|
||||
preparedObject: prepared,
|
||||
}),
|
||||
).toMatchObject({ ok: true });
|
||||
return prepared;
|
||||
}
|
||||
|
||||
const first = await putAndFinalize(
|
||||
"transaction_dedup_0001",
|
||||
PHYSICAL_GENERATION_A,
|
||||
);
|
||||
const digestHex = first.chunks[0]!.digestHex;
|
||||
// The chunk is now older than any grace period.
|
||||
memoryFileAt(root, chunkFilePath(scopeA, digestHex)).lastModified = 0;
|
||||
const cutoff = Date.now() - runtimePolicy.orphanGracePeriodMs;
|
||||
expect(
|
||||
await runtime.handleRequest({
|
||||
requestId: "request_dedup_list_before",
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "LIST_ORPHAN_CANDIDATES",
|
||||
scope: scopeA,
|
||||
olderThanEpochMs: cutoff,
|
||||
maxEntries: 10,
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: { digests: [digestHex] } });
|
||||
|
||||
// A second PUT of the same bytes reuses the chunk and then leaves staging,
|
||||
// so from here on only the chunk mtime protects it from a GC that read
|
||||
// "not referenced" just before the reuse committed.
|
||||
const second = await putAndFinalize(
|
||||
"transaction_dedup_0002",
|
||||
"b".repeat(32) as OpfsPhysicalGenerationId,
|
||||
);
|
||||
expect(second.chunks[0]!.digestHex).toBe(digestHex);
|
||||
|
||||
expect(
|
||||
await runtime.handleRequest({
|
||||
requestId: "request_dedup_list_after",
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "LIST_ORPHAN_CANDIDATES",
|
||||
scope: scopeA,
|
||||
olderThanEpochMs: cutoff,
|
||||
maxEntries: 10,
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: { digests: [] } });
|
||||
expect(
|
||||
await runtime.handleRequest({
|
||||
requestId: "request_dedup_delete",
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "DELETE_ORPHAN_CHUNK",
|
||||
scope: scopeA,
|
||||
digestHex,
|
||||
olderThanEpochMs: cutoff,
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: { deleted: false } });
|
||||
expect(
|
||||
await runtime.handleRequest({
|
||||
requestId: "request_dedup_verify",
|
||||
protocolVersion: OPFS_WORKER_PROTOCOL_VERSION,
|
||||
kind: "VERIFY_OBJECT",
|
||||
preparedObject: second,
|
||||
}),
|
||||
).toMatchObject({ ok: true, value: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("OPFS worker client lifecycle", () => {
|
||||
it("rejects a colliding concurrent request id without replacing the first RPC", async () => {
|
||||
let listener: ((event: MessageEvent<unknown>) => void) | undefined;
|
||||
|
||||
Reference in New Issue
Block a user