Unverified Commit 24219b19 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

Clean up dev errors RSC streams the HMR client never consumes

Every HTML render in dev stores its errors RSC stream keyed by
htmlRequestId so the stream can be sent once the HMR client connects. The
entry was only deleted when a client actually consumed it, so any render
whose client never connected (curl, disabled JavaScript, bots, or simply
closing the tab before the websocket upgrade) left the stream - and the
RSC payload buffered in it - alive for the lifetime of the dev server.
The module already carried a TODO for exactly this cleanup.

Adds a 60s cleanup timer per stored entry (unref'ed backstop), cancelled
when the entry is consumed or deleted.

Unit tests cover consumption before the timeout, abandonment cleanup,
explicit deletion, and timer replacement. Without the fix the
abandonment test fails: the stream is retained forever.
Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent 865d623a
import {
deleteErrorsRscStreamForHtmlRequest,
sendSerializedErrorsToClientForHtmlRequest,
setErrorsRscStreamForHtmlRequest,
} from './serialized-errors'
import type { AnyStream } from '../app-render/stream-ops'
jest.useFakeTimers()
function makeStream(): AnyStream {
return new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode('errors-payload'))
controller.close()
},
}) as unknown as AnyStream
}
describe('serialized-errors', () => {
it('sends a stored errors stream when the client connects', async () => {
const sendToClient = jest.fn()
setErrorsRscStreamForHtmlRequest('req-consume', makeStream())
sendSerializedErrorsToClientForHtmlRequest('req-consume', sendToClient)
await jest.advanceTimersByTimeAsync(0)
expect(sendToClient).toHaveBeenCalledTimes(1)
expect(sendToClient.mock.calls[0][0].serializedErrors).toBeDefined()
})
it('cleans up an errors stream the client never consumes', async () => {
const sendToClient = jest.fn()
setErrorsRscStreamForHtmlRequest('req-abandoned', makeStream())
// The client never connects. After the cleanup window the entry is gone.
await jest.advanceTimersByTimeAsync(10 * 60_000 + 1_000)
sendSerializedErrorsToClientForHtmlRequest('req-abandoned', sendToClient)
await jest.advanceTimersByTimeAsync(0)
expect(sendToClient).not.toHaveBeenCalled()
})
it('deleteErrorsRscStreamForHtmlRequest drops the entry and its cleanup', async () => {
const sendToClient = jest.fn()
setErrorsRscStreamForHtmlRequest('req-deleted', makeStream())
deleteErrorsRscStreamForHtmlRequest('req-deleted')
await jest.advanceTimersByTimeAsync(10 * 60_000 + 1_000)
sendSerializedErrorsToClientForHtmlRequest('req-deleted', sendToClient)
await jest.advanceTimersByTimeAsync(0)
expect(sendToClient).not.toHaveBeenCalled()
})
it('consuming before the timeout keeps the entry', async () => {
const sendToClient = jest.fn()
setErrorsRscStreamForHtmlRequest('req-early', makeStream())
await jest.advanceTimersByTimeAsync(30_000)
sendSerializedErrorsToClientForHtmlRequest('req-early', sendToClient)
await jest.advanceTimersByTimeAsync(0)
expect(sendToClient).toHaveBeenCalledTimes(1)
})
})
......@@ -6,6 +6,26 @@ import type { AnyStream } from '../app-render/stream-ops'
import { streamToUint8Array } from '../stream-utils/node-web-streams-helper'
const errorsRscStreamsByHtmlRequestId = new Map<string, AnyStream>()
const cleanupTimersByHtmlRequestId = new Map<string, NodeJS.Timeout>()
/**
* How long an unconsumed errors stream is retained for a client that connects
* later. The window needs to comfortably cover legitimate HMR startups —
* a large cold dev build, a throttled network, or a paused debugger can take
* minutes before the client opens the socket — while still bounding how long
* a stream whose client never connects (curl, disabled JavaScript, bots) is
* retained: without any bound, the entry and the RSC payload buffered in it
* stay alive for the lifetime of the dev server.
*/
const CLEANUP_AFTER_MS = 10 * 60_000
function clearCleanupTimer(htmlRequestId: string) {
const timer = cleanupTimersByHtmlRequestId.get(htmlRequestId)
if (timer) {
clearTimeout(timer)
cleanupTimersByHtmlRequestId.delete(htmlRequestId)
}
}
export function sendSerializedErrorsToClient(
errorsRscStream: AnyStream,
......@@ -34,6 +54,7 @@ export function sendSerializedErrorsToClientForHtmlRequest(
return
}
clearCleanupTimer(htmlRequestId)
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
sendSerializedErrorsToClient(errorsRscStream, sendToClient)
......@@ -43,11 +64,21 @@ export function setErrorsRscStreamForHtmlRequest(
htmlRequestId: string,
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)
// 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.
clearCleanupTimer(htmlRequestId)
const timer = setTimeout(() => {
cleanupTimersByHtmlRequestId.delete(htmlRequestId)
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
}, CLEANUP_AFTER_MS)
// The timer is only a backstop and must not keep the process alive.
timer.unref?.()
cleanupTimersByHtmlRequestId.set(htmlRequestId, timer)
}
export function deleteErrorsRscStreamForHtmlRequest(htmlRequestId: string) {
clearCleanupTimer(htmlRequestId)
errorsRscStreamsByHtmlRequestId.delete(htmlRequestId)
}
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