Unverified Commit 269a6915 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

Fix prerender manifest update race in dev (#100521)

in dev, we update the prerender manifest based on the
`generateStaticParams` result. we were using
`fs.promises.{readFile,writeFile}` for this, which made it possible for
parallel requests to interfere with each other -- when a file is opened
for writing by `fs.promises.writeFile`, it gets truncated, and if
another request tries to read the manifest in the meantime, it can
observe an empty file. this [has caused intermittent
crashes](https://app.datadoghq.com/ci/ci-cd/explorer/executions?query=test_level%3Atest%20%40git.repository.id%3A%22github.com%2Fvercel%2Fnext.js%22%20%40test.name%3A%22use-cache-fallback-root-params%20resolves%20cached%20and%20uncached%20roots%20on%20ordinary%20requests%22%20%40test.status%3A%22fail%22&agg_m=count&agg_m_source=base&agg_t=count&ci_cd_explorer_tab=tests&currentTab=overview&eventStack=&fromUser=true&start=1789839150900&end=1792431150900&paused=false)
in `use-cache-fallback-root-params > resolves cached and uncached roots
on ordinary requests` which has a couple requests hitting the same route
in parallel with different params and attempting to update the manifest.

switching the reads/writes to be synchronous avoids this problem. i was
unable to reproduce this race in 100 runs of
`use-cache-fallback-root-params`, which previously would get a failure
in around 20 runs
parent 691ec2a1
...@@ -840,10 +840,16 @@ export default class DevServer extends Server { ...@@ -840,10 +840,16 @@ export default class DevServer extends Server {
// This matches the hasGenerateStaticParams logic we do during build. // This matches the hasGenerateStaticParams logic we do during build.
(!isAppPath || (prerenderedRoutes && prerenderedRoutes.length > 0)) (!isAppPath || (prerenderedRoutes && prerenderedRoutes.length > 0))
) { ) {
// we write the static paths to partial manifest for const prerenderManifestPath = pathJoin(
// fallback handling inside of entry handler's this.distDir,
const rawExistingManifest = await fs.promises.readFile( PRERENDER_MANIFEST
pathJoin(this.distDir, PRERENDER_MANIFEST), )
// We write the static paths to the partial manifest for fallback handling
// inside of entry handlers
// NOTE: We're mutating a shared manifest, so we use sync reads/writes
// without any awaits in between to avoid races with other requests
const rawExistingManifest = fs.readFileSync(
prerenderManifestPath,
'utf8' 'utf8'
) )
const existingManifest: PrerenderManifest = const existingManifest: PrerenderManifest =
...@@ -880,13 +886,10 @@ export default class DevServer extends Server { ...@@ -880,13 +886,10 @@ export default class DevServer extends Server {
allowHeader: [], allowHeader: [],
} }
const updatedManifest = JSON.stringify(existingManifest) const updatedManifest = JSON.stringify(existingManifest, null, 2)
if (updatedManifest !== rawExistingManifest) { if (updatedManifest !== rawExistingManifest) {
await fs.promises.writeFile( fs.writeFileSync(prerenderManifestPath, updatedManifest)
pathJoin(this.distDir, PRERENDER_MANIFEST),
updatedManifest
)
} }
} }
this.staticPathsCache.set(pathname, value) this.staticPathsCache.set(pathname, value)
......
...@@ -2,7 +2,7 @@ import type { NextConfigComplete } from '../../config-shared' ...@@ -2,7 +2,7 @@ import type { NextConfigComplete } from '../../config-shared'
import type { FilesystemDynamicRoute } from './filesystem' import type { FilesystemDynamicRoute } from './filesystem'
import type { UnwrapPromise } from '../../../lib/coalesced-function' import type { UnwrapPromise } from '../../../lib/coalesced-function'
import type { ProxyMatcher } from '../../../build/analysis/get-page-static-info' import type { ProxyMatcher } from '../../../build/analysis/get-page-static-info'
import type { RoutesManifest } from '../../../build' import type { PrerenderManifest, RoutesManifest } from '../../../build'
import type { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher' import type { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher'
import type { PropagateToWorkersField } from './types' import type { PropagateToWorkersField } from './types'
import type { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types' import type { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types'
...@@ -334,10 +334,7 @@ async function startWatcher( ...@@ -334,10 +334,7 @@ async function startWatcher(
i18n: nextConfig.i18n || undefined, i18n: nextConfig.i18n || undefined,
skipProxyUrlNormalize: nextConfig.skipProxyUrlNormalize, skipProxyUrlNormalize: nextConfig.skipProxyUrlNormalize,
} }
await fs.promises.writeFile( fs.writeFileSync(routesManifestPath, JSON.stringify(routesManifest))
routesManifestPath,
JSON.stringify(routesManifest)
)
const previewPropsManifestPath = path.join( const previewPropsManifestPath = path.join(
distDir, distDir,
...@@ -345,24 +342,23 @@ async function startWatcher( ...@@ -345,24 +342,23 @@ async function startWatcher(
PREVIEW_PROPS_MANIFEST PREVIEW_PROPS_MANIFEST
) )
fs.mkdirSync(path.join(distDir, SERVER_DIRECTORY), { recursive: true }) fs.mkdirSync(path.join(distDir, SERVER_DIRECTORY), { recursive: true })
await fs.promises.writeFile( fs.writeFileSync(
previewPropsManifestPath, previewPropsManifestPath,
JSON.stringify(opts.fsChecker.previewProps, null, 2) JSON.stringify(opts.fsChecker.previewProps, null, 2)
) )
const prerenderManifest: Omit<PrerenderManifest, 'preview'> = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
// Technically required in the manifest type, but we don't provide it here
// preview: ...
}
const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST) const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST)
await fs.promises.writeFile( fs.writeFileSync(
prerenderManifestPath, prerenderManifestPath,
JSON.stringify( JSON.stringify(prerenderManifest, null, 2)
{
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
},
null,
2
)
) )
if (opts.nextConfig.experimental.nextScriptWorkers) { if (opts.nextConfig.experimental.nextScriptWorkers) {
......
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