AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

PartialPrefetching Causes Four Renders Per On-Demand Page, Timer-Retained Render State Leads To Large Memory Spikes

With partialPrefetching enabled, dynamic pages are rendered four times per request (request, runtime prefetch, shell upgrade x2). Timers scheduled during each render capture AsyncLocalStorage context and retain large React Flight request and cache state, causing memory retention of ~3.8 MiB per request for a 10-minute timer, up to 45 GiB at 20 rps.

highConfidence 92%Next.jsRoute HandlersTurbopackServer ActionsAffected V16.3.5Affected V16.3.6Affected V16.4.0-Canary.39

Origin Analysis

Next.js spawns two additional background render phases when partialPrefetching is active: spawnRuntimePrefetchWithFilledCaches and the fallback shell upgrade via responseCache.revalidate. Each phase executes user code and schedules timers; those timers inherit the render's AsyncLocalStorage store, which holds workUnitStore, React Flight Request, cacheController and abort-reason Error frames. The references are not cleared after background renders settle, so timers pin the entire render state until they fire.
1. Clone https://github.com/giovannetti-eric/next-partial-prefetching-memory-repro and npm install. 2. Build/start with PARTIAL_PREFETCHING=true and NODE_OPTIONS=--expose-gc. 3. Run count-timers.mjs to observe 4 renders per request. 4. Run load.mjs to measure retained heap after forced GC; compare with PARTIAL_PREFETCHING=false. 5. Observe 3.8 MiB retained per request for 600s timers vs 0.55 MiB without flag.

Fixing Code Block

Edge Case Audit

Disabling partialPrefetching loses the prefetch performance benefit and may not be acceptable for apps that rely on partial prefetching. Setting gcTime=Infinity on server may change cache eviction behavior for client-side hydration? Actually it only affects server, so safe. The config change is easily reversible; if you revert, the memory issue returns. Upgrading Next.js may not fix until a patch is released. Test thoroughly for regressions in caching and PPR behavior.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state