Fix/streamed response termination 2#3533
Draft
pieh wants to merge 4 commits into
Draft
Conversation
A streamed SSR response commits its headers (typically a 200, with no Content-Length) as soon as Next.js flushes them, well before the body finishes. If the render then fails *after* that point, response finalization had no error path: - a render that aborts the response mid-stream leaves the body hung open forever (the @fastly/http-compute-js source stream never closes), and - a render that throws hits the catch which sets statusCode=500 and writes "Internal Server Error" - but the status change is a no-op once headers are sent, so the client receives a 200 with the error string concatenated onto partial HTML (a garbled but "complete" response). Both reach the edge as an unparseable HTTP message, which ATS reports as a 502 (ats_status_502_invalid_http_response). Exposure scales with how long the response stream stays open, i.e. with pages that do a lot of async work during render. Fix: - When the handler fails after headers are committed, end the response cleanly (so buffered bytes flush in order) and record the error instead of appending "Internal Server Error". - Replace the body TransformStream (flush-only, no error path) with a ReadableStream that surfaces a recorded render error to the platform and, if the response was destroyed without a clean end, errors promptly instead of hanging. The happy path is unchanged (full body, then background work, then close). Adds regression tests that mirror the finalization pipeline. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
📊 Package size report 0.05%↑
Unchanged files
🤖 This report was automatically generated by pkg-size-action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Some tidying up of #3532 (opening as separate PR so it's not from fork and let tests fully run)
Documentation
Tests
You can test this change yourself like so:
Relevant links (GitHub issues, etc.) or a picture of cute animal