Unverified Commit c8540b5e authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Release unclaimed parked dev state for HTML requests after a timeout

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 44525146
...@@ -38,14 +38,43 @@ export function createNodeDebugChannel(): NodeDebugChannelPair { ...@@ -38,14 +38,43 @@ export function createNodeDebugChannel(): NodeDebugChannelPair {
// writable half is still open — so the debug channel would never close on the // writable half is still open — so the debug channel would never close on the
// client. Ending it via `passthrough.end()` closes both halves and the close // client. Ending it via `passthrough.end()` closes both halves and the close
// propagates. // propagates.
//
// The client side might be destroyed while React is still writing to the
// debug channel, e.g. when nobody claimed it in time (see
// `releaseParkedStream`). In that case, we drop the remaining chunks instead
// of erroring the server side, which React doesn't expect.
const passthrough = new PassThrough() const passthrough = new PassThrough()
// The callback of the write (or end) that's currently forwarded to the
// PassThrough. If the PassThrough is destroyed, a pending write's callback
// might never be called (e.g. when it's waiting for a reader), or it's called
// with an error, so we settle it ourselves when the PassThrough closes.
let pendingCallback: ((error?: Error | null) => void) | undefined
const settlePendingCallback = (error?: Error | null) => {
const callback = pendingCallback
pendingCallback = undefined
callback?.(passthrough.destroyed ? null : error)
}
passthrough.once('close', () => settlePendingCallback())
const writable = new Writable({ const writable = new Writable({
write(chunk, encoding, callback) { write(chunk, encoding, callback) {
passthrough.write(chunk, encoding, callback) if (passthrough.destroyed) {
callback()
} else {
pendingCallback = callback
passthrough.write(chunk, encoding, settlePendingCallback)
}
}, },
final(callback) { final(callback) {
passthrough.end(callback) if (passthrough.destroyed) {
callback()
} else {
pendingCallback = callback
passthrough.end(settlePendingCallback)
}
}, },
}) })
......
...@@ -23,24 +23,38 @@ type DebugChannelClient = { ...@@ -23,24 +23,38 @@ type DebugChannelClient = {
*/ */
export function createWebDebugChannel(): DebugChannelPair { export function createWebDebugChannel(): DebugChannelPair {
let readableController: ReadableStreamDefaultController | undefined let readableController: ReadableStreamDefaultController | undefined
// The client side might be cancelled while React is still writing to the
// debug channel, e.g. when nobody claimed it in time (see
// `releaseParkedStream`). In that case, we drop the remaining chunks instead
// of erroring the server side, which React doesn't expect.
let isCancelled = false
const clientSideReadable = new ReadableStream<Uint8Array>({ const clientSideReadable = new ReadableStream<Uint8Array>({
start(controller) { start(controller) {
readableController = controller readableController = controller
}, },
cancel() {
isCancelled = true
},
}) })
return { return {
serverSide: { serverSide: {
writable: new WritableStream<Uint8Array>({ writable: new WritableStream<Uint8Array>({
write(chunk) { write(chunk) {
readableController?.enqueue(chunk) if (!isCancelled) {
readableController?.enqueue(chunk)
}
}, },
close() { close() {
readableController?.close() if (!isCancelled) {
readableController?.close()
}
}, },
abort(err) { abort(err) {
readableController?.error(err) if (!isCancelled) {
readableController?.error(err)
}
}, },
}), }),
}, },
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
type HmrMessageSentToBrowser, type HmrMessageSentToBrowser,
} from './hot-reloader-types' } from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops' import type { AnyStream } from '../app-render/stream-ops'
import { ParkedEntries, releaseParkedStream } from './parked-entries'
// Chunks are sent to the browser in batches to reduce overhead, flushing // Chunks are sent to the browser in batches to reduce overhead, flushing
// synchronously once this many bytes have accumulated. // synchronously once this many bytes have accumulated.
...@@ -15,10 +16,13 @@ export interface ReactDebugChannelForBrowser { ...@@ -15,10 +16,13 @@ export interface ReactDebugChannelForBrowser {
readonly readable: AnyStream readonly readable: AnyStream
} }
const reactDebugChannelsByHtmlRequestId = new Map< // Debug channels of HTML requests whose HMR client hasn't connected yet. A
string, // channel that isn't claimed in time is released, so that the debug chunks it
ReactDebugChannelForBrowser // has buffered can be garbage collected.
>() const reactDebugChannelsByHtmlRequestId =
new ParkedEntries<ReactDebugChannelForBrowser>((debugChannel) =>
releaseParkedStream(debugChannel.readable)
)
/** /**
* Reads the React debug channel and forwards its chunks to the browser through * Reads the React debug channel and forwards its chunks to the browser through
...@@ -100,14 +104,12 @@ export function connectReactDebugChannelForHtmlRequest( ...@@ -100,14 +104,12 @@ export function connectReactDebugChannelForHtmlRequest(
htmlRequestId: string, htmlRequestId: string,
sendToClient: (message: HmrMessageSentToBrowser) => void sendToClient: (message: HmrMessageSentToBrowser) => void
) { ) {
const debugChannel = reactDebugChannelsByHtmlRequestId.get(htmlRequestId) const debugChannel = reactDebugChannelsByHtmlRequestId.take(htmlRequestId)
if (!debugChannel) { if (!debugChannel) {
return return
} }
reactDebugChannelsByHtmlRequestId.delete(htmlRequestId)
connectReactDebugChannel(htmlRequestId, debugChannel, sendToClient) connectReactDebugChannel(htmlRequestId, debugChannel, sendToClient)
} }
...@@ -115,8 +117,6 @@ export function setReactDebugChannelForHtmlRequest( ...@@ -115,8 +117,6 @@ export function setReactDebugChannelForHtmlRequest(
htmlRequestId: string, htmlRequestId: string,
debugChannel: ReactDebugChannelForBrowser debugChannel: ReactDebugChannelForBrowser
) { ) {
// TODO: Clean up after a timeout, in case the client never connects, e.g.
// when CURL'ing the page, or loading the page with JavaScript disabled etc.
reactDebugChannelsByHtmlRequestId.set(htmlRequestId, debugChannel) reactDebugChannelsByHtmlRequestId.set(htmlRequestId, debugChannel)
} }
......
...@@ -138,6 +138,7 @@ import { ...@@ -138,6 +138,7 @@ import {
deleteReactDebugChannelForHtmlRequest, deleteReactDebugChannelForHtmlRequest,
setReactDebugChannelForHtmlRequest, setReactDebugChannelForHtmlRequest,
} from './debug-channel' } from './debug-channel'
import { ParkedEntries } from './parked-entries'
import { import {
getVersionInfo, getVersionInfo,
matchNextPageBundleRequest, matchNextPageBundleRequest,
...@@ -892,7 +893,8 @@ export async function createHotReloaderTurbopack( ...@@ -892,7 +893,8 @@ export async function createHotReloaderTurbopack(
Boolean(process.env.__NEXT_EXPOSE_RUNTIME_ERRORS_TO_HMR) Boolean(process.env.__NEXT_EXPOSE_RUNTIME_ERRORS_TO_HMR)
? new Map<string, RuntimeErrorStateMessage>() ? new Map<string, RuntimeErrorStateMessage>()
: null : null
const cacheStatusesByHtmlRequestId = new Map<string, ServerCacheStatus>() // Cache statuses of HTML requests whose HMR client hasn't connected yet.
const cacheStatusesByHtmlRequestId = new ParkedEntries<ServerCacheStatus>()
const clientStates = new WeakMap<ws, ClientState>() const clientStates = new WeakMap<ws, ClientState>()
function sendToClient(client: ws, message: HmrMessageSentToBrowser) { function sendToClient(client: ws, message: HmrMessageSentToBrowser) {
...@@ -1555,13 +1557,12 @@ export async function createHotReloaderTurbopack( ...@@ -1555,13 +1557,12 @@ export async function createHotReloaderTurbopack(
const enableCacheComponents = nextConfig.cacheComponents const enableCacheComponents = nextConfig.cacheComponents
if (enableCacheComponents) { if (enableCacheComponents) {
onUpgrade(client, { isLegacyClient: false }) onUpgrade(client, { isLegacyClient: false })
const cacheStatus = cacheStatusesByHtmlRequestId.get(htmlRequestId) const cacheStatus = cacheStatusesByHtmlRequestId.take(htmlRequestId)
if (cacheStatus !== undefined) { if (cacheStatus !== undefined) {
sendToClient(client, { sendToClient(client, {
type: HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR, type: HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR,
state: cacheStatus, state: cacheStatus,
}) })
cacheStatusesByHtmlRequestId.delete(htmlRequestId)
} }
} else { } else {
onUpgrade(client, { isLegacyClient: true }) onUpgrade(client, { isLegacyClient: true })
......
...@@ -103,6 +103,7 @@ import { ...@@ -103,6 +103,7 @@ import {
setReactDebugChannelForHtmlRequest, setReactDebugChannelForHtmlRequest,
type ReactDebugChannelForBrowser, type ReactDebugChannelForBrowser,
} from './debug-channel' } from './debug-channel'
import { ParkedEntries } from './parked-entries'
import { import {
getVersionInfo, getVersionInfo,
matchNextPageBundleRequest, matchNextPageBundleRequest,
...@@ -265,7 +266,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -265,7 +266,8 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
private devtoolsFrontendUrl: string | undefined private devtoolsFrontendUrl: string | undefined
private reloadAfterInvalidation: boolean = false private reloadAfterInvalidation: boolean = false
private isSrcDir: boolean private isSrcDir: boolean
private cacheStatusesByRequestId = new Map<string, ServerCacheStatus>() // Cache statuses of HTML requests whose HMR client hasn't connected yet.
private cacheStatusesByRequestId = new ParkedEntries<ServerCacheStatus>()
private hasVulnerabilityInsight: Promise<boolean> private hasVulnerabilityInsight: Promise<boolean>
public serverStats: webpack.Stats | null public serverStats: webpack.Stats | null
...@@ -668,13 +670,12 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -668,13 +670,12 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
) )
if (enableCacheComponents) { if (enableCacheComponents) {
const status = this.cacheStatusesByRequestId.get(htmlRequestId) const status = this.cacheStatusesByRequestId.take(htmlRequestId)
if (status) { if (status) {
this.sendToClient(client, { this.sendToClient(client, {
type: HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR, type: HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR,
state: status, state: status,
}) })
this.cacheStatusesByRequestId.delete(htmlRequestId)
} }
} }
} }
......
import type { AnyStream } from '../app-render/stream-ops'
/**
* How long state for an HTML request is kept for its HMR client to claim. The
* browser opens the HMR WebSocket (carrying the HTML request ID) shortly after
* the page starts hydrating, so this only needs to cover slow initial loads.
* Requests whose client never connects (e.g. when `curl`ing the page, fetching
* it from a script, or loading it with JavaScript disabled) would otherwise
* retain the parked state for the lifetime of the dev server.
*/
export const PARKED_ENTRY_TIMEOUT_MS = 30_000
type ParkedEntry<T> = {
value: T
timeout: ReturnType<typeof setTimeout> | undefined
}
/**
* A map of values that are parked for an HTML request until its HMR client
* connects and claims them. A value that isn't claimed (or deleted) within
* `timeoutMs` is evicted, and passed to `onEvict` so that it can be released.
*/
export class ParkedEntries<T> {
private readonly entries = new Map<string, ParkedEntry<T>>()
constructor(
private readonly onEvict?: (value: T) => void,
private readonly timeoutMs: number = PARKED_ENTRY_TIMEOUT_MS
) {}
get size(): number {
return this.entries.size
}
/**
* Parks a value for the given HTML request ID, replacing (without releasing)
* any value that was previously parked for it.
*/
set(htmlRequestId: string, value: T): void {
this.delete(htmlRequestId)
const entry: ParkedEntry<T> = { value, timeout: undefined }
entry.timeout = setTimeout(() => {
// The entry might have been replaced in the meantime. Its timer would
// have been cleared then, but check anyway, so that we never evict a
// newer entry.
if (this.entries.get(htmlRequestId) !== entry) {
return
}
this.entries.delete(htmlRequestId)
try {
this.onEvict?.(value)
} catch {
// Releasing an evicted value is best-effort.
}
}, this.timeoutMs)
// Don't keep the process alive just to evict parked values.
entry.timeout.unref?.()
this.entries.set(htmlRequestId, entry)
}
/**
* Removes and returns the value parked for the given HTML request ID, if any.
*/
take(htmlRequestId: string): T | undefined {
const entry = this.entries.get(htmlRequestId)
if (!entry) {
return undefined
}
clearTimeout(entry.timeout)
this.entries.delete(htmlRequestId)
return entry.value
}
/**
* Removes the value parked for the given HTML request ID, if any, without
* releasing it.
*/
delete(htmlRequestId: string): void {
this.take(htmlRequestId)
}
}
/**
* Releases a parked stream that no client is going to read, so that the chunks
* it has buffered can be garbage collected. This only affects the parked
* stream, and not any other consumers of the same source.
*/
export function releaseParkedStream(stream: AnyStream): void {
if (stream instanceof ReadableStream) {
stream.cancel().catch(() => {
// Cancelling is best-effort.
})
} else {
stream.destroy()
}
}
...@@ -4,8 +4,13 @@ import { ...@@ -4,8 +4,13 @@ import {
} from './hot-reloader-types' } from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops' import type { AnyStream } from '../app-render/stream-ops'
import { streamToUint8Array } from '../stream-utils/node-web-streams-helper' import { streamToUint8Array } from '../stream-utils/node-web-streams-helper'
import { ParkedEntries, releaseParkedStream } from './parked-entries'
const errorsRscStreamsByHtmlRequestId = new Map<string, AnyStream>() // Errors of HTML requests whose HMR client hasn't connected yet. A stream that
// isn't claimed in time is released.
const errorsRscStreamsByHtmlRequestId = new ParkedEntries<AnyStream>(
releaseParkedStream
)
export function sendSerializedErrorsToClient( export function sendSerializedErrorsToClient(
errorsRscStream: AnyStream, errorsRscStream: AnyStream,
...@@ -28,14 +33,12 @@ export function sendSerializedErrorsToClientForHtmlRequest( ...@@ -28,14 +33,12 @@ export function sendSerializedErrorsToClientForHtmlRequest(
htmlRequestId: string, htmlRequestId: string,
sendToClient: (message: HmrMessageSentToBrowser) => void sendToClient: (message: HmrMessageSentToBrowser) => void
) { ) {
const errorsRscStream = errorsRscStreamsByHtmlRequestId.get(htmlRequestId) const errorsRscStream = errorsRscStreamsByHtmlRequestId.take(htmlRequestId)
if (!errorsRscStream) { if (!errorsRscStream) {
return return
} }
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
sendSerializedErrorsToClient(errorsRscStream, sendToClient) sendSerializedErrorsToClient(errorsRscStream, sendToClient)
} }
...@@ -43,8 +46,6 @@ export function setErrorsRscStreamForHtmlRequest( ...@@ -43,8 +46,6 @@ export function setErrorsRscStreamForHtmlRequest(
htmlRequestId: string, htmlRequestId: string,
errorsRscStream: AnyStream errorsRscStream: AnyStream
) { ) {
// TODO: Clean up after a timeout, in case the client never connects, e.g.
// when CURL'ing the page, or loading the page with JavaScript disabled etc.
errorsRscStreamsByHtmlRequestId.set(htmlRequestId, errorsRscStream) errorsRscStreamsByHtmlRequestId.set(htmlRequestId, errorsRscStream)
} }
......
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