From 5fb54d25da081c5a0c475edbda6323ef4b6df028 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sat, 14 Nov 2026 21:34:39 +0800 Subject: [PATCH] fix: cancel invalidated keepalive timers on credential rotation --- src/cache-keepalive.ts | 2 +- tests/cache-keepalive.test.ts | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/cache-keepalive.ts b/src/cache-keepalive.ts index a1483a609d..8bbe411ac8 100644 --- a/src/cache-keepalive.ts +++ b/src/cache-keepalive.ts @@ -293,7 +293,7 @@ function armInternal(inp: KeepaliveArmInput): void { const prev = registry.get(inp.sessionId); // Credential rotated since the snapshot (#2131): the old body was signed // for another key — drop it before deciding about the new one. - if (prev && prev.credFp !== credFp) registry.delete(inp.sessionId); + if (prev && prev.credFp !== credFp) disarm(inp.sessionId, "credential-rotated"); const breakpointTier = detectKeepaliveTier(parsed); let cadenceMs: number; diff --git a/tests/cache-keepalive.test.ts b/tests/cache-keepalive.test.ts index 66b060a2f5..8ce0799bf3 100644 --- a/tests/cache-keepalive.test.ts +++ b/tests/cache-keepalive.test.ts @@ -621,3 +621,55 @@ for (const reply of [ assert.equal(timers.pendingCount(), 1, "only the replacement snapshot stays scheduled"); }); } + +for (const [tier, wireBody] of [["5m", BODY_5M], ["1h", BODY_1H]] as const) { + test(`arm: rotating credentials without a replacement ${tier} snapshot cancels its timer`, () => { + noteSuccessfulForward(armInput({ wireBody, headers: { "x-api-key": "rotation-old" } })); + assert.equal(timers.pendingCount(), 1); + noteSuccessfulForward(armInput({ wireBody: BODY_NO_BP, headers: { "x-api-key": "rotation-new" } })); + assert.equal(keepaliveStatusFor(sid), undefined, "the old credential's snapshot is ineligible"); + assert.equal(timers.pendingCount(), 0, "no callback should retain the dropped snapshot until its old cadence"); + }); +} + +test("arm: repeated credential rotation cannot retain snapshots outside the concurrent cap", () => { + process.env.BILI_CACHE_KEEPALIVE_MAX_PINGS = "1"; + for (let round = 0; round < 20; round++) { + noteSuccessfulForward(armInput({ wireBody: BODY_1H, headers: { "x-api-key": `old-${round}` } })); + noteSuccessfulForward(armInput({ wireBody: BODY_NO_BP, headers: { "x-api-key": `new-${round}` } })); + assert.equal(keepaliveStatusFor(sid), undefined); + } + assert.equal(timers.pendingCount(), 0, "all unregistered snapshots release their timer-held references"); +}); + +test("arm: dropping one rotated snapshot leaves another session's timer intact", () => { + const other = `${sid}-other`; + noteSuccessfulForward(armInput({ sessionId: other, wireBody: BODY_1H })); + const otherStatus = keepaliveStatusFor(other); + noteSuccessfulForward(armInput({ headers: { "x-api-key": "rotation-old" } })); + noteSuccessfulForward(armInput({ wireBody: BODY_NO_BP, headers: { "x-api-key": "rotation-new" } })); + assert.equal(keepaliveStatusFor(sid), undefined); + assert.deepEqual(keepaliveStatusFor(other), otherStatus); + assert.equal(timers.pendingCount(), 1); +}); + +test("arm: unchanged credentials preserve the prior eligible snapshot when no new breakpoint exists", () => { + noteSuccessfulForward(armInput()); + const previous = keepaliveStatusFor(sid); + noteSuccessfulForward(armInput({ wireBody: BODY_NO_BP })); + assert.deepEqual(keepaliveStatusFor(sid), previous); + assert.equal(timers.pendingCount(), 1); +}); + +test("arm: an eligible replacement after rotation keeps one live timer and sends the new credential", async () => { + noteSuccessfulForward(armInput({ wireBody: BODY_1H, headers: { "x-api-key": "rotation-old" } })); + noteSuccessfulForward(armInput({ headers: { "x-api-key": "rotation-new" } })); + assert.equal(timers.pendingCount(), 1); + assert.equal(keepaliveStatusFor(sid)?.tierLabel, "5m"); + timers.advance(270_000); + await settle(() => keepaliveStatusFor(sid)?.pingCount === 1); + assert.equal(upstreamRequests.length, 1); + assert.equal(upstreamRequests[0].headers["x-api-key"], "rotation-new"); + assert.equal(upstreamRequests[0].body, PING_5M); + assert.equal(timers.pendingCount(), 1); +});