Skip to content

Commit

Permalink
Revert "Revert "Fix stream cancellation in RenderResult.pipe() and …
Browse files Browse the repository at this point in the history
…`sendResponse()`" (#52277)"

This reverts commit 5046ee1.
  • Loading branch information
jridgewell committed Jul 6, 2023
1 parent cee0599 commit 67b96e2
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,20 @@ async function render(request: NextRequest, event: NextFetchEvent) {
response.headers.append('Vary', RSC_VARY_HEADER)

const writer = tranform.writable.getWriter()
result.pipe({
const target = {
write: (chunk: Uint8Array) => writer.write(chunk),
end: () => writer.close(),
destroy: (reason?: Error) => writer.abort(reason),
})
closed: false,
}
const onClose = () => {
target.closed = true
}
// No, this cannot be replaced with `finally`, because early cancelling
// the stream will create a rejected promise, and finally will create an
// unhandled rejection.
writer.closed.then(onClose, onClose)
result.pipe(target)

return response
}
Expand Down
21 changes: 8 additions & 13 deletions packages/next/src/server/render-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface PipeTarget {
end: () => unknown
flush?: () => unknown
destroy: (err?: Error) => unknown
get closed(): boolean
}

export default class RenderResult {
Expand Down Expand Up @@ -111,31 +112,25 @@ export default class RenderResult {
: () => {}
const reader = this.response.getReader()

let shouldFatalError = false
try {
let result = await reader.read()
if (!result.done) {
// As we're going to write to the response, we should destroy the
// response if an error occurs.
shouldFatalError = true
}
while (true) {
const result = await reader.read()

if (res.closed || result.done) {
break
}

while (!result.done) {
// Write the data to the response.
res.write(result.value)

// Flush it to the client (if it supports flushing).
flush()

// Read the next chunk.
result = await reader.read()
}

// We're done writing to the response, so we can end it.
res.end()
} catch (err) {
// If we've written to the response, we should destroy it.
if (shouldFatalError) {
if (!res.closed) {
res.destroy(err as any)
}

Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/send-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function sendResponse(
const iterator = consumeUint8ArrayReadableStream(response.body)
try {
for await (const chunk of iterator) {
if (originalResponse.closed) break
originalResponse.write(chunk)
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export const streamToBufferedResult = async (
},
end() {},
destroy() {},
closed: false,
}
await renderResult.pipe(writable as any)
await renderResult.pipe(writable)
return renderChunks.join('')
}

Expand Down
16 changes: 11 additions & 5 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,20 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {

if (options.result.isDynamic) {
const writer = res.transformStream.writable.getWriter()
options.result.pipe({
const target = {
write: (chunk: Uint8Array) => writer.write(chunk),
end: () => writer.close(),
destroy: (err: Error) => writer.abort(err),
cork: () => {},
uncork: () => {},
// Not implemented: on/removeListener
} as any)
closed: false,
} as any
const onClose = () => {
target.closed = true
}
// No, this cannot be replaced with `finally`, because early cancelling
// the stream will create a rejected promise, and finally will create an
// unhandled rejection.
writer.closed.then(onClose, onClose)
options.result.pipe(target)
} else {
const payload = await options.result.toUnchunkedString()
res.setHeader('Content-Length', String(byteLength(payload)))
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/cancel-request/app/edge-route/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Streamable } from '../../streamable'

export const runtime = 'edge'

let streamable
let requestAborted = false

export function GET(req: Request): Response {
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
if (streamable) {
return new Response(
JSON.stringify({
requestAborted,
i: streamable.i,
streamCleanedUp: streamable.streamCleanedUp,
})
)
}

streamable = Streamable()
req.signal.onabort = () => {
requestAborted = true
}
return new Response(streamable.stream)
}
28 changes: 28 additions & 0 deletions test/e2e/cancel-request/app/node-route/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Streamable } from '../../streamable'

export const runtime = 'nodejs'
// Next thinks it can statically compile this route, which breaks the test.
export const dynamic = 'force-dynamic'

let streamable
let requestAborted = false

export function GET(req: Request): Response {
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
if (streamable) {
return new Response(
JSON.stringify({
requestAborted,
i: streamable.i,
streamCleanedUp: streamable.streamCleanedUp,
})
)
}

streamable = Streamable()
req.signal.onabort = () => {
requestAborted = true
}
return new Response(streamable.stream)
}
28 changes: 28 additions & 0 deletions test/e2e/cancel-request/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Streamable } from './streamable'

export const config = {
matcher: '/middleware',
}

let streamable
let requestAborted = false

export default function handler(req: Request): Response {
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
if (streamable) {
return new Response(
JSON.stringify({
requestAborted,
i: streamable.i,
streamCleanedUp: streamable.streamCleanedUp,
})
)
}

streamable = Streamable()
req.signal.onabort = () => {
requestAborted = true
}
return new Response(streamable.stream)
}
28 changes: 28 additions & 0 deletions test/e2e/cancel-request/pages/api/edge-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Streamable } from '../../streamable'

export const config = {
runtime: 'edge',
}

let streamable
let requestAborted = false

export default function handler(req: Request): Response {
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
if (streamable) {
return new Response(
JSON.stringify({
requestAborted,
i: streamable.i,
streamCleanedUp: streamable.streamCleanedUp,
})
)
}

streamable = Streamable()
req.signal.onabort = () => {
requestAborted = true
}
return new Response(streamable.stream)
}
36 changes: 36 additions & 0 deletions test/e2e/cancel-request/pages/api/node-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IncomingMessage, ServerResponse } from 'http'
import { pipeline } from 'stream'
import { Readable } from '../../readable'

export const config = {
runtime: 'nodejs',
}

let readable
let requestAborted = false

export default function handler(
_req: IncomingMessage,
res: ServerResponse
): void {
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
if (readable) {
res.end(
JSON.stringify({
requestAborted,
i: readable.i,
streamCleanedUp: readable.streamCleanedUp,
})
)
return
}

readable = Readable()
res.on('close', () => {
requestAborted = true
})
pipeline(readable.stream, res, () => {
res.end()
})
}
22 changes: 22 additions & 0 deletions test/e2e/cancel-request/readable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as stream from 'stream'
import { sleep } from './sleep'

export function Readable() {
const encoder = new TextEncoder()
const readable = {
i: 0,
streamCleanedUp: false,
stream: new stream.Readable({
async read() {
await sleep(100)
this.push(encoder.encode(String(readable.i++)))

if (readable.i >= 25) this.push(null)
},
destroy() {
readable.streamCleanedUp = true
},
}),
}
return readable
}
3 changes: 3 additions & 0 deletions test/e2e/cancel-request/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sleep(ms: number) {
return new Promise((res) => setTimeout(res, ms))
}
Loading

0 comments on commit 67b96e2

Please sign in to comment.