Repository navigation
fix(@angular/ssr): harden platform destruction and abort/response cleanup guards - #34781
alan-agius4 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves SSR request handling and lifecycle management by ensuring that SSR requests are aborted on client disconnect or timeout, response bodies are cancelled when the destination is closed, and platform/injector destruction is handled gracefully. The review feedback highlights two important robustness improvements: providing a fallback for AbortSignal.any to support Node.js versions prior to v20.3.0, and using optional chaining when accessing destination.req.url to prevent potential TypeErrors.
| const nodeWebReq = createWebRequestFromNodeRequest(req); | ||
| const webReq = new Request(nodeWebReq, { | ||
| signal: AbortSignal.any([nodeWebReq.signal, AbortSignal.timeout(30_000)]), | ||
| }); |
There was a problem hiding this comment.
Using AbortSignal.any directly will throw a TypeError in Node.js versions prior to v20.3.0 (such as Node.js v18.19.1+, which is officially supported by Angular 19). To prevent runtime crashes in these environments, please provide a fallback implementation when AbortSignal.any is not available.
const nodeWebReq = createWebRequestFromNodeRequest(req);
const timeoutSignal = AbortSignal.timeout(30_000);
const signal = typeof AbortSignal.any === 'function'
? AbortSignal.any([nodeWebReq.signal, timeoutSignal])
: (() => {
const controller = new AbortController();
const abort = () => controller.abort();
if (nodeWebReq.signal.aborted || timeoutSignal.aborted) {
controller.abort();
} else {
nodeWebReq.signal.addEventListener('abort', abort, { once: true });
timeoutSignal.addEventListener('abort', abort, { once: true });
}
return controller.signal;
})();
const webReq = new Request(nodeWebReq, { signal });| if (source.body && !source.body.locked) { | ||
| await source.body.cancel().catch((err) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error( | ||
| `An error occurred while cancelling the response body for: ${destination.req.url}.`, | ||
| err, | ||
| ); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Accessing destination.req.url directly can throw a TypeError if destination.req is undefined (e.g., in mock environments or if the request/response has been fully torn down). Using optional chaining prevents potential crashes in this error-handling path.
| if (source.body && !source.body.locked) { | |
| await source.body.cancel().catch((err) => { | |
| // eslint-disable-next-line no-console | |
| console.error( | |
| `An error occurred while cancelling the response body for: ${destination.req.url}.`, | |
| err, | |
| ); | |
| }); | |
| } | |
| if (source.body && !source.body.locked) { | |
| await source.body.cancel().catch((err) => { | |
| // eslint-disable-next-line no-console | |
| console.error( | |
| `An error occurred while cancelling the response body for: ${destination.req?.url ?? 'unknown'}.`, | |
| err, | |
| ); | |
| }); | |
| } |
…uards - Guard `platformRef.destroy()` in `getRoutesFromAngularRouterConfig` against already-destroyed platforms and catch/log destroy-hook errors so route extraction neither fails nor masks earlier errors. - Check `applicationRef.injector.destroyed` and `destroyPromise` consistently across `renderAngular` bootstrap, stability, and `content()` guards. - Cancel unlocked `source.body` streams when `writeResponseToNodeResponse` returns early for an already-closed Node response.
Combine the client-disconnect `AbortSignal` from `createWebRequestFromNodeRequest` with `AbortSignal.timeout(30_000)` using `AbortSignal.any` instead of replacing the client signal in `createAngularSsrInternalMiddleware`.
413cdfa to
930d82e
Compare
PR Checklist
PR Type
What is the current behavior?
Follow-up hardening after #34768 (related to #34754 / #34777):
getRoutesFromAngularRouterConfig()callsplatformRef.destroy()directly infinallywithout checking!platformRef.destroyedor catching destroy-hook errors, which can throwNG0404, fail route extraction, or mask errors thrown insidetry.renderAngular()checksplatformRef.destroyedandapplicationRef.destroyed, but does not checkapplicationRef.injector.destroyed(ordestroyPromisebeforecontent()), which can still throwNG0205if the environment injector is marked destroyed beforeApplicationRef.ngOnDestroy()completes.writeResponseToNodeResponse()returns early when the Node destination is already closed/destroyed without cancellingsource.body.createAngularSsrInternalMiddleware()wrapscreateWebRequestFromNodeRequest(req)with{ signal: AbortSignal.timeout(30_000) }, replacing the client-disconnectAbortSignalinstead of combining both signals.What is the new behavior?
platformRef.destroy()ingetRoutesFromAngularRouterConfig()withif (!platformRef.destroyed)and catch/log destroy-hook errors viaconsole.error.destroyPromise,platformRef.destroyed,applicationRef.destroyed,applicationRef.injector.destroyed, andsignal?.abortedconsistently acrossrenderAngular()andcontent()(and replace silent early-return guards inng_spec.tswithnode:assert).source.bodystreams (with error logging) whenwriteResponseToNodeResponse()returns early on an already-closed Node response.nodeWebReq.signalandAbortSignal.timeout(30_000)usingAbortSignal.any()increateAngularSsrInternalMiddleware().Does this PR introduce a breaking change?