Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
5817bd1d
Unverified
Commit
5817bd1d
authored
Aug 16, 2026
by
Hendrik Liebau
Committed by
GitHub
Aug 16, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Anchor the async local storage instances to global symbols (#97255)
parent
c18acf5c
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
91 additions
and
121 deletions
+91
-121
action-async-storage-instance.ts
...xt/src/server/app-render/action-async-storage-instance.ts
+2
-2
after-task-async-storage-instance.ts
...rc/server/app-render/after-task-async-storage-instance.ts
+2
-2
async-local-storage.ts
packages/next/src/server/app-render/async-local-storage.ts
+37
-0
console-async-storage-instance.ts
...t/src/server/app-render/console-async-storage-instance.ts
+2
-2
dynamic-access-async-storage-instance.ts
...erver/app-render/dynamic-access-async-storage-instance.ts
+2
-2
work-async-storage-instance.ts
...next/src/server/app-render/work-async-storage-instance.ts
+2
-2
work-unit-async-storage-instance.ts
...src/server/app-render/work-unit-async-storage-instance.ts
+2
-2
request-insights-identity.ts
...es/next/src/server/lib/trace/request-insights-identity.ts
+2
-11
dev-warmup.util.ts
...nt/app-dir/cache-components-dev-warmup/dev-warmup.util.ts
+39
-71
cache-components-tasks.test.ts
...dir/cache-components-tasks/cache-components-tasks.test.ts
+1
-27
No files found.
packages/next/src/server/app-render/action-async-storage-instance.ts
View file @
5817bd1d
import
type
{
ActionAsyncStorage
}
from
'./action-async-storage.external'
import
type
{
ActionAsyncStorage
}
from
'./action-async-storage.external'
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
export
const
actionAsyncStorageInstance
:
ActionAsyncStorage
=
export
const
actionAsyncStorageInstance
:
ActionAsyncStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'action-async-storage'
)
packages/next/src/server/app-render/after-task-async-storage-instance.ts
View file @
5817bd1d
import
type
{
AfterTaskAsyncStorage
}
from
'./after-task-async-storage.external'
import
type
{
AfterTaskAsyncStorage
}
from
'./after-task-async-storage.external'
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
export
const
afterTaskAsyncStorageInstance
:
AfterTaskAsyncStorage
=
export
const
afterTaskAsyncStorageInstance
:
AfterTaskAsyncStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'after-task-async-storage'
)
packages/next/src/server/app-render/async-local-storage.ts
View file @
5817bd1d
...
@@ -45,6 +45,43 @@ export function createAsyncLocalStorage<
...
@@ -45,6 +45,43 @@ export function createAsyncLocalStorage<
return
new
FakeAsyncLocalStorage
()
return
new
FakeAsyncLocalStorage
()
}
}
/**
* Returns the storage registered under `name`, and creates it on first use.
*
* These storages must be singletons within a realm. A store that is entered
* through one reference to a storage must be readable through every other
* reference to it. If it is not, code that runs inside the scope sees no store
* at all. Module identity does not guarantee this. A realm can evaluate the
* same `next` file more than once if the package is reachable through more than
* one path, and then each evaluation creates a storage of its own. A global
* symbol keeps the singleton intact for any number of copies. Worker threads
* and edge sandboxes still get separate storages, because each of them has its
* own `globalThis`.
*
* Module identity broke this way in `next dev`. A bug in Node's
* `fs.realpathSync` can return a path with its symlinks unresolved, and the
* module loader keys the module cache on that path. On a pnpm install it then
* resolves `next/dist/...` through the `node_modules/next` symlink instead of
* the real path, so the file is evaluated a second time. See
* https://github.com/nodejs/node/pull/65113. Node versions without that fix
* stay affected.
*
* The key includes the Next.js version, so two different versions of Next.js in
* the same realm keep separate storages. Their store shapes might not be
* compatible.
*/
export
function
getOrCreateGlobalAsyncLocalStorage
<
Store
extends
{}
>
(
name
:
string
):
AsyncLocalStorage
<
Store
>
{
const
key
=
Symbol
.
for
(
`@next/
${
name
}
@
${
process
.
env
.
__NEXT_VERSION
}
`
)
const
globalStore
=
globalThis
as
typeof
globalThis
&
{
[
key
:
symbol
]:
AsyncLocalStorage
<
Store
>
|
undefined
}
return
(
globalStore
[
key
]
??
=
createAsyncLocalStorage
<
Store
>
())
}
export
function
bindSnapshot
<
T
>
(
export
function
bindSnapshot
<
T
>
(
// WARNING: Don't pass a named function to this argument! See: https://github.com/facebook/react/pull/34911
// WARNING: Don't pass a named function to this argument! See: https://github.com/facebook/react/pull/34911
fn
:
T
fn
:
T
...
...
packages/next/src/server/app-render/console-async-storage-instance.ts
View file @
5817bd1d
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
import
type
{
ConsoleAsyncStorage
}
from
'./console-async-storage.external'
import
type
{
ConsoleAsyncStorage
}
from
'./console-async-storage.external'
export
const
consoleAsyncStorageInstance
:
ConsoleAsyncStorage
=
export
const
consoleAsyncStorageInstance
:
ConsoleAsyncStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'console-async-storage'
)
packages/next/src/server/app-render/dynamic-access-async-storage-instance.ts
View file @
5817bd1d
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
import
type
{
DynamicAccessStorage
}
from
'./dynamic-access-async-storage.external'
import
type
{
DynamicAccessStorage
}
from
'./dynamic-access-async-storage.external'
export
const
dynamicAccessAsyncStorageInstance
:
DynamicAccessStorage
=
export
const
dynamicAccessAsyncStorageInstance
:
DynamicAccessStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'dynamic-access-async-storage'
)
packages/next/src/server/app-render/work-async-storage-instance.ts
View file @
5817bd1d
import
type
{
WorkAsyncStorage
}
from
'./work-async-storage.external'
import
type
{
WorkAsyncStorage
}
from
'./work-async-storage.external'
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
export
const
workAsyncStorageInstance
:
WorkAsyncStorage
=
export
const
workAsyncStorageInstance
:
WorkAsyncStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'work-async-storage'
)
packages/next/src/server/app-render/work-unit-async-storage-instance.ts
View file @
5817bd1d
import
{
create
AsyncLocalStorage
}
from
'./async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'./async-local-storage'
import
type
{
WorkUnitAsyncStorage
}
from
'./work-unit-async-storage.external'
import
type
{
WorkUnitAsyncStorage
}
from
'./work-unit-async-storage.external'
export
const
workUnitAsyncStorageInstance
:
WorkUnitAsyncStorage
=
export
const
workUnitAsyncStorageInstance
:
WorkUnitAsyncStorage
=
createAsyncLocalStorage
(
)
getOrCreateGlobalAsyncLocalStorage
(
'work-unit-async-storage'
)
packages/next/src/server/lib/trace/request-insights-identity.ts
View file @
5817bd1d
import
type
{
AsyncLocalStorage
}
from
'async_hooks'
import
type
{
AsyncLocalStorage
}
from
'async_hooks'
import
type
{
RequestInsightKind
}
from
'../../../next-devtools/shared/request-insights'
import
type
{
RequestInsightKind
}
from
'../../../next-devtools/shared/request-insights'
import
{
create
AsyncLocalStorage
}
from
'../../app-render/async-local-storage'
import
{
getOrCreateGlobal
AsyncLocalStorage
}
from
'../../app-render/async-local-storage'
export
type
RequestInsightsIdentity
=
{
export
type
RequestInsightsIdentity
=
{
requestId
:
string
requestId
:
string
...
@@ -12,17 +12,8 @@ export type RequestInsightsIdentity = {
...
@@ -12,17 +12,8 @@ export type RequestInsightsIdentity = {
// This storage covers the part of BaseServer request handling that runs before
// This storage covers the part of BaseServer request handling that runs before
// App Render creates workAsyncStorage. Once available, workStore remains the
// App Render creates workAsyncStorage. Once available, workStore remains the
// primary identity source for locally recorded spans.
// primary identity source for locally recorded spans.
const
REQUEST_INSIGHTS_IDENTITY_STORAGE_KEY
=
Symbol
.
for
(
'@next/request-insights-identity-storage'
)
function
getRequestInsightsIdentityStorage
():
AsyncLocalStorage
<
RequestInsightsIdentity
>
{
function
getRequestInsightsIdentityStorage
():
AsyncLocalStorage
<
RequestInsightsIdentity
>
{
const
globalStore
=
globalThis
as
typeof
globalThis
&
{
return
getOrCreateGlobalAsyncLocalStorage
(
'request-insights-identity-storage'
)
[
REQUEST_INSIGHTS_IDENTITY_STORAGE_KEY
]?:
AsyncLocalStorage
<
RequestInsightsIdentity
>
}
return
(
globalStore
[
REQUEST_INSIGHTS_IDENTITY_STORAGE_KEY
]
??
=
createAsyncLocalStorage
())
}
}
export
function
runWithRequestInsightsIdentity
<
T
>
(
export
function
runWithRequestInsightsIdentity
<
T
>
(
...
...
test/development/app-dir/cache-components-dev-warmup/dev-warmup.util.ts
View file @
5817bd1d
...
@@ -25,7 +25,7 @@ export function runDevWarmupTests({
...
@@ -25,7 +25,7 @@ export function runDevWarmupTests({
:
'fixtures/without-prefetch-config'
:
'fixtures/without-prefetch-config'
describe
(
`cache-components-dev-warmup -
${
description
}
`
,
()
=>
{
describe
(
`cache-components-dev-warmup -
${
description
}
`
,
()
=>
{
const
{
next
,
isTurbopack
}
=
nextTestSetup
({
const
{
next
}
=
nextTestSetup
({
files
:
nodePath
.
join
(
__dirname
,
fixturePath
),
files
:
nodePath
.
join
(
__dirname
,
fixturePath
),
})
})
...
@@ -102,19 +102,6 @@ export function runDevWarmupTests({
...
@@ -102,19 +102,6 @@ export function runDevWarmupTests({
'AbortError: This operation was aborted'
'AbortError: This operation was aborted'
)
)
if
(
isTurbopack
)
{
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console
.
log
(
'WARNING: skipping revalidation assertions in turbopack'
)
return
}
// After a revalidation the subsequent render must discard the stale cache
// After a revalidation the subsequent render must discard the stale cache
// entries. This should not affect the environment labels once the caches
// entries. This should not affect the environment labels once the caches
// are warm again.
// are warm again.
...
@@ -171,19 +158,6 @@ export function runDevWarmupTests({
...
@@ -171,19 +158,6 @@ export function runDevWarmupTests({
'AbortError: This operation was aborted'
'AbortError: This operation was aborted'
)
)
if
(
isTurbopack
)
{
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console
.
log
(
'WARNING: skipping revalidation assertions in turbopack'
)
return
}
// After a revalidation the subsequent render must discard the stale cache
// After a revalidation the subsequent render must discard the stale cache
// entries. This should not affect the environment labels once the caches
// entries. This should not affect the environment labels once the caches
// are warm again.
// are warm again.
...
@@ -443,58 +417,52 @@ export function runDevWarmupTests({
...
@@ -443,58 +417,52 @@ export function runDevWarmupTests({
})
})
})
})
// FIXME: it seems like in Turbopack we sometimes get two instances of `workUnitAsyncStorage` --
it
(
'sync IO in the static phase'
,
async
()
=>
{
// `app-render` gets a second, newer instance, different from `io()`.
const
path
=
'/sync-io/static'
// Thus, `io()` gets an undefined `workUnitStore` and does nothing, so sync IO does not get tracked at all.
// This is likely caused by the same bug that breaks `/revalidate` (see other FIXME above),
// where a route crashes due to a missing `workStore`.
if
(
!
isTurbopack
)
{
it
(
'sync IO in the static phase'
,
async
()
=>
{
const
path
=
'/sync-io/static'
const
assertLogs
=
async
(
browser
:
Playwright
)
=>
{
const
assertLogs
=
async
(
browser
:
Playwright
)
=>
{
const
logs
=
await
browser
.
log
()
const
logs
=
await
browser
.
log
()
assertLog
(
logs
,
'after first cache'
,
'Prerender'
)
// sync IO in the static stage errors and advances to Server.
assertLog
(
logs
,
'after sync io'
,
'Server'
)
assertLog
(
logs
,
'after cache read - page'
,
'Server'
)
}
if
(
isInitialLoad
)
{
assertLog
(
logs
,
'after first cache'
,
'Prerender'
)
await
testInitialLoad
(
path
,
assertLogs
)
// sync IO in the static stage errors and advances to Server.
}
else
{
assertLog
(
logs
,
'after sync io'
,
'Server'
)
await
testNavigation
(
path
,
assertLogs
)
assertLog
(
logs
,
'after cache read - page'
,
'Server'
)
}
}
})
it
(
'sync IO in the runtime phase'
,
async
()
=>
{
if
(
isInitialLoad
)
{
const
path
=
'/sync-io/runtime'
await
testInitialLoad
(
path
,
assertLogs
)
}
else
{
await
testNavigation
(
path
,
assertLogs
)
}
})
const
assertLogs
=
async
(
browser
:
Playwright
)
=>
{
it
(
'sync IO in the runtime phase'
,
async
(
)
=>
{
const
logs
=
await
browser
.
log
()
const
path
=
'/sync-io/runtime'
assertLog
(
logs
,
'after first cache'
,
'Prerender'
)
const
assertLogs
=
async
(
browser
:
Playwright
)
=>
{
assertLog
(
logs
,
'after cookies'
,
'Prefetch'
)
const
logs
=
await
browser
.
log
()
if
(
hasRuntimePrefetch
||
partialPrefetching
)
{
// in partialPrefetching (via per-segment config or global flag),
// sync IO in the runtime stage errors and advances to Server.
assertLog
(
logs
,
'after sync io'
,
'Server'
)
assertLog
(
logs
,
'after cache read - page'
,
'Server'
)
}
else
{
// if runtime prefetching is not on, sync IO in the runtime stage does nothing.
assertLog
(
logs
,
'after sync io'
,
'Prefetch'
)
assertLog
(
logs
,
'after cache read - page'
,
'Prefetch'
)
}
}
if
(
isInitialLoad
)
{
assertLog
(
logs
,
'after first cache'
,
'Prerender'
)
await
testInitialLoad
(
path
,
assertLogs
)
assertLog
(
logs
,
'after cookies'
,
'Prefetch'
)
if
(
hasRuntimePrefetch
||
partialPrefetching
)
{
// in partialPrefetching (via per-segment config or global flag),
// sync IO in the runtime stage errors and advances to Server.
assertLog
(
logs
,
'after sync io'
,
'Server'
)
assertLog
(
logs
,
'after cache read - page'
,
'Server'
)
}
else
{
}
else
{
await
testNavigation
(
path
,
assertLogs
)
// if runtime prefetching is not on, sync IO in the runtime stage
// does nothing.
assertLog
(
logs
,
'after sync io'
,
'Prefetch'
)
assertLog
(
logs
,
'after cache read - page'
,
'Prefetch'
)
}
}
})
}
}
if
(
isInitialLoad
)
{
await
testInitialLoad
(
path
,
assertLogs
)
}
else
{
await
testNavigation
(
path
,
assertLogs
)
}
})
})
})
})
})
}
}
test/development/app-dir/cache-components-tasks/cache-components-tasks.test.ts
View file @
5817bd1d
...
@@ -16,7 +16,7 @@ describe.each([
...
@@ -16,7 +16,7 @@ describe.each([
])(
])(
'cache-components-tasks - $description'
,
'cache-components-tasks - $description'
,
({
fixturePath
,
hasRuntimePrefetch
})
=>
{
({
fixturePath
,
hasRuntimePrefetch
})
=>
{
const
{
next
,
isTurbopack
,
isNextDev
}
=
nextTestSetup
({
const
{
next
}
=
nextTestSetup
({
files
:
nodePath
.
join
(
__dirname
,
fixturePath
),
files
:
nodePath
.
join
(
__dirname
,
fixturePath
),
})
})
...
@@ -79,19 +79,6 @@ describe.each([
...
@@ -79,19 +79,6 @@ describe.each([
await
retry
(()
=>
assertLogs
(
browser
))
await
retry
(()
=>
assertLogs
(
browser
))
assertNoUnexpectedErrorsInCli
()
assertNoUnexpectedErrorsInCli
()
if
(
isNextDev
&&
isTurbopack
)
{
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console
.
log
(
'WARNING: skipping revalidation assertions in turbopack'
)
return
}
// After a revalidation the subsequent warmup render must discard stale
// After a revalidation the subsequent warmup render must discard stale
// cache entries.
// cache entries.
// This should not affect the environment labels.
// This should not affect the environment labels.
...
@@ -119,19 +106,6 @@ describe.each([
...
@@ -119,19 +106,6 @@ describe.each([
await
retry
(()
=>
assertLogs
(
browser
))
await
retry
(()
=>
assertLogs
(
browser
))
assertNoUnexpectedErrorsInCli
()
assertNoUnexpectedErrorsInCli
()
if
(
isNextDev
&&
isTurbopack
)
{
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console
.
log
(
'WARNING: skipping revalidation assertions in turbopack'
)
return
}
// After a revalidation the subsequent warmup render must discard stale
// After a revalidation the subsequent warmup render must discard stale
// cache entries.
// cache entries.
// This should not affect the environment labels.
// This should not affect the environment labels.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment