Skip to content

feat(cache): detect 'use cache' module-scope deadlocks early in dev#1155

Closed
Divkix wants to merge 1 commit into
cloudflare:mainfrom
Divkix:fix/issue-1126
Closed

feat(cache): detect 'use cache' module-scope deadlocks early in dev#1155
Divkix wants to merge 1 commit into
cloudflare:mainfrom
Divkix:fix/issue-1126

Conversation

@Divkix
Copy link
Copy Markdown
Contributor

@Divkix Divkix commented May 11, 2026

Closes #1126

Ports the Next.js use-cache deadlock probe (vercel/next.js#93500, vercel/next.js#93538) to vinext.

Problem

When a "use cache" function deadlocks in vinext dev due to module-scope state (e.g. a top-level joining a hanging promise from outer scope), users get an indefinite hang with no actionable error.

Solution

  • Dev-only timeout (default 30s, configurable via ) around every fill.
  • Deadlock probe scheduler that fires after ~10s of idle time. It spawns a worker-thread that re-executes the same cached function with a fresh ESM module scope (via cache-bust).
  • Request store snapshot forwarding so caches that read / resolve correctly in the probe.
  • HMR teardown — the worker pool is torn down on file invalidation so probes don't use stale caches.
  • Actionable error — when the probe completes while the main fill is still hung, we abort with pointing at the dedupe pattern instead of the generic .

All new probe code is gated on and tree-shakes out of production / Workers builds.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@1155

commit: 5405055

…loudflare#1126)

Ports Next.js use-cache deadlock probe (vercel/next.js#93500, vercel/next.js#93538):

- UseCacheTimeoutError / UseCacheDeadlockError with digest codes
- Dev-only timeout around 'use cache' fills (default 30s)
- Probe scheduler that fires after 10s idle time in dev
- Worker-thread probe pool that re-executes the function with fresh module scope
- Request store snapshot forwarding so private caches resolve correctly
- HMR teardown so probes don't use stale module caches
- Gated on NODE_ENV === 'development', tree-shakes out of production/Workers

New files:
- shims/use-cache-errors.ts
- server/use-cache-probe-globals.ts
- server/use-cache-probe-scheduler.ts
- server/use-cache-probe-pool.ts
- server/use-cache-probe-worker.ts

Modified:
- shims/cache-runtime.ts (dev timeout + probe integration)
- index.ts (install probe in configureServer)

Tests:
- tests/use-cache-deadlock.test.ts (10 tests, all passing)
@Divkix
Copy link
Copy Markdown
Contributor Author

Divkix commented May 11, 2026

Superseded by #1157 — same feature (#1126) using Vite ModuleRunner instead of worker_threads for better architectural alignment with the repo.

@Divkix Divkix closed this May 11, 2026
@Divkix Divkix deleted the fix/issue-1126 branch May 11, 2026 01:56
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.

Detect 'use cache' module-scope deadlocks early in dev

1 participant