Unverified Commit 3478b30f authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Fix hangs and crashes for 204 responses from route handlers and deduped fetches (#100159)

### What

`new Response(body, { status })` throws `Invalid response status code
204` for null body statuses (204, 205, 304) whenever `body` is non-null,
and that includes an empty buffer. A few places buffer a response with
`arrayBuffer()` and rebuild it, which turns a body-less response into an
empty buffer:

- **Route handler prerender with `cacheComponents`**
(`app-route/module.ts`). The throw happened inside a `.then` callback
whose rejection nothing handled, so the prerender promise never settled.
Any route handler returning 204, including the auto-implemented
`OPTIONS` handler, hung until the function timeout, and a GET returning
204 hung `next build`. We hit this on nextjs.org: every `OPTIONS
/feed.xml` logged an `unhandledRejection` and then a 60s `Vercel Runtime
Timeout Error`.
- **Serving a cached app route response**
(`build/templates/app-route.ts`), which rebuilt the response from the
stored body and returned a 500. This runs on Vercel too, right after
generation, so fixing only the prerender path would have turned the
timeouts into 500s.
- **Fetches deduped through `workStore.pendingRevalidates`**
(`patch-fetch.ts`). The origin response is buffered for any status, so
when it was a 204, every waiting caller after the first threw.

### How

- On the in-memory paths (the route handler prerender and both
`pendingRevalidates` producers), check `body === null` on the original
response and skip `arrayBuffer()` entirely, so nothing is read or
allocated. The route handler prerender also now uses
`.then().catch(reject)`, so a future throw there rejects instead of
hanging.
- Serving a cached route response checks the status with a new
`isNullBodyStatus` helper. The stored format (`.body` file,
`CachedRouteValue.body: Buffer`) stores a missing body as zero bytes, so
only the status can tell them apart. I left the format alone because
custom cache handlers and existing entries depend on it.

The fetch cache itself only stores 200 responses, so a fetch cache hit
and `createCachedPrerenderResponse` can't see a 204 and are unchanged.

### Tests

- `use-cache-route-handler-only`: a GET returning 204. Without the fix,
`next build` hangs on it and gets killed.
- `use-cache-route-handler-only`: auto-implemented `OPTIONS` on a `'use
cache'` route, deploy-only. `next start` answers non-GET/HEAD requests
to prerendered routes with a 405 before the handler runs, while Vercel
invokes the function, which is how production reached the prerender
path. Not run locally.
- `patch-fetch.test.ts`: two concurrent identical fetches during a
foreground revalidate, with the origin returning 204. Without the fix,
this throws.

<!-- NEXT_JS_LLM -->
parent 4ebb2b7a
...@@ -27,6 +27,7 @@ import { ...@@ -27,6 +27,7 @@ import {
import { BaseServerSpan } from '../../server/lib/trace/constants' import { BaseServerSpan } from '../../server/lib/trace/constants'
import { getRevalidateReason } from '../../server/instrumentation/utils' import { getRevalidateReason } from '../../server/instrumentation/utils'
import { sendResponse } from '../../server/send-response' import { sendResponse } from '../../server/send-response'
import { isNullBodyStatus } from '../../server/web/http'
import { import {
fromNodeOutgoingHttpHeaders, fromNodeOutgoingHttpHeaders,
toNodeOutgoingHttpHeaders, toNodeOutgoingHttpHeaders,
...@@ -507,14 +508,15 @@ async function handleRequest( ...@@ -507,14 +508,15 @@ async function handleRequest(
) )
} }
const status = cacheEntry.value.status || 200
// The cached body is an empty buffer for null body statuses, which the
// Response constructor rejects.
const body = isNullBodyStatus(status) ? null : cacheEntry.value.body
await sendResponse( await sendResponse(
nodeNextReq, nodeNextReq,
nodeNextRes, nodeNextRes,
// @ts-expect-error - Argument of type 'Buffer<ArrayBufferLike>' is not assignable to parameter of type 'BodyInit | null | undefined'. // @ts-expect-error - Argument of type 'Buffer<ArrayBufferLike>' is not assignable to parameter of type 'BodyInit | null | undefined'.
new Response(cacheEntry.value.body, { new Response(body, { headers, status })
headers,
status: cacheEntry.value.status || 200,
})
) )
return return
} catch (err) { } catch (err) {
......
...@@ -381,4 +381,65 @@ describe('createPatchedFetcher', () => { ...@@ -381,4 +381,65 @@ describe('createPatchedFetcher', () => {
} }
} }
}) })
it('shares a 204 response between fetches deduped during a foreground revalidate', async () => {
const mockFetch: jest.MockedFunction<typeof fetch> = jest.fn()
mockFetch.mockImplementation(
async () => new Response(null, { status: 204 })
)
const workAsyncStorage = new AsyncLocalStorage<WorkStore>()
const workUnitAsyncStorage = new AsyncLocalStorage<WorkUnitStore>()
const patchedFetch = createPatchedFetcher(mockFetch, {
workAsyncStorage,
workUnitAsyncStorage,
})
// A stale entry makes the fetch revalidate in the foreground, because the
// consumer will persist the result in a server cache.
const incrementalCache = {
get: jest.fn(async () => ({
isStale: true,
value: {
kind: 'FETCH',
data: { body: '', headers: {}, status: 200, url: '' },
revalidate: 1,
},
})),
set: jest.fn(),
generateCacheKey: jest.fn(() => 'test-cache-key'),
lock: jest.fn(() => () => {}),
} as unknown as IncrementalCache
const workStore: Partial<WorkStore> = {
page: '/',
route: '/',
incrementalCache,
}
const workUnitStore = {
type: 'prerender-legacy',
phase: 'render',
rootParams: {},
implicitTags: { tags: [], expirationsByCacheKind: new Map() },
revalidate: 1,
expire: Infinity,
stale: Infinity,
tags: [],
} as unknown as WorkUnitStore
await workAsyncStorage.run(workStore as WorkStore, () =>
workUnitAsyncStorage.run(workUnitStore, async () => {
const responses = await Promise.all([
patchedFetch('https://example.com', { cache: 'force-cache' }),
patchedFetch('https://example.com', { cache: 'force-cache' }),
])
expect(mockFetch).toHaveBeenCalledTimes(1)
for (const response of responses) {
expect(response.status).toBe(204)
expect(response.body).toBeNull()
}
})
)
})
}) })
...@@ -227,6 +227,28 @@ function trackFetchMetric( ...@@ -227,6 +227,28 @@ function trackFetchMetric(
workStore.fetchMetrics.push(metric) workStore.fetchMetrics.push(metric)
} }
type BufferedResponse = {
body: ArrayBuffer | null
headers: Headers
status: number
statusText: string
}
/**
* Buffers a response so it can be shared with deduped fetches through
* `workStore.pendingRevalidates`. Responses without a body (e.g. 204) are kept
* without one, as the Response constructor rejects even an empty buffer for
* them.
*/
async function bufferResponse(response: Response): Promise<BufferedResponse> {
return {
body: response.body === null ? null : await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText,
}
}
async function createCachedPrerenderResponse( async function createCachedPrerenderResponse(
res: Response, res: Response,
cacheKey: string, cacheKey: string,
...@@ -1125,12 +1147,7 @@ export function createPatchedFetcher( ...@@ -1125,12 +1147,7 @@ export function createPatchedFetcher(
workStore.pendingRevalidates ??= {} workStore.pendingRevalidates ??= {}
if (!workStore.pendingRevalidates[cacheKey]) { if (!workStore.pendingRevalidates[cacheKey]) {
const pendingRevalidate = doOriginalFetch(true) const pendingRevalidate = doOriginalFetch(true)
.then(async (response) => ({ .then(bufferResponse)
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText,
}))
.finally(() => { .finally(() => {
workStore.pendingRevalidates ??= {} workStore.pendingRevalidates ??= {}
delete workStore.pendingRevalidates[cacheKey || ''] delete workStore.pendingRevalidates[cacheKey || '']
...@@ -1300,12 +1317,7 @@ export function createPatchedFetcher( ...@@ -1300,12 +1317,7 @@ export function createPatchedFetcher(
workStore.pendingRevalidates[pendingRevalidateKey] workStore.pendingRevalidates[pendingRevalidateKey]
if (pendingRevalidate) { if (pendingRevalidate) {
const revalidatedResult: { const revalidatedResult: BufferedResponse = await pendingRevalidate
body: ArrayBuffer
headers: Headers
status: number
statusText: string
} = await pendingRevalidate
return new Response(revalidatedResult.body, { return new Response(revalidatedResult.body, {
headers: revalidatedResult.headers, headers: revalidatedResult.headers,
status: revalidatedResult.status, status: revalidatedResult.status,
...@@ -1329,15 +1341,7 @@ export function createPatchedFetcher( ...@@ -1329,15 +1341,7 @@ export function createPatchedFetcher(
.then(cloneResponse) .then(cloneResponse)
pendingRevalidate = pendingResponse pendingRevalidate = pendingResponse
.then(async (responses) => { .then((responses) => bufferResponse(responses[0]))
const response = responses[0]
return {
body: await response.arrayBuffer(),
headers: response.headers,
status: response.status,
statusText: response.statusText,
}
})
.finally(() => { .finally(() => {
// If the pending revalidate is not present in the store, then // If the pending revalidate is not present in the store, then
// we have nothing to delete. // we have nothing to delete.
......
...@@ -719,20 +719,32 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -719,20 +719,32 @@ export class AppRouteRouteModule extends RouteModule<
responseHandled = true responseHandled = true
let bodyHandled = false const init: ResponseInit = {
result.arrayBuffer().then((body) => { headers: result.headers,
if (!bodyHandled) { status: result.status,
bodyHandled = true statusText: result.statusText,
}
resolve( // There's nothing to buffer, and rebuilding with the empty buffer
new Response(body, { // from `arrayBuffer()` would throw for null body statuses (e.g.
headers: result.headers, // the 204 from an auto-implemented OPTIONS handler).
status: result.status, if (result.body === null) {
statusText: result.statusText, resolve(new Response(null, init))
}) return
) }
}
}, reject) let bodyHandled = false
result
.arrayBuffer()
.then((body) => {
if (!bodyHandled) {
bodyHandled = true
resolve(new Response(body, init))
}
})
// Ensure anything thrown above rejects instead of becoming an
// unhandled rejection that leaves the prerender hanging.
.catch(reject)
scheduleImmediate(() => { scheduleImmediate(() => {
if (!bodyHandled) { if (!bodyHandled) {
bodyHandled = true bodyHandled = true
......
...@@ -28,3 +28,15 @@ export type HTTP_METHOD = (typeof HTTP_METHODS)[number] ...@@ -28,3 +28,15 @@ export type HTTP_METHOD = (typeof HTTP_METHODS)[number]
export function isHTTPMethod(maybeMethod: string): maybeMethod is HTTP_METHOD { export function isHTTPMethod(maybeMethod: string): maybeMethod is HTTP_METHOD {
return HTTP_METHODS.includes(maybeMethod as HTTP_METHOD) return HTTP_METHODS.includes(maybeMethod as HTTP_METHOD)
} }
/**
* Checks to see if the passed status is a null body status, i.e. a response
* with this status cannot be constructed with a body (not even an empty one).
* 101 and 103 are excluded because the Response constructor doesn't accept
* them at all.
*
* @see https://fetch.spec.whatwg.org/#null-body-status
*/
export function isNullBodyStatus(status: number): boolean {
return status === 204 || status === 205 || status === 304
}
export async function GET() {
return new Response(null, { status: 204 })
}
async function getCachedBody() {
'use cache'
return 'hello'
}
// Only GET is defined, so OPTIONS is auto-implemented with a 204 response.
export async function GET() {
return new Response(await getCachedBody())
}
...@@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi' ...@@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi'
// Explicitly don't mix route handlers with pages in this test app, to make sure // Explicitly don't mix route handlers with pages in this test app, to make sure
// that this also works in isolation. // that this also works in isolation.
describe('use-cache-route-handler-only', () => { describe('use-cache-route-handler-only', () => {
const { next, isNextDeploy } = nextTestSetup({ const { next, isNextDeploy, isNextStart } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -97,6 +97,40 @@ describe('use-cache-route-handler-only', () => { ...@@ -97,6 +97,40 @@ describe('use-cache-route-handler-only', () => {
expect(getOutput()).toIncludeRepeated(escapeStringRegexp(generating), 1) expect(getOutput()).toIncludeRepeated(escapeStringRegexp(generating), 1)
}) })
it('should respond to auto-implemented OPTIONS requests', async () => {
const response = await next.fetch('/options-auto-implemented', {
method: 'OPTIONS',
})
if (isNextStart) {
// TODO: `next start` answers non-GET/HEAD requests for prerendered
// routes with a 405 before the route handler runs, because the method
// guard in `BaseServer.renderToResponseWithComponentsImpl` treats any
// route in the prerender manifest as SSG. On Vercel the request invokes
// the function, and in dev the route isn't treated as SSG.
expect(response.status).toBe(405)
expect(response.headers.get('allow')).toBe('GET, HEAD')
} else {
expect(response.status).toBe(204)
expect(await response.text()).toBe('')
if (!isNextDeploy) {
// In dev, the auto-implemented OPTIONS handler runs and sets the
// `Allow` header. On Vercel the prerendered route is served from the
// CDN, which answers the preflight with a bare 204 without invoking
// the function, so the `Allow` header never reaches the client.
expect(response.headers.get('allow')).toContain('GET')
}
}
})
it('should prerender route handlers that respond with 204', async () => {
const response = await next.fetch('/no-content')
expect(response.status).toBe(204)
expect(await response.text()).toBe('')
})
it('should be able to revalidate prerendered route handlers', async () => { it('should be able to revalidate prerendered route handlers', async () => {
const response1 = await next.fetch('/node') const response1 = await next.fetch('/node')
const { date1: date1a } = await response1.json() const { date1: date1a } = await response1.json()
......
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