Fix request context retention in cookies/headers/draftMode
Immediately resolved cookies(), headers(), and draftMode() promises were
created while the work and work-unit async-local stores were active. For
cookies() and headers() the promise is cached in the module-scope
CachedCookies/CachedHeaders WeakMaps, so it retained the originating
request context through its async-hooks resource store for as long as the
WeakMap entry lived - the same leak class as #96533 (fixed for
params/searchParams in #96573), in the request APIs that PR did not
cover. Per-request keys make the retention transient, but the WeakMap
ephemeron cycle (the promise resolves to its key object and captures the
store that owns the key) defeats minor GC, so every request that reads
them leaves its full request context in old space until a major GC - the
same accumulation mechanism measured in #96533 on Node 22.
Create these promises inside the work store's clean async snapshot
instead, matching the approach of #96573, so framework-created request
API promises never root the request context that produced them. No
WeakRef accessors are needed: unlike the searchParams erroring proxy,
these production paths create plain resolved promises with no deferred
store access.
draftMode() gets the same treatment for consistency, and its vestigial
draft-mode cache is removed: CachedDraftModes has been get-only since the
.set was dropped in #84179, so no immortal-key retention ever existed
there; the clean snapshot keeps all four APIs uniform regardless.
Adds an async-hooks e2e regression test proving ordinary render promises
capture the stores while cookies()/headers()/draftMode() promises do not,
covering a dynamic request (request store, exercises the per-request
WeakMap keys) and a force-static page prerendered at build time
(exercises the forceStatic override paths).
Validation: the regression test fails without the fix (both cases) and
passes with it in both bundlers; existing suites for the touched APIs
pass (draft-mode, draft-mode-middleware, dynamic-data,
headers-static-bailout); tsc/Prettier/ESLint clean; autoreview panel
findings addressed.
Related: #96533, #96573
Co-Authored-By:
Marcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment