Skip to content

Revert "Fix stream cancellation in RenderResult.pipe() and sendResponse()" #52277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,11 @@ async function render(request: NextRequest, event: NextFetchEvent) {
response.headers.append('Vary', RSC_VARY_HEADER)

const writer = tranform.writable.getWriter()
const target = {
result.pipe({
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: 13 additions & 8 deletions packages/next/src/server/render-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface PipeTarget {
end: () => unknown
flush?: () => unknown
destroy: (err?: Error) => unknown
get closed(): boolean
}

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

let shouldFatalError = false
try {
while (true) {
const result = await reader.read()

if (res.closed || result.done) {
break
}
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 (!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 (!res.closed) {
// If we've written to the response, we should destroy it.
if (shouldFatalError) {
res.destroy(err as any)
}

Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/send-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ 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,9 +25,8 @@ export const streamToBufferedResult = async (
},
end() {},
destroy() {},
closed: false,
}
await renderResult.pipe(writable)
await renderResult.pipe(writable as any)
return renderChunks.join('')
}

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

if (options.result.isDynamic) {
const writer = res.transformStream.writable.getWriter()
const target = {
options.result.pipe({
write: (chunk: Uint8Array) => writer.write(chunk),
end: () => writer.close(),
destroy: (err: Error) => writer.abort(err),
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)
cork: () => {},
uncork: () => {},
// Not implemented: on/removeListener
} as any)
} else {
const payload = await options.result.toUnchunkedString()
res.setHeader('Content-Length', String(byteLength(payload)))
Expand Down
26 changes: 0 additions & 26 deletions test/e2e/cancel-request/app/edge-route/route.ts

This file was deleted.

28 changes: 0 additions & 28 deletions test/e2e/cancel-request/app/node-route/route.ts

This file was deleted.

28 changes: 0 additions & 28 deletions test/e2e/cancel-request/middleware.ts

This file was deleted.

28 changes: 0 additions & 28 deletions test/e2e/cancel-request/pages/api/edge-api.ts

This file was deleted.

36 changes: 0 additions & 36 deletions test/e2e/cancel-request/pages/api/node-api.ts

This file was deleted.

22 changes: 0 additions & 22 deletions test/e2e/cancel-request/readable.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/e2e/cancel-request/sleep.ts

This file was deleted.

Loading