Skip to content

fix(cache): handle eviction promise rejections and use waitUntil#16

Open
zsilbi wants to merge 2 commits into
unjs:mainfrom
zsilbi:fix/evict-promises
Open

fix(cache): handle eviction promise rejections and use waitUntil#16
zsilbi wants to merge 2 commits into
unjs:mainfrom
zsilbi:fix/evict-promises

Conversation

@zsilbi
Copy link
Copy Markdown

@zsilbi zsilbi commented Apr 15, 2026

Based on the review in #15 (comment)

_evictFromStorage was calling storage.set(..., null) in a bare for loop with no promise handling, leaving rejections from async storage backends unhandled.

  • _evictFromStorage now returns Promise.all(...) instead of fire-and-forgetting in a loop
  • Both call sites (resolver rejection + invalid revalidation result) attach a .catch that routes errors through the existing _onError hook
  • When event.req.waitUntil is available (srvx / Cloudflare Workers pattern), the eviction promise is registered there so the runtime keeps the request context alive until the eviction completes

Summary by CodeRabbit

  • Bug Fixes
    • Cache eviction now runs asynchronously to avoid blocking requests.
    • Improved error handling and lifecycle integration for cache cleanup, ensuring eviction errors are reported and eviction is scheduled with the request lifecycle when available.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 185f48f6-cfb0-4753-ba03-60c81f73b913

📥 Commits

Reviewing files that changed from the base of the PR and between 5b17fa0 and 06da63f.

📒 Files selected for processing (1)
  • src/cache.ts
✅ Files skipped from review due to trivial changes (1)
  • src/cache.ts

📝 Walkthrough

Walkthrough

Stale-cache eviction was converted to asynchronous execution via Promise.all across base tiers. Eviction calls are now caught and forwarded to _onError; eviction promises are scheduled with event?.req.waitUntil?.(...) where available. Unsafe type casts were removed in favor of direct optional chaining.

Changes

Cohort / File(s) Summary
Cache eviction & scheduling
src/cache.ts
Converted _evictFromStorage to return Promise.all(...) of per-tier storage clears. Call sites now attach .catch(...) to forward errors to _onError and schedule eviction via event?.req.waitUntil?.(...). Removed type-casting in favor of optional chaining.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    rect rgba(200,230,255,0.5)
    Caller->>+CacheModule: trigger eviction / handle SWR paths
    end
    rect rgba(220,255,200,0.5)
    CacheModule->>+StorageTierA: set(key, null)
    CacheModule->>+StorageTierB: set(key, null)
    CacheModule->>+StorageTierN: set(key, null)
    end
    rect rgba(255,230,200,0.5)
    StorageTierA-->>-CacheModule: Promise resolves/rejects
    StorageTierB-->>-CacheModule: Promise resolves/rejects
    StorageTierN-->>-CacheModule: Promise resolves/rejects
    end
    CacheModule->>CacheModule: Promise.all(...) aggregates
    alt eviction error
        CacheModule->>EventLoop/_onError: _onError("[cache] Cache eviction error.", err)
    end
    opt event.req.waitUntil available
        CacheModule->>Event: event.req.waitUntil(evictPromise)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nudge the cache, let promises glide,

Evicting softly on a breezy tide.
Errors I catch, I send them along,
waitUntil hums a safety song.
Hops, async, tidy — that’s my prance! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main changes: fixing promise rejection handling in cache eviction and implementing waitUntil usage, which directly match the PR's core objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@zsilbi

This comment has been minimized.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cache.ts`:
- Around line 140-142: Replace direct runtime-specific accesses to
event.req.waitUntil with the repo-standard pattern by reading the waitUntil
function once into a local const (e.g., const waitUntil = (event.req as
any).waitUntil) and then call waitUntil(evictPromise) at each site; update all
four call sites that currently call event.req.waitUntil(...) to use the local
waitUntil reference (so references like event.req.waitUntil(evictPromise) become
waitUntil(evictPromise)) to avoid repetition and ensure compatibility with
srvx/Cloudflare runtimes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a871130-3b08-4c11-85e5-2143c421e63f

📥 Commits

Reviewing files that changed from the base of the PR and between a2a761d and 5b17fa0.

📒 Files selected for processing (1)
  • src/cache.ts

Comment thread src/cache.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant