From 421026da9f98551cb347163d5762f39fc3e42bcc Mon Sep 17 00:00:00 2001 From: Louie0806 Date: Sat, 31 Oct 2026 02:06:53 +0800 Subject: [PATCH] fix(#4599): converge the four remaining master-red lanes onto ci-fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ci-fixes at 67f8e894 still leaves four Windows shards red, and all four deltas already exist on PR #5178. This ports the missing hunks so the convergence branch actually converges. 1. `test (windows-latest, 2x, 3)` — #5103. `UnifiedShadow` builds its db path as `${dir}/index.db`, so the open-failure warning reads `C:\...\sessions/index.db`. `issue5103` asserts the warning contains `path.join(dir, "index.db")` — `...\index.db` on win32 — so a mixed separator never matches. `path` is already imported at :30 and the db is opened through the same join, so POSIX output is byte-identical. 2. `test (windows-latest, 22, 4)` + the 4788 leg — #4739/#4788 teardown. The suites arm a real `SessionStorage` under `BILI_STORAGE_UNIFIED=full`, so the sidecar is still open at teardown. `cancelAll()` clears timers and the build map but never closes the shadow; Windows refuses to unlink a directory holding an open file (`EBUSY`/`EPERM` on `index.db`). Use `closeQuietly()` — cancel, then close — in the short-lived-store teardown. 3. `test (ubuntu-latest, 24, 3)` + `(windows-latest, 22, 2)` — #5155. Both cold-rebuild cases waited for the debounced durable flush with a fixed `setTimeout(20)`. A millisecond budget is a deadline a loaded runner can miss, and the miss is an honest `409 !== 200` from a one-turn-stale table. Await the settle itself: `afterNextFlush("conv-ptrs")` resolves once the armed save lands. 4. `test (ubuntu-latest, 22, 3)` — #4788 verdict deadlock. `afterNextFlush` parks a resolver in `flushWaiters`, and the only thing that drains it is the debounce timer — which is armed `unref?.()`. Once that timer is the last pending handle the loop drains, the promise never settles, and node reports `Promise resolution is still pending but the event loop has already resolved`. Keep the timer referenced exactly while a waiter exists (`ref()` on waiter registration; the host-upkeep default stays unref'd). Both view hunks and the two test-teardown hunks are byte-identical to the ones already on PR #5178, so this converges the branches instead of inventing a third variant. Verified locally: `npx tsc --noEmit` clean; issue4788 0/4 -> 4/4; issue4739 4/4; issue5103 5/5; inc-canonical-pointers 3/3. The `cancelAll` -> `closeQuietly` hunk has no local negative control: reverting it keeps Linux green because `EBUSY`/`EPERM` only fire where an open handle blocks unlink. That control can only run on a Windows lane — stated rather than papered over. The #4788 arm's own reader is confirmed instead: `cancelAll()` leaves `index.db`, `index.db-shm`, `index.db-wal` in `/proc/self/fd` while `closeQuietly()` leaves none (probed directly). --- src/persist.ts | 13 ++++++++++++- src/storage/shadow.ts | 4 ++-- tests/inc-canonical-pointers.test.ts | 8 ++++++-- tests/issue4739-bounded-session-load.test.ts | 4 ++-- tests/issue4788-resident-minimal.test.ts | 13 +++++++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index 1586370479..315810d969 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2766,7 +2766,14 @@ export class SessionStore { this.drainFlushWaiters(id, this.lastSaveLanded.get(id) === true); }); }, this.debounceMs); - timer.unref?.(); + // #4788: the debounce timer is host-owned periodic upkeep and must not + // pin a process open on its own -- so it is unref'd. But `afterNextFlush` + // makes it a HOST: a caller that awaits a verdict has no other handle + // guaranteed to fire, and an unref'd timer is not a reason for Node to + // stay alive. Awaiting then exits the loop with the promise unresolved + // (`Promise resolution is still pending but the event loop has already + // resolved`). Keep the timer referenced exactly while someone waits. + if (!this.flushWaiters.has(id)) timer.unref?.(); this.saveTimers.set(id, timer); } @@ -2787,6 +2794,10 @@ export class SessionStore { const ws = this.flushWaiters.get(sessionId) ?? []; ws.push((ok: boolean): void => { resolve(ok); }); this.flushWaiters.set(sessionId, ws); + // #4788: the timer was armed unref'd (no waiter existed then). The + // waiter now holds the promise open, so the timer must hold the loop + // open too -- otherwise the process drains and the verdict is lost. + this.saveTimers.get(sessionId)?.ref?.(); }); } diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index b9dd9f19dc..4656314ced 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -300,12 +300,12 @@ export class UnifiedShadow { : "; delete the file to reset the index"; loggerLog( "warn", - `[storage] unified sidecar failed to open ${dir}/index.db (${errStr}) — staying legacy-only${tail}`, + `[storage] unified sidecar failed to open ${path.join(dir, "index.db")} (${errStr}) — staying legacy-only${tail}`, ); recordSidecarOpenFailure(errStr); return null; } - const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null), undefined, `${dir}/index.db`); + const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null), undefined, path.join(dir, "index.db")); recordSidecarInstance(shadow); const engineTag = ` [engine: ${store.engine}/${store.engineSource}]`; const walTag = ` [wal-cap: ${walLimit}]`; diff --git a/tests/inc-canonical-pointers.test.ts b/tests/inc-canonical-pointers.test.ts index e0210b8d4f..bdc730e143 100644 --- a/tests/inc-canonical-pointers.test.ts +++ b/tests/inc-canonical-pointers.test.ts @@ -105,7 +105,11 @@ test("cold canonical rebuild: identical resync keeps the shared prefix; delta as // #5155: the wrap-up defers the durable flush (debounced) — let the // rows settle before dropping the resident caches, or the cold // rebuild reads a one-turn-stale table and honestly 409s. - await new Promise((resolve) => setTimeout(resolve, 20)); + // Await the settle itself (afterNextFlush resolves once the armed save + // lands) instead of a fixed sleep: the settle is a real condition, and + // a magic millisecond budget is a deadline a loaded windows-latest + // runner can miss — the 409 flake this replaces. + assert.equal(await getStore().afterNextFlush("conv-ptrs"), true, "the turn's durable save must land before the cold reset"); __resetIncrementalLaneForTests(); const delta = await h.turn({ sinceSeq: 3, messages: [core("u4", "four")] }); @@ -135,7 +139,7 @@ test("cold canonical rebuild: edited-tail resync keeps the shared prefix, drops await h.turn({ sinceSeq: 0, resync: true, messages: [core("u1", "one"), core("u2", "two-EDITED")] }); await __incrementalPtrChainForTests("conv-ptrs"); // #5155: settle the debounced flush before the cold reset (see above). - await new Promise((resolve) => setTimeout(resolve, 20)); + assert.equal(await getStore().afterNextFlush("conv-ptrs"), true, "the resync's durable save must land before the cold reset"); __resetIncrementalLaneForTests(); const delta = await h.turn({ sinceSeq: 2, messages: [core("u3", "three")] }); assert.equal(delta.status, 200); diff --git a/tests/issue4739-bounded-session-load.test.ts b/tests/issue4739-bounded-session-load.test.ts index bc9906b5f0..bf91303252 100644 --- a/tests/issue4739-bounded-session-load.test.ts +++ b/tests/issue4739-bounded-session-load.test.ts @@ -139,7 +139,7 @@ test("full mode: viewFor decodes only pointer rows and matches the full-log deco assert.deepEqual(via, reference, "bounded decode byte-equal to the full-log decode"); assert.equal(delta, expectedPointers, `viewFor read ${delta} message rows for ${expectedPointers} pointer seqs (full log = ${liveSeqs.size})`); } finally { - store.cancelAll(); + store.closeQuietly(); if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prev; rmrf(dir); @@ -178,7 +178,7 @@ test("full mode: dropped metadata restores from the index alone (narrow read, no assert.ok(again, "envelope rewritten by later save"); assert.deepEqual(Object.keys(JSON.parse(fs.readFileSync(again, "utf8")).payload.metadata as object).sort(), persistedKeys); } finally { - store.cancelAll(); + store.closeQuietly(); if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prev; rmrf(dir); diff --git a/tests/issue4788-resident-minimal.test.ts b/tests/issue4788-resident-minimal.test.ts index 5f20c83aa8..488ff932b9 100644 --- a/tests/issue4788-resident-minimal.test.ts +++ b/tests/issue4788-resident-minimal.test.ts @@ -129,7 +129,12 @@ test("(a) between-turns drop: every landed request-end boundary discharges dropp assert.ok((s.blockContents as LazyBlockContents).isDropped, "still folded after the second boundary"); assert.ok(isDroppedMetadata(s.metadata)); } finally { - store.cancelAll(); + // #4739/#5140 family: the suite arms a real SessionStorage under + // BILI_STORAGE_UNIFIED=full, so the sidecar handle is still open here. + // cancelAll() clears timers and the build map but never closes the + // shadow; Windows then refuses to unlink the directory (EBUSY/EPERM). + // Close the short-lived store first: cancel, then close. + store.closeQuietly(); rmrf(dir); } }); @@ -164,7 +169,7 @@ test("(b) narrow-read reconstruction deepEquals the pre-drop reference", async ( const rec = (parsed.payload ?? parsed) as Record; assert.equal(Object.keys((rec.blockContents ?? {}) as object).length, Object.keys(refBlocks).length, "full set round-trips through the envelope"); } finally { - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } }); @@ -203,7 +208,7 @@ test("(c) hard cap under sustained activity: boundaries drop, in-flight skips, w process.env.BILI_SESSION_RESIDENT_HARD_MAX_BYTES = saved; } } finally { - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } }); @@ -246,7 +251,7 @@ test("(d) envelope-frozen routing is unchanged AND gets equivalent drops", async assert.equal(s.blockContents.size, 21, "all entries resident again after reconstruction"); } finally { process.env.BILI_STORAGE_UNIFIED = "legacy"; - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } });