-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/ssr): settle writeResponseToNodeResponse when client disconnects #33721
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,24 @@ | |
| import type { ServerResponse } from 'node:http'; | ||
| import type { Http2ServerResponse } from 'node:http2'; | ||
|
|
||
| /** | ||
| * Checks whether a Node.js `ServerResponse` or `Http2ServerResponse` is destroyed, closed, or ended. | ||
| * | ||
| * @param destination - The HTTP/1.1 or HTTP/2 server response to check. | ||
| * @returns `true` if the response or its underlying stream is destroyed, closed, or ended; otherwise `false`. | ||
| */ | ||
| function isResponseDestroyedOrClosed(destination: ServerResponse | Http2ServerResponse): boolean { | ||
| return ( | ||
| Boolean(destination.destroyed) || | ||
| Boolean(destination.closed) || | ||
| Boolean(destination.writableEnded) || | ||
| ('stream' in destination && | ||
| (!destination.stream || | ||
| Boolean(destination.stream.destroyed) || | ||
| Boolean(destination.stream.closed))) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Streams a web-standard `Response` into a Node.js `ServerResponse` | ||
| * or `Http2ServerResponse`. | ||
|
|
@@ -24,6 +42,10 @@ export async function writeResponseToNodeResponse( | |
| source: Response, | ||
| destination: ServerResponse | Http2ServerResponse, | ||
| ): Promise<void> { | ||
| if (isResponseDestroyedOrClosed(destination)) { | ||
| return; | ||
| } | ||
|
|
||
| const { status, headers, body } = source; | ||
| destination.statusCode = status; | ||
|
|
||
|
|
@@ -48,27 +70,52 @@ export async function writeResponseToNodeResponse( | |
| } | ||
|
|
||
| if (!body) { | ||
| destination.end(); | ||
| if (!isResponseDestroyedOrClosed(destination)) { | ||
| destination.end(); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const reader = body.getReader(); | ||
|
|
||
| destination.on('close', () => { | ||
| reader.cancel().catch((error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error( | ||
| `An error occurred while writing the response body for: ${destination.req.url}.`, | ||
| error, | ||
| ); | ||
| }); | ||
| let isClosed = isResponseDestroyedOrClosed(destination); | ||
| const isDestroyedOrClosed = () => isClosed || isResponseDestroyedOrClosed(destination); | ||
|
|
||
| let readerCancelled = false; | ||
| const reader = body.getReader(); | ||
| const cancelReader = (error?: unknown) => { | ||
| if (readerCancelled) { | ||
| return; | ||
| } | ||
| readerCancelled = true; | ||
| isClosed = true; | ||
| destination.off('close', cancelReader); | ||
| destination.off('error', cancelReader); | ||
| reader.cancel(error).catch((err) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error( | ||
| `An error occurred while writing the response body for: ${destination.req.url}.`, | ||
| err, | ||
| ); | ||
| }); | ||
| }; | ||
|
|
||
| destination.once('close', cancelReader); | ||
| destination.once('error', cancelReader); | ||
|
Comment on lines
+102
to
+103
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Is there a risk of leaking memory if ex.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! In Node.js To make cleanup immediate and deterministic (matching the pattern used below in the backpressure Promise), I've updated |
||
|
|
||
| try { | ||
| // eslint-disable-next-line no-constant-condition | ||
| while (true) { | ||
| if (isDestroyedOrClosed()) { | ||
| cancelReader(); | ||
| break; | ||
| } | ||
|
|
||
| const { done, value } = await reader.read(); | ||
| if (isDestroyedOrClosed()) { | ||
| cancelReader(); | ||
| break; | ||
| } | ||
|
|
||
| if (done) { | ||
| destination.end(); | ||
| break; | ||
|
|
@@ -78,10 +125,39 @@ export async function writeResponseToNodeResponse( | |
| if (canContinue === false) { | ||
| // Explicitly check for `false`, as AWS may return `undefined` even though this is not valid. | ||
| // See: https://github.com/CodeGenieApp/serverless-express/issues/683 | ||
| await new Promise<void>((resolve) => destination.once('drain', resolve)); | ||
| await new Promise<void>((resolve) => { | ||
| if (isDestroyedOrClosed()) { | ||
| resolve(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const onDrain = () => { | ||
| destination.off('close', onClose); | ||
| destination.off('error', onClose); | ||
| resolve(); | ||
| }; | ||
|
|
||
| const onClose = () => { | ||
| destination.off('drain', onDrain); | ||
| destination.off('close', onClose); | ||
| destination.off('error', onClose); | ||
| cancelReader(); | ||
| resolve(); | ||
| }; | ||
|
alan-agius4 marked this conversation as resolved.
|
||
|
|
||
| destination.once('drain', onDrain); | ||
| destination.once('close', onClose); | ||
| destination.once('error', onClose); | ||
| }); | ||
| } | ||
| } | ||
| } catch { | ||
| destination.end('Internal server error.'); | ||
| if (!isDestroyedOrClosed()) { | ||
| destination.end('Internal server error.'); | ||
| } | ||
| } finally { | ||
| destination.off('close', cancelReader); | ||
| destination.off('error', cancelReader); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.