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
4f0a2b98
Commit
4f0a2b98
authored
Jan 05, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
foo
parent
a7bccf3a
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
788 additions
and
7 deletions
+788
-7
FLAKY_TEST_INVESTIGATION.md
FLAKY_TEST_INVESTIGATION.md
+376
-0
layout.tsx
...e/app-dir/segment-cache/async-context-loss/app/layout.tsx
+11
-0
page.tsx
...e2e/app-dir/segment-cache/async-context-loss/app/page.tsx
+26
-0
page.tsx
...egment-cache/async-context-loss/app/test-cookies/page.tsx
+53
-0
page.tsx
...ache/async-context-loss/app/test-generic-promise/page.tsx
+66
-0
page.tsx
...egment-cache/async-context-loss/app/test-headers/page.tsx
+55
-0
page.tsx
...ment-cache/async-context-loss/app/test-page/[id]/page.tsx
+55
-0
async-context-loss.test.ts
...gment-cache/async-context-loss/async-context-loss.test.ts
+137
-0
next.config.js
...e/app-dir/segment-cache/async-context-loss/next.config.js
+8
-0
page.tsx
...rs/sync-io-after-runtime-api/dynamic-params/[id]/page.tsx
+1
-7
No files found.
FLAKY_TEST_INVESTIGATION.md
0 → 100644
View file @
4f0a2b98
This diff is collapsed.
Click to expand it.
test/e2e/app-dir/segment-cache/async-context-loss/app/layout.tsx
0 → 100644
View file @
4f0a2b98
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/app/page.tsx
0 → 100644
View file @
4f0a2b98
import
Link
from
'next/link'
export
default
function
Home
()
{
return
(
<
main
>
<
h1
>
AsyncLocalStorage Context Loss Test
</
h1
>
<
p
>
Click a link to trigger runtime prefetch and test context preservation:
</
p
>
<
ul
>
<
li
>
<
Link
href=
"/test-page/123"
>
Test with params
</
Link
>
</
li
>
<
li
>
<
Link
href=
"/test-cookies"
>
Test with cookies()
</
Link
>
</
li
>
<
li
>
<
Link
href=
"/test-headers"
>
Test with headers()
</
Link
>
</
li
>
<
li
>
<
Link
href=
"/test-generic-promise"
>
Test with generic promise
</
Link
>
</
li
>
</
ul
>
</
main
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/app/test-cookies/page.tsx
0 → 100644
View file @
4f0a2b98
import
{
Suspense
}
from
'react'
import
{
cookies
}
from
'next/headers'
import
{
workUnitAsyncStorage
}
from
'next/dist/server/app-render/work-unit-async-storage.external'
export
const
unstable_prefetch
=
{
mode
:
'runtime'
,
samples
:
[{
cookies
:
[]
}],
}
export
default
async
function
Page
()
{
return
(
<
main
>
<
h1
>
Cookies Context Loss Test
</
h1
>
<
Suspense
fallback=
{
<
div
>
Loading...
</
div
>
}
>
<
TestComponent
/>
</
Suspense
>
</
main
>
)
}
async
function
TestComponent
()
{
const
contextBefore
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[COOKIES] CONTEXT_BEFORE_AWAIT:'
,
contextBefore
?.
type
??
'undefined'
)
// Await cookies - triggers runtime stage delay
const
cookieStore
=
await
cookies
()
const
contextAfter
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[COOKIES] CONTEXT_AFTER_AWAIT:'
,
contextAfter
?.
type
??
'undefined'
)
if
(
contextAfter
===
undefined
)
{
console
.
log
(
'[COOKIES] CONTEXT_STATUS: LOST'
)
}
else
{
console
.
log
(
'[COOKIES] CONTEXT_STATUS: PRESERVED'
)
}
const
timestamp
=
Date
.
now
()
return
(
<
div
>
<
p
id=
"result"
>
Cookies count:
{
cookieStore
.
getAll
().
length
}
</
p
>
<
p
>
Timestamp:
{
timestamp
}
</
p
>
<
p
>
Context Before:
{
contextBefore
?.
type
??
'undefined'
}
</
p
>
<
p
>
Context After:
{
contextAfter
?.
type
??
'undefined'
}
</
p
>
</
div
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/app/test-generic-promise/page.tsx
0 → 100644
View file @
4f0a2b98
import
{
Suspense
}
from
'react'
import
{
cookies
}
from
'next/headers'
import
{
workUnitAsyncStorage
}
from
'next/dist/server/app-render/work-unit-async-storage.external'
// A simple promise that resolves after a microtask
// This demonstrates the issue is with React's scheduler, not specific to Next.js APIs
function
createDelayedPromise
<
T
>
(
value
:
T
):
Promise
<
T
>
{
return
new
Promise
((
resolve
)
=>
{
queueMicrotask
(()
=>
resolve
(
value
))
})
}
export
const
unstable_prefetch
=
{
mode
:
'runtime'
,
samples
:
[{
cookies
:
[]
}],
}
export
default
async
function
Page
()
{
return
(
<
main
>
<
h1
>
Generic Promise Context Loss Test
</
h1
>
<
Suspense
fallback=
{
<
div
>
Loading...
</
div
>
}
>
<
TestComponent
/>
</
Suspense
>
</
main
>
)
}
async
function
TestComponent
()
{
// First access cookies to make this a valid dynamic page
const
cookieStore
=
await
cookies
()
const
contextBefore
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[GENERIC] CONTEXT_BEFORE_AWAIT:'
,
contextBefore
?.
type
??
'undefined'
)
// Await a generic promise AFTER the runtime API
const
data
=
await
createDelayedPromise
({
message
:
'Hello from promise'
})
const
contextAfter
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[GENERIC] CONTEXT_AFTER_AWAIT:'
,
contextAfter
?.
type
??
'undefined'
)
if
(
contextAfter
===
undefined
)
{
console
.
log
(
'[GENERIC] CONTEXT_STATUS: LOST'
)
}
else
{
console
.
log
(
'[GENERIC] CONTEXT_STATUS: PRESERVED'
)
}
// Now Date.now() is valid because we already accessed cookies
const
timestamp
=
Date
.
now
()
return
(
<
div
>
<
p
id=
"result"
>
Message:
{
data
.
message
}
</
p
>
<
p
>
Cookies:
{
cookieStore
.
getAll
().
length
}
</
p
>
<
p
>
Timestamp:
{
timestamp
}
</
p
>
<
p
>
Context Before:
{
contextBefore
?.
type
??
'undefined'
}
</
p
>
<
p
>
Context After:
{
contextAfter
?.
type
??
'undefined'
}
</
p
>
</
div
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/app/test-headers/page.tsx
0 → 100644
View file @
4f0a2b98
import
{
Suspense
}
from
'react'
import
{
headers
}
from
'next/headers'
import
{
workUnitAsyncStorage
}
from
'next/dist/server/app-render/work-unit-async-storage.external'
export
const
unstable_prefetch
=
{
mode
:
'runtime'
,
samples
:
[{
cookies
:
[]
}],
}
export
default
async
function
Page
()
{
return
(
<
main
>
<
h1
>
Headers Context Loss Test
</
h1
>
<
Suspense
fallback=
{
<
div
>
Loading...
</
div
>
}
>
<
TestComponent
/>
</
Suspense
>
</
main
>
)
}
async
function
TestComponent
()
{
const
contextBefore
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[HEADERS] CONTEXT_BEFORE_AWAIT:'
,
contextBefore
?.
type
??
'undefined'
)
// Await headers - triggers runtime stage delay
const
headerStore
=
await
headers
()
const
contextAfter
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[HEADERS] CONTEXT_AFTER_AWAIT:'
,
contextAfter
?.
type
??
'undefined'
)
if
(
contextAfter
===
undefined
)
{
console
.
log
(
'[HEADERS] CONTEXT_STATUS: LOST'
)
}
else
{
console
.
log
(
'[HEADERS] CONTEXT_STATUS: PRESERVED'
)
}
const
timestamp
=
Date
.
now
()
return
(
<
div
>
<
p
id=
"result"
>
User-Agent:
{
headerStore
.
get
(
'user-agent'
)?.
slice
(
0
,
20
)
}
...
</
p
>
<
p
>
Timestamp:
{
timestamp
}
</
p
>
<
p
>
Context Before:
{
contextBefore
?.
type
??
'undefined'
}
</
p
>
<
p
>
Context After:
{
contextAfter
?.
type
??
'undefined'
}
</
p
>
</
div
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/app/test-page/[id]/page.tsx
0 → 100644
View file @
4f0a2b98
import
{
Suspense
}
from
'react'
import
{
workUnitAsyncStorage
}
from
'next/dist/server/app-render/work-unit-async-storage.external'
type
Params
=
{
id
:
string
}
export
const
unstable_prefetch
=
{
mode
:
'runtime'
,
samples
:
[{
cookies
:
[]
}],
}
export
default
async
function
Page
({
params
}:
{
params
:
Promise
<
Params
>
})
{
return
(
<
main
>
<
h1
>
Params Context Loss Test
</
h1
>
<
Suspense
fallback=
{
<
div
>
Loading...
</
div
>
}
>
<
TestComponent
params=
{
params
}
/>
</
Suspense
>
</
main
>
)
}
async
function
TestComponent
({
params
}:
{
params
:
Promise
<
Params
>
})
{
const
contextBefore
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[PARAMS] CONTEXT_BEFORE_AWAIT:'
,
contextBefore
?.
type
??
'undefined'
)
// Await params - this triggers delayUntilRuntimeStage
// With the macrotask boundary fix, this will force context loss
const
resolvedParams
=
await
params
const
contextAfter
=
workUnitAsyncStorage
.
getStore
()
console
.
log
(
'[PARAMS] CONTEXT_AFTER_AWAIT:'
,
contextAfter
?.
type
??
'undefined'
)
if
(
contextAfter
===
undefined
)
{
console
.
log
(
'[PARAMS] CONTEXT_STATUS: LOST'
)
}
else
{
console
.
log
(
'[PARAMS] CONTEXT_STATUS: PRESERVED'
)
}
const
timestamp
=
Date
.
now
()
return
(
<
div
>
<
p
id=
"result"
>
Param ID:
{
resolvedParams
.
id
}
</
p
>
<
p
>
Timestamp:
{
timestamp
}
</
p
>
<
p
>
Context Before:
{
contextBefore
?.
type
??
'undefined'
}
</
p
>
<
p
>
Context After:
{
contextAfter
?.
type
??
'undefined'
}
</
p
>
</
div
>
)
}
test/e2e/app-dir/segment-cache/async-context-loss/async-context-loss.test.ts
0 → 100644
View file @
4f0a2b98
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
createRouterAct
}
from
'router-act'
/**
* This test demonstrates the AsyncLocalStorage context loss issue
* when React's scheduler continues a suspended component.
*
* The test uses the same infrastructure as the flaky prefetch-runtime tests
* to ensure we're hitting the runtime prefetch code path.
*/
describe
(
'AsyncLocalStorage context loss in React scheduler'
,
()
=>
{
const
{
next
,
isNextDev
,
skipped
}
=
nextTestSetup
({
files
:
__dirname
,
skipDeployment
:
true
,
})
if
(
isNextDev
||
skipped
)
{
test
(
'skipped'
,
()
=>
{})
return
}
it
(
'loses context after awaiting params in runtime prefetch'
,
async
()
=>
{
let
act
:
ReturnType
<
typeof
createRouterAct
>
const
browser
=
await
next
.
browser
(
'/'
,
{
beforePageLoad
(
page
)
{
act
=
createRouterAct
(
page
)
},
})
// Navigate to the params test page via prefetch
// This triggers runtime prefetch on the server
await
act
(
async
()
=>
{
// Navigate to the dynamic params page
await
browser
.
elementByCss
(
'a[href="/test-page/123"]'
).
click
()
},
{
// We expect the page to render with the result
includes
:
'Param ID: 123'
,
}
)
// Check server logs for context status
const
logs
=
next
.
cliOutput
// Look for the context logs during runtime prefetch
const
hasContextLost
=
logs
.
includes
(
'[PARAMS] CONTEXT_STATUS: LOST'
)
const
hasContextPreserved
=
logs
.
includes
(
'[PARAMS] CONTEXT_STATUS: PRESERVED'
)
console
.
log
(
'Server logs contain CONTEXT_STATUS: LOST:'
,
hasContextLost
)
console
.
log
(
'Server logs contain CONTEXT_STATUS: PRESERVED:'
,
hasContextPreserved
)
// If context is lost, this test fails - demonstrating the bug
if
(
hasContextLost
)
{
console
.
log
(
'
\
n=== BUG CONFIRMED ==='
)
console
.
log
(
'AsyncLocalStorage context is LOST after awaiting params'
)
console
.
log
(
'This causes io() to fail to detect sync IO operations'
)
console
.
log
(
'=====================
\
n'
)
}
expect
(
hasContextLost
).
toBe
(
false
)
expect
(
hasContextPreserved
).
toBe
(
true
)
})
it
(
'loses context after awaiting cookies() in runtime prefetch'
,
async
()
=>
{
let
act
:
ReturnType
<
typeof
createRouterAct
>
const
browser
=
await
next
.
browser
(
'/'
,
{
beforePageLoad
(
page
)
{
act
=
createRouterAct
(
page
)
},
})
await
act
(
async
()
=>
{
await
browser
.
elementByCss
(
'a[href="/test-cookies"]'
).
click
()
},
{
includes
:
'Cookies count:'
,
}
)
const
logs
=
next
.
cliOutput
const
hasContextLost
=
logs
.
includes
(
'[COOKIES] CONTEXT_STATUS: LOST'
)
const
hasContextPreserved
=
logs
.
includes
(
'[COOKIES] CONTEXT_STATUS: PRESERVED'
)
console
.
log
(
'COOKIES - Context Lost:'
,
hasContextLost
,
'Preserved:'
,
hasContextPreserved
)
expect
(
hasContextLost
).
toBe
(
false
)
expect
(
hasContextPreserved
).
toBe
(
true
)
})
it
(
'loses context after awaiting headers() in runtime prefetch'
,
async
()
=>
{
let
act
:
ReturnType
<
typeof
createRouterAct
>
const
browser
=
await
next
.
browser
(
'/'
,
{
beforePageLoad
(
page
)
{
act
=
createRouterAct
(
page
)
},
})
await
act
(
async
()
=>
{
await
browser
.
elementByCss
(
'a[href="/test-headers"]'
).
click
()
},
{
includes
:
'User-Agent:'
,
}
)
const
logs
=
next
.
cliOutput
const
hasContextLost
=
logs
.
includes
(
'[HEADERS] CONTEXT_STATUS: LOST'
)
const
hasContextPreserved
=
logs
.
includes
(
'[HEADERS] CONTEXT_STATUS: PRESERVED'
)
console
.
log
(
'HEADERS - Context Lost:'
,
hasContextLost
,
'Preserved:'
,
hasContextPreserved
)
expect
(
hasContextLost
).
toBe
(
false
)
expect
(
hasContextPreserved
).
toBe
(
true
)
})
})
test/e2e/app-dir/segment-cache/async-context-loss/next.config.js
0 → 100644
View file @
4f0a2b98
/** @type {import('next').NextConfig} */
module
.
exports
=
{
experimental
:
{
dynamicIO
:
true
,
clientSegmentCache
:
true
,
cacheComponents
:
true
,
},
}
test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/errors/sync-io-after-runtime-api/dynamic-params/[id]/page.tsx
View file @
4f0a2b98
import
{
Suspense
}
from
'react'
import
{
Suspense
}
from
'react'
import
{
DebugRenderKind
}
from
'../../../../../shared'
import
{
DebugRenderKind
}
from
'../../../../../shared'
import
{
workUnitAsyncStorage
}
from
'next/dist/server/app-render/work-unit-async-storage.external'
type
Params
=
{
id
:
string
}
type
Params
=
{
id
:
string
}
...
@@ -25,11 +24,6 @@ export default async function Page({ params }: { params: Promise<Params> }) {
...
@@ -25,11 +24,6 @@ export default async function Page({ params }: { params: Promise<Params> }) {
}
}
async
function
RuntimePrefetchable
({
params
}:
{
params
:
Promise
<
Params
>
})
{
async
function
RuntimePrefetchable
({
params
}:
{
params
:
Promise
<
Params
>
})
{
const
res
=
await
params
await
params
console
.
log
(
'RuntimePrefetchable :: awaited params'
,
res
,
workUnitAsyncStorage
.
getStore
()
)
return
<
div
id=
"timestamp"
>
Timestamp:
{
Date
.
now
()
}
</
div
>
return
<
div
id=
"timestamp"
>
Timestamp:
{
Date
.
now
()
}
</
div
>
}
}
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