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

debug logs for repro

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