Skip to content

fix(cache): use ctx.waitUntil() for fire-and-forget KV deletes #27

@rianvdm

Description

@rianvdm

Problem

`src/utils/cache.ts:94,101` does fire-and-forget KV deletes:

```ts
this.kv.delete(cacheKey).catch(console.error)
```

In Cloudflare Workers, any pending promise that isn't awaited or registered with `ctx.waitUntil()` can be cancelled when the request handler returns. So the delete might fire, or the runtime might tear down the isolate first.

Fix

Pass an `ExecutionContext` (or `waitUntil`-callable surface) into the cache layer and wrap both deletes:

```ts
ctx.waitUntil(this.kv.delete(cacheKey))
```

Both call sites are in the cache invalidation path (deleting stale entries on read), so the visible symptom of dropped deletes is stale cache hits — silent and rare.

Scope

  • ~5 LOC change in `src/utils/cache.ts`
  • Whatever plumbing is needed to thread `ctx` through to the cache instance — likely the cached-client constructor or a per-request setter

Cost if unfixed

Stale cache entries linger past their intended invalidation. Low blast radius (cache layer, not data integrity), but it's a known Workers footgun and the fix is small.

Source: this repo's CLAUDE.md calls out `waitUntil`-style patterns; `workers-best-practices` flags raw `.catch()` on background work as an anti-pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions