Unverified Commit b714ac55 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

Support response aborting (#51594)

### What?

This is an alternative to #51330, which only support aborting the response (doesn't support back-pressure). If the client cancels the request (HMR update, navigates to another page, etc), we'll be able to detect that and stop pulling data from the dev's `ReadableStream` response.

### Why?

We want to allow API routes to stream data coming from another server (eg, AI services). The responses from these other servers can be long running and expensive. In the case the browser aborts the connection, it's critical that we stop streaming data as soon as possible.

### How?

By checking whether `response.closed` is set during the `for await (…)` iteration, we're able to detect that the client has aborted the connection. Cleanup of the `ReadableStream` is handled implicitly by the async iterator when the loop ends.

The one catch is our use of http-proxy for worker processes. It does not properly detect a client disconnecting (but does handle back-pressure). In order to fix that, I've manually added event listeners to detect the disconnect and cancel the proxied req/res pair.

Re: [WEB-1185](https://linear.app/vercel/issue/WEB-1185) (we still need back-pressure)
Fixes https://github.com/vercel/next.js/issues/50364
Fixes https://github.com/vercel-labs/ai/issues/90
parent f59710e9
...@@ -100,6 +100,20 @@ export const createServerHandler = async ({ ...@@ -100,6 +100,20 @@ export const createServerHandler = async ({
return return
} }
const proxyServer = getProxyServer(req.url || '/') const proxyServer = getProxyServer(req.url || '/')
// http-proxy does not properly detect a client disconnect in newer
// versions of Node.js. This is caused because it only listens for the
// `aborted` event on the our request object, but it also fully reads and
// closes the request object. Node **will not** fire `aborted` when the
// request is already closed. Listening for `close` on our response object
// will detect the disconnect, and we can abort the proxy's connection.
proxyServer.on('proxyReq', (proxyReq) => {
res.on('close', () => proxyReq.destroy())
})
proxyServer.on('proxyRes', (proxyRes) => {
res.on('close', () => proxyRes.destroy())
})
proxyServer.web(req, res) proxyServer.web(req, res)
proxyServer.on('error', (err) => { proxyServer.on('error', (err) => {
res.statusCode = 500 res.statusCode = 500
......
...@@ -284,6 +284,21 @@ export async function startServer({ ...@@ -284,6 +284,21 @@ export async function startServer({
return return
} }
const proxyServer = getProxyServer(req.url || '/') const proxyServer = getProxyServer(req.url || '/')
// http-proxy does not properly detect a client disconnect in newer
// versions of Node.js. This is caused because it only listens for the
// `aborted` event on the our request object, but it also fully reads
// and closes the request object. Node **will not** fire `aborted` when
// the request is already closed. Listening for `close` on our response
// object will detect the disconnect, and we can abort the proxy's
// connection.
proxyServer.on('proxyReq', (proxyReq) => {
res.on('close', () => proxyReq.destroy())
})
proxyServer.on('proxyRes', (proxyRes) => {
res.on('close', () => proxyRes.destroy())
})
proxyServer.web(req, res) proxyServer.web(req, res)
} }
upgradeHandler = async (req, socket, head) => { upgradeHandler = async (req, socket, head) => {
......
...@@ -993,13 +993,12 @@ export default class NextNodeServer extends BaseServer { ...@@ -993,13 +993,12 @@ export default class NextNodeServer extends BaseServer {
) )
} }
protected streamResponseChunk(res: NodeNextResponse, chunk: any) { private streamResponseChunk(res: ServerResponse, chunk: any) {
res.originalResponse.write(chunk) res.write(chunk)
// When both compression and streaming are enabled, we need to explicitly // When both compression and streaming are enabled, we need to explicitly
// flush the response to avoid it being buffered by gzip. // flush the response to avoid it being buffered by gzip.
if (this.compression && 'flush' in res.originalResponse) { if (this.compression && 'flush' in res) {
;(res.originalResponse as any).flush() ;(res as any).flush()
} }
} }
...@@ -1541,10 +1540,12 @@ export default class NextNodeServer extends BaseServer { ...@@ -1541,10 +1540,12 @@ export default class NextNodeServer extends BaseServer {
res.statusCode = invokeRes.statusCode res.statusCode = invokeRes.statusCode
res.statusMessage = invokeRes.statusMessage res.statusMessage = invokeRes.statusMessage
const { originalResponse } = res as NodeNextResponse
for await (const chunk of invokeRes) { for await (const chunk of invokeRes) {
this.streamResponseChunk(res as NodeNextResponse, chunk) if (originalResponse.closed) break
this.streamResponseChunk(originalResponse, chunk)
} }
;(res as NodeNextResponse).originalResponse.end() res.send()
return { return {
finished: true, finished: true,
} }
...@@ -2521,9 +2522,12 @@ export default class NextNodeServer extends BaseServer { ...@@ -2521,9 +2522,12 @@ export default class NextNodeServer extends BaseServer {
} }
} }
res.statusCode = result.response.status res.statusCode = result.response.status
const { originalResponse } = res as NodeNextResponse
for await (const chunk of result.response.body || for await (const chunk of result.response.body ||
([] as any)) { ([] as any)) {
this.streamResponseChunk(res as NodeNextResponse, chunk) if (originalResponse.closed) break
this.streamResponseChunk(originalResponse, chunk)
} }
res.send() res.send()
return { return {
...@@ -2694,17 +2698,14 @@ export default class NextNodeServer extends BaseServer { ...@@ -2694,17 +2698,14 @@ export default class NextNodeServer extends BaseServer {
if (result.response.headers.has('x-middleware-refresh')) { if (result.response.headers.has('x-middleware-refresh')) {
res.statusCode = result.response.status res.statusCode = result.response.status
if ((result.response as any).invokeRes) { const { originalResponse } = res as NodeNextResponse
for await (const chunk of (result.response as any).invokeRes) { const body =
this.streamResponseChunk(res as NodeNextResponse, chunk) (result.response as any).invokeRes || result.response.body || []
} for await (const chunk of body) {
;(res as NodeNextResponse).originalResponse.end() if (originalResponse.closed) break
} else { this.streamResponseChunk(originalResponse, chunk)
for await (const chunk of result.response.body || ([] as any)) {
this.streamResponseChunk(res as NodeNextResponse, chunk)
}
res.send()
} }
res.send()
return { return {
finished: true, finished: true,
} }
...@@ -2884,22 +2885,23 @@ export default class NextNodeServer extends BaseServer { ...@@ -2884,22 +2885,23 @@ export default class NextNodeServer extends BaseServer {
} }
}) })
const nodeResStream = (params.res as NodeNextResponse).originalResponse
if (result.response.body) { if (result.response.body) {
// TODO(gal): not sure that we always need to stream // TODO(gal): not sure that we always need to stream
const nodeResStream = (params.res as NodeNextResponse).originalResponse
const { consumeUint8ArrayReadableStream } = const { consumeUint8ArrayReadableStream } =
require('next/dist/compiled/edge-runtime') as typeof import('next/dist/compiled/edge-runtime') require('next/dist/compiled/edge-runtime') as typeof import('next/dist/compiled/edge-runtime')
try { try {
for await (const chunk of consumeUint8ArrayReadableStream( for await (const chunk of consumeUint8ArrayReadableStream(
result.response.body result.response.body
)) { )) {
if (nodeResStream.closed) break
nodeResStream.write(chunk) nodeResStream.write(chunk)
} }
} finally { } finally {
nodeResStream.end() nodeResStream.end()
} }
} else { } else {
;(params.res as NodeNextResponse).originalResponse.end() nodeResStream.end()
} }
return result return result
......
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