Commit 3233772f authored by Janka Uryga's avatar Janka Uryga

debug logs for repro

parent 57d2367c
......@@ -3108,6 +3108,10 @@ async function renderWithRestartOnCacheMissInDev(
// where sync IO does not cause aborts, so it's okay if it happens before render.
const initialRscPayload = await getPayload(requestStore)
console.log(
`******** app-render :: using workUnitAsyncStorage [id: ${workUnitAsyncStorage['__INSTANCE_ID']}]`
)
const maybeInitialServerStream = await workUnitAsyncStorage.run(
requestStore,
() =>
......@@ -3150,6 +3154,9 @@ async function renderWithRestartOnCacheMissInDev(
if (initialStageController.currentStage === RenderStage.Abandoned) {
// If we abandoned the render in the static stage, we won't proceed further.
console.log(
'******** app-render :: render was abandoned during static stage'
)
return null
}
......@@ -3161,6 +3168,9 @@ async function renderWithRestartOnCacheMissInDev(
// Regardless of whether we are going to abandon this
// render we need the unblock runtime b/c it's essential
// filling caches.
console.log(
'******** app-render :: abandoning after static stage due to cache miss'
)
initialStageController.abandonRender()
return null
}
......@@ -3228,6 +3238,8 @@ async function renderWithRestartOnCacheMissInDev(
await cacheSignal.cacheReady()
initialReactController.abort()
console.log('******** app-render :: restarting')
//===============================================
// Final render (restarted)
//===============================================
......
......@@ -85,11 +85,21 @@ export class StagedRenderingController {
// so we want a slightly different flow.
// See the implementation of `abandonRenderImpl` for more explanation.
if (this.mayAbandon) {
console.log(
'abandoning initial render due to sync IO',
RenderStage[this.currentStage],
reason
)
return this.abandonRenderImpl()
}
// If we're in the final render, we cannot abandon it. We need to advance to the Dynamic stage
// and capture the interruption reason.
console.log(
'aborting final render due to sync IO',
RenderStage[this.currentStage],
reason
)
switch (this.currentStage) {
case RenderStage.Static: {
this.staticInterruptReason = reason
......
import { createAsyncLocalStorage } from './async-local-storage'
import type { WorkUnitAsyncStorage } from './work-unit-async-storage.external'
export const workUnitAsyncStorageInstance: WorkUnitAsyncStorage =
const instances: Set<WorkUnitAsyncStorage> =
globalThis.__WORK_UNIT_STORE_INSTANCES ??
(globalThis.__WORK_UNIT_STORE_INSTANCES = new Set())
const workUnitAsyncStorageInstance: WorkUnitAsyncStorage =
createAsyncLocalStorage()
instances.add(workUnitAsyncStorageInstance)
console.log(
`created workUnitAsyncStorage (instances: ${instances.size}, module id: ${module.id})`
)
workUnitAsyncStorageInstance.__INSTANCE_ID = instances.size
export { workUnitAsyncStorageInstance }
......@@ -397,6 +397,8 @@ class NextTracerImpl implements NextTracer {
const [name, options, fn] =
args.length === 3 ? args : [args[0], {}, args[1]]
return fn
if (
!NextVanillaSpanAllowlist.includes(name) &&
process.env.NEXT_OTEL_VERBOSE !== '1'
......
......@@ -30,6 +30,7 @@ function createDate(originalConstructor: typeof Date): typeof Date {
const newConstructor = Object.defineProperties(
// Ideally this should not minify the name.
function Date() {
console.log('hello from patched `Date`')
if (new.target === undefined) {
io('`Date()`', 'time')
return apply(originalConstructor, undefined, arguments)
......@@ -48,8 +49,10 @@ function createDate(originalConstructor: typeof Date): typeof Date {
}
try {
console.log('--------- patching Date')
// eslint-disable-next-line no-native-reassign
Date = createDate(Date)
Date.__IS_PATCHED = true
} catch {
console.error(
'Failed to install `Date` class extension. When using `cacheComponents`, APIs that read the current time will not correctly trigger dynamic behavior.'
......
......@@ -10,6 +10,7 @@ import { io } from './utils'
const expression = '`Math.random()`'
try {
console.log('--------- patching Math')
const _random = Math.random
Math.random = function random() {
io(expression, 'random')
......@@ -19,6 +20,7 @@ try {
// eslint-disable-next-line no-extra-bind
}.bind(null)
Object.defineProperty(Math.random, 'name', { value: 'random' })
Math.__IS_PATCHED = true
} catch {
console.error(
`Failed to install ${expression} extension. When using \`cacheComponents\` calling this function will not correctly trigger dynamic behavior.`
......
......@@ -12,7 +12,16 @@ export function io(expression: string, type: ApiType) {
const workUnitStore = workUnitAsyncStorage.getStore()
const workStore = workAsyncStorage.getStore()
if (expression === '`new Date()`') {
console.log(`io() - ${expression}`)
}
if (!workUnitStore || !workStore) {
if (expression === '`new Date()`') {
console.log(
`io() - ${expression} - (workUnitStore [id: ${workUnitAsyncStorage['__INSTANCE_ID']}]: ${!!workUnitStore}, workStore: ${!!workStore})`
)
}
return
}
......@@ -85,6 +94,14 @@ export function io(expression: string, type: ApiType) {
case 'request':
if (process.env.NODE_ENV === 'development') {
const stageController = workUnitStore.stagedRendering
if (stageController) {
console.log(`io() - ${expression} - staged`, {
canSyncInterrupt: stageController.canSyncInterrupt(),
currentStage: RenderStage[stageController.currentStage],
mayAbandon: stageController['mayAbandon'],
hasRuntimePrefetch: stageController['hasRuntimePrefetch'],
})
}
if (stageController && stageController.canSyncInterrupt()) {
let message: string
if (stageController.currentStage === RenderStage.Static) {
......
......@@ -309,7 +309,7 @@ describe.each([
}
})
it('sync IO in the static phase', async () => {
it.only('sync IO in the static phase', async () => {
const path = '/sync-io/static'
const assertLogs = async (browser: Playwright) => {
......@@ -328,7 +328,7 @@ describe.each([
}
})
it('sync IO in the runtime phase', async () => {
it.only('sync IO in the runtime phase', async () => {
const path = '/sync-io/runtime'
const assertLogs = async (browser: Playwright) => {
......
import { Suspense } from 'react'
import { CachedData, getCachedData } from '../../data-fetching'
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: [{}] }
......@@ -24,7 +25,10 @@ async function Runtime() {
await cookies()
console.log(`after cookies`)
Date.now()
console.log(
`before sync io - ${Date.__IS_PATCHED ? 'Date is patched' : 'Date is NOT PATCHED!!!!!!!'}, stage: ${workUnitAsyncStorage.getStore()?.['stagedRendering']?.['currentStage']}`
)
new Date()
console.log(`after sync io`)
return <CachedData label="page" cacheKey={CACHE_KEY} />
......
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
import { CachedData, getCachedData } from '../../data-fetching'
export const unstable_prefetch = { mode: 'runtime', samples: [{}] }
......@@ -8,7 +9,10 @@ export default async function Page() {
await getCachedData(CACHE_KEY + '-1')
console.log(`after first cache`)
Date.now()
console.log(
`before sync io - ${Date.__IS_PATCHED ? 'Date is patched' : 'Date is NOT PATCHED!!!!!!!'}, stage: ${workUnitAsyncStorage.getStore()?.['stagedRendering']?.['currentStage']}`
)
new Date()
console.log(`after sync io`)
return (
......
import { Suspense } from 'react'
import { CachedData, getCachedData } from '../../data-fetching'
import { cookies } from 'next/headers'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
const CACHE_KEY = __dirname + '/__PAGE__'
......@@ -22,7 +23,10 @@ async function Runtime() {
await cookies()
console.log(`after cookies`)
Date.now()
console.log(
`before sync io - ${Date.__IS_PATCHED ? 'Date is patched' : 'Date is NOT PATCHED!!!!!!!'}, stage: ${workUnitAsyncStorage.getStore()?.['stagedRendering']?.['currentStage']}`
)
new Date()
console.log(`after sync io`)
return <CachedData label="page" cacheKey={CACHE_KEY} />
......
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
import { CachedData, getCachedData } from '../../data-fetching'
const CACHE_KEY = __dirname + '/__PAGE__'
......@@ -6,7 +7,10 @@ export default async function Page() {
await getCachedData(CACHE_KEY + '-1')
console.log(`after first cache`)
Date.now()
console.log(
`before sync io - ${Date.__IS_PATCHED ? 'Date is patched' : 'Date is NOT PATCHED!!!!!!!'}, stage: ${workUnitAsyncStorage.getStore()?.['stagedRendering']?.['currentStage']}`
)
new Date()
console.log(`after sync io`)
return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment