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 {
// This matches the hasGenerateStaticParams logic we do during build.
(!isAppPath || (prerenderedRoutes && prerenderedRoutes.length > 0))
) {
// we write the static paths to partial manifest for
// fallback handling inside of entry handler's
const rawExistingManifest = await fs.promises.readFile(
pathJoin(this.distDir, PRERENDER_MANIFEST),
const prerenderManifestPath = 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'
)
const existingManifest: PrerenderManifest =
......@@ -880,13 +886,10 @@ export default class DevServer extends Server {
allowHeader: [],
}
const updatedManifest = JSON.stringify(existingManifest)
const updatedManifest = JSON.stringify(existingManifest, null, 2)
if (updatedManifest !== rawExistingManifest) {
await fs.promises.writeFile(
pathJoin(this.distDir, PRERENDER_MANIFEST),
updatedManifest
)
fs.writeFileSync(prerenderManifestPath, updatedManifest)
}
}
this.staticPathsCache.set(pathname, value)
......
......@@ -2,7 +2,7 @@ import type { NextConfigComplete } from '../../config-shared'
import type { FilesystemDynamicRoute } from './filesystem'
import type { UnwrapPromise } from '../../../lib/coalesced-function'
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 { PropagateToWorkersField } from './types'
import type { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types'
......@@ -334,10 +334,7 @@ async function startWatcher(
i18n: nextConfig.i18n || undefined,
skipProxyUrlNormalize: nextConfig.skipProxyUrlNormalize,
}
await fs.promises.writeFile(
routesManifestPath,
JSON.stringify(routesManifest)
)
fs.writeFileSync(routesManifestPath, JSON.stringify(routesManifest))
const previewPropsManifestPath = path.join(
distDir,
......@@ -345,24 +342,23 @@ async function startWatcher(
PREVIEW_PROPS_MANIFEST
)
fs.mkdirSync(path.join(distDir, SERVER_DIRECTORY), { recursive: true })
await fs.promises.writeFile(
fs.writeFileSync(
previewPropsManifestPath,
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)
await fs.promises.writeFile(
fs.writeFileSync(
prerenderManifestPath,
JSON.stringify(
{
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
},
null,
2
)
JSON.stringify(prerenderManifest, null, 2)
)
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