Unverified Commit 5817bd1d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Anchor the async local storage instances to global symbols (#97255)

parent c18acf5c
import type { ActionAsyncStorage } from './action-async-storage.external' import type { ActionAsyncStorage } from './action-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } from './async-local-storage'
export const actionAsyncStorageInstance: ActionAsyncStorage = export const actionAsyncStorageInstance: ActionAsyncStorage =
createAsyncLocalStorage() getOrCreateGlobalAsyncLocalStorage('action-async-storage')
import type { AfterTaskAsyncStorage } from './after-task-async-storage.external' import type { AfterTaskAsyncStorage } from './after-task-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } from './async-local-storage'
export const afterTaskAsyncStorageInstance: AfterTaskAsyncStorage = export const afterTaskAsyncStorageInstance: AfterTaskAsyncStorage =
createAsyncLocalStorage() getOrCreateGlobalAsyncLocalStorage('after-task-async-storage')
...@@ -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
......
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } 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')
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } 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')
import type { WorkAsyncStorage } from './work-async-storage.external' import type { WorkAsyncStorage } from './work-async-storage.external'
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } from './async-local-storage'
export const workAsyncStorageInstance: WorkAsyncStorage = export const workAsyncStorageInstance: WorkAsyncStorage =
createAsyncLocalStorage() getOrCreateGlobalAsyncLocalStorage('work-async-storage')
import { createAsyncLocalStorage } from './async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } 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')
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 { createAsyncLocalStorage } from '../../app-render/async-local-storage' import { getOrCreateGlobalAsyncLocalStorage } 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>(
......
...@@ -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)
}
})
}) })
}) })
} }
...@@ -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.
......
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