diff --git a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts index 1f590f52..1aab66bd 100644 --- a/src/adapters/storage/opfs/opfs-byte-store-adapter.ts +++ b/src/adapters/storage/opfs/opfs-byte-store-adapter.ts @@ -782,10 +782,14 @@ export function createOpfsByteStoreAdapter( let moreObjectsAvailable = false; let deadlineReached = false; - while ( - inspectedObjects < maxObjects && - now() < deadline - ) { + for (;;) { + // 1101/13.6. Only stopping before a page is read leaves objects + // behind. Turning to the next page says nothing yet, so it no longer + // sets `moreObjectsAvailable`; a finished last page used to inherit it. + if (inspectedObjects >= maxObjects || now() >= deadline) { + moreObjectsAvailable = true; + break; + } const page = await dependencies.journal.listCommittedObjects({ scope: dependencies.scope, afterObjectId, @@ -821,8 +825,8 @@ export function createOpfsByteStoreAdapter( if (removed.error.code === "CONFLICT" || removed.error.code === "NOT_FOUND") { // [0929] 05-02. Another context changed this object, so it may // still be here: report more work and skip the chunk sweep. No - // later assignment clears this — the page-advance one below only - // runs when the page had more objects, and PRESSURE never sweeps. + // later assignment clears this — the last-page one ORs, and the + // PRESSURE one only runs for a purge that never sweeps. moreObjectsAvailable = true; continue; } @@ -849,7 +853,6 @@ export function createOpfsByteStoreAdapter( break; } afterObjectId = page.value.nextObjectId; - moreObjectsAvailable = page.value.moreAvailable; } // 05-04. Removing an object leaves its chunks, and GC only takes a // chunk 24 h after it was written, so a purged user's bytes stayed on diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index 8aba8732..8aa17744 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -2373,6 +2373,41 @@ describe("05-04 a LOGOUT or ACCOUNT_DELETION purge leaves no partition bytes beh }, ); + // 1101/13.6. A journal may hand back a page shorter than the limit (the port + // does not promise full pages). A purge that walked two such pages and + // removed everything is finished: it must say so and sweep the chunks. + it("finishes a purge that walked more than one journal page", async () => { + const { root, idb, tab } = sharedRootTabs(); + const journal = createIndexedDbOpfsJournal({ + authorityToken: scopeA.authorityToken, + factory: idb.factory, + keyRange: idb.keyRange, + }); + const purged = tab("p", { + ...authority, + journal: { + ...journal, + listCommittedObjects: (request) => + journal.listCommittedObjects({ ...request, limit: 1 }), + }, + }); + expect(await putBytes(purged, "object_secret_1", [1, 2, 3])).toMatchObject({ ok: true }); + expect(await putBytes(purged, "object_secret_2", [4, 5])).toMatchObject({ ok: true }); + + expect( + await purged.maintenance.enforcePolicies({ reason: "ACCOUNT_DELETION" }), + ).toMatchObject({ + ok: true, + value: { + inspectedObjects: 2, + removedObjects: 2, + moreObjectsAvailable: false, + chunkSweep: "COMPLETED", + }, + }); + expect(filesUnder(root, scopeA)).toEqual([]); + }); + it("never sweeps the chunk of a put that runs while the purge does", async () => { const { tab } = sharedRootTabs(); const purger = tab("p", authority);