Skip to content
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

fix: clone response in first handler to prevent race (#70082) #70649

Merged
merged 5 commits into from
Oct 1, 2024

Conversation

ijjk
Copy link
Member

@ijjk ijjk commented Sep 30, 2024

This fixes a race where if the body was resolved before the clone operation, it would clone later, resulting in an error being thrown due to the body already being consumed.

backports #70082

This fixes a race where if the body was resolved before the clone
operation, it would clone later, resulting in an error being thrown due
to the body already being consumed.
# Conflicts:
#	packages/next/src/server/lib/patch-fetch.ts
@ijjk ijjk added created-by: Next.js team PRs by the Next.js team. type: next labels Sep 30, 2024
@ijjk ijjk requested a review from ztanner September 30, 2024 22:24
@ijjk
Copy link
Member Author

ijjk commented Sep 30, 2024

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
buildDuration 20.3s 19s N/A
buildDurationCached 10s 9s N/A
nodeModulesSize 200 MB 200 MB ⚠️ +2.97 kB
nextStartRea..uration (ms) 491ms 484ms N/A
Client Bundles (main, webpack)
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
1a9f679d-HASH.js gzip 53.7 kB 53.7 kB N/A
5428.HASH.js gzip 181 B 180 B N/A
6067-HASH.js gzip 5.05 kB 5.05 kB N/A
6428-HASH.js gzip 31.6 kB 31.6 kB N/A
framework-HASH.js gzip 44.9 kB 44.9 kB
main-app-HASH.js gzip 242 B 242 B
main-HASH.js gzip 32.3 kB 32.3 kB N/A
webpack-HASH.js gzip 1.68 kB 1.68 kB N/A
Overall change 45.1 kB 45.1 kB
Legacy Client Bundles (polyfills)
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Overall change 39.4 kB 39.4 kB
Client Pages
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
_app-HASH.js gzip 196 B 196 B
_error-HASH.js gzip 184 B 185 B N/A
amp-HASH.js gzip 502 B 504 B N/A
css-HASH.js gzip 321 B 324 B N/A
dynamic-HASH.js gzip 1.82 kB 1.82 kB N/A
edge-ssr-HASH.js gzip 258 B 257 B N/A
head-HASH.js gzip 352 B 352 B
hooks-HASH.js gzip 371 B 372 B N/A
image-HASH.js gzip 4.22 kB 4.22 kB N/A
index-HASH.js gzip 259 B 257 B N/A
link-HASH.js gzip 2.67 kB 2.68 kB N/A
routerDirect..HASH.js gzip 316 B 314 B N/A
script-HASH.js gzip 385 B 386 B N/A
withRouter-HASH.js gzip 311 B 310 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 654 B 654 B
Client Build Manifests
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
_buildManifest.js gzip 483 B 481 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
index.html gzip 529 B 526 B N/A
link.html gzip 542 B 538 B N/A
withRouter.html gzip 525 B 523 B N/A
Overall change 0 B 0 B
Edge SSR bundle Size
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
edge-ssr.js gzip 95.4 kB 95.4 kB N/A
page.js gzip 3.06 kB 3.07 kB N/A
Overall change 0 B 0 B
Middleware size
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
middleware-b..fest.js gzip 658 B 657 B N/A
middleware-r..fest.js gzip 156 B 154 B N/A
middleware.js gzip 25.5 kB 25.5 kB N/A
edge-runtime..pack.js gzip 839 B 839 B
Overall change 839 B 839 B
Next Runtimes
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
app-page-exp...dev.js gzip 171 kB 171 kB
app-page-exp..prod.js gzip 98.2 kB 98.2 kB
app-page-tur..prod.js gzip 99.9 kB 99.9 kB
app-page-tur..prod.js gzip 94.2 kB 94.2 kB
app-page.run...dev.js gzip 145 kB 145 kB
app-page.run..prod.js gzip 92.7 kB 92.7 kB
app-route-ex...dev.js gzip 22 kB 22 kB N/A
app-route-ex..prod.js gzip 15.5 kB 15.5 kB N/A
app-route-tu..prod.js gzip 15.5 kB 15.5 kB N/A
app-route-tu..prod.js gzip 15.2 kB 15.2 kB N/A
app-route.ru...dev.js gzip 21.7 kB 21.7 kB N/A
app-route.ru..prod.js gzip 15.2 kB 15.2 kB N/A
pages-api-tu..prod.js gzip 9.58 kB 9.58 kB
pages-api.ru...dev.js gzip 9.85 kB 9.85 kB
pages-api.ru..prod.js gzip 9.57 kB 9.57 kB
pages-turbo...prod.js gzip 22.5 kB 22.5 kB
pages.runtim...dev.js gzip 23.2 kB 23.2 kB
pages.runtim..prod.js gzip 22.5 kB 22.5 kB
server.runti..prod.js gzip 51.5 kB 51.5 kB
Overall change 850 kB 850 kB
build cache Overall increase ⚠️
vercel/next.js 14-2-1 ijjk/next.js ijjk/backport-fix-res-clone Change
0.pack gzip 1.6 MB 1.61 MB ⚠️ +1.1 kB
index.pack gzip 114 kB 114 kB ⚠️ +844 B
Overall change 1.72 MB 1.72 MB ⚠️ +1.94 kB
Diff details
Diff for middleware.js

Diff too large to display

Diff for edge-ssr.js

Diff too large to display

Diff for app-route-ex..ntime.dev.js

Diff too large to display

Diff for app-route-ex..time.prod.js

Diff too large to display

Diff for app-route-tu..time.prod.js

Diff too large to display

Diff for app-route-tu..time.prod.js

Diff too large to display

Diff for app-route.runtime.dev.js

Diff too large to display

Diff for app-route.ru..time.prod.js

Diff too large to display

Commit: 8f3ecff

@ijjk ijjk merged commit 887a419 into vercel:14-2-1 Oct 1, 2024
51 checks passed
@ijjk ijjk deleted the ijjk/backport-fix-res-clone branch October 1, 2024 01:52
zemnmez-renovate-bot added a commit to zemn-me/monorepo that referenced this pull request Oct 1, 2024
##### [`v14.2.14](https://github.com/vercel/next.js/releases/tag/v14.2.14)

> \[!NOTE]\
> This release is backporting bug fixes. It does **not** include all pending features/changes on canary.

##### Core Changes

-   Fix: clone response in first handler to prevent race ([#70082](vercel/next.js#70082)) ([#70649](vercel/next.js#70649))
-   Respect reexports from metadata API routes ([#70508](vercel/next.js#70508)) ([#70647](vercel/next.js#70647))
-   Externalize node binary modules for app router ([#70646](vercel/next.js#70646))
-   Fix revalidateTag() behaviour when invoked in server components ([#70446](vercel/next.js#70446)) ([#70642](vercel/next.js#70642))
-   Fix prefetch bailout detection for nested loading segments ([#70618](vercel/next.js#70618))
-   Add missing node modules to externals ([#70382](vercel/next.js#70382))
-   Feature: next/image: add support for images.remotePatterns.search ([#70302](vercel/next.js#70302))

##### Credits

Huge thanks to [@styfle](https://github.com/styfle), [@ztanner](https://github.com/ztanner), [@ijjk](https://github.com/ijjk), [@huozhi](https://github.com/huozhi) and [@wyattjoh](https://github.com/wyattjoh) for helping!
github-merge-queue bot pushed a commit to zemn-me/monorepo that referenced this pull request Oct 1, 2024
##### [`v14.2.14](https://github.com/vercel/next.js/releases/tag/v14.2.14)

> \[!NOTE]\
> This release is backporting bug fixes. It does **not** include all pending features/changes on canary.

##### Core Changes

-   Fix: clone response in first handler to prevent race ([#70082](vercel/next.js#70082)) ([#70649](vercel/next.js#70649))
-   Respect reexports from metadata API routes ([#70508](vercel/next.js#70508)) ([#70647](vercel/next.js#70647))
-   Externalize node binary modules for app router ([#70646](vercel/next.js#70646))
-   Fix revalidateTag() behaviour when invoked in server components ([#70446](vercel/next.js#70446)) ([#70642](vercel/next.js#70642))
-   Fix prefetch bailout detection for nested loading segments ([#70618](vercel/next.js#70618))
-   Add missing node modules to externals ([#70382](vercel/next.js#70382))
-   Feature: next/image: add support for images.remotePatterns.search ([#70302](vercel/next.js#70302))

##### Credits

Huge thanks to [@styfle](https://github.com/styfle), [@ztanner](https://github.com/ztanner), [@ijjk](https://github.com/ijjk), [@huozhi](https://github.com/huozhi) and [@wyattjoh](https://github.com/wyattjoh) for helping!
renovate bot added a commit to mmkal/eslint-plugin-mmkal that referenced this pull request Oct 1, 2024
##### [v14.2.14](https://github.com/vercel/next.js/releases/tag/v14.2.14)

> \[!NOTE]\
> This release is backporting bug fixes. It does **not** include all pending features/changes on canary.

##### Core Changes

-   Fix: clone response in first handler to prevent race ([#70082](vercel/next.js#70082)) ([#70649](vercel/next.js#70649))
-   Respect reexports from metadata API routes ([#70508](vercel/next.js#70508)) ([#70647](vercel/next.js#70647))
-   Externalize node binary modules for app router ([#70646](vercel/next.js#70646))
-   Fix revalidateTag() behaviour when invoked in server components ([#70446](vercel/next.js#70446)) ([#70642](vercel/next.js#70642))
-   Fix prefetch bailout detection for nested loading segments ([#70618](vercel/next.js#70618))
-   Add missing node modules to externals ([#70382](vercel/next.js#70382))
-   Feature: next/image: add support for images.remotePatterns.search ([#70302](vercel/next.js#70302))

##### Credits

Huge thanks to [@styfle](https://github.com/styfle), [@ztanner](https://github.com/ztanner), [@ijjk](https://github.com/ijjk), [@huozhi](https://github.com/huozhi) and [@wyattjoh](https://github.com/wyattjoh) for helping!
renovate bot added a commit to mmkal/eslint-plugin-mmkal that referenced this pull request Oct 1, 2024
##### [v14.2.14](https://github.com/vercel/next.js/releases/tag/v14.2.14)

> \[!NOTE]\
> This release is backporting bug fixes. It does **not** include all pending features/changes on canary.

##### Core Changes

-   Fix: clone response in first handler to prevent race ([#70082](vercel/next.js#70082)) ([#70649](vercel/next.js#70649))
-   Respect reexports from metadata API routes ([#70508](vercel/next.js#70508)) ([#70647](vercel/next.js#70647))
-   Externalize node binary modules for app router ([#70646](vercel/next.js#70646))
-   Fix revalidateTag() behaviour when invoked in server components ([#70446](vercel/next.js#70446)) ([#70642](vercel/next.js#70642))
-   Fix prefetch bailout detection for nested loading segments ([#70618](vercel/next.js#70618))
-   Add missing node modules to externals ([#70382](vercel/next.js#70382))
-   Feature: next/image: add support for images.remotePatterns.search ([#70302](vercel/next.js#70302))

##### Credits

Huge thanks to [@styfle](https://github.com/styfle), [@ztanner](https://github.com/ztanner), [@ijjk](https://github.com/ijjk), [@huozhi](https://github.com/huozhi) and [@wyattjoh](https://github.com/wyattjoh) for helping!
@mogzol
Copy link

mogzol commented Oct 9, 2024

Still seeing the error even after this backport, running version 14.2.15

@mogzol
Copy link

mogzol commented Oct 9, 2024

Looking at where the error is happening, it looks like it is actually occurring on the res.clone() line above where these changes were made, line 738 in the diff

@ijjk
Copy link
Member Author

ijjk commented Oct 9, 2024

Hi, could you share the error you're seeing on latest version?

@mattandre
Copy link

I am seeing the following error on v14.2.15
TypeError: Response.clone: Body has already been consumed.
at webidl.errors.exception (node:internal/deps/undici/undici:3384:14)
at _Response.clone (node:internal/deps/undici/undici:8883:31)
at /app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:181023

@mogzol
Copy link

mogzol commented Oct 10, 2024

Yes, that's the same error I'm seeing:

TypeError: Response.clone: Body has already been consumed.
at webidl.errors.exception (node:internal/deps/undici/undici:3384:14)
at _Response.clone (node:internal/deps/undici/undici:8883:31)
at /app/node_modules/.pnpm/next@14.2.15_react-dom@18.3.1_react@18.3.1__react@18.3.1_sass@1.79.4/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:181023

Line 12, column 181023 corresponds to this line in the source code: https://github.com/vercel/next.js/blob/v14.2.15/packages/next/src/server/lib/patch-fetch.ts#L738

@zsoltiii
Copy link

As previous comments, we are still seeing the following error on v14.2.14 on Vercel

TypeError: Response.clone: Body has already been consumed.
    at i.errors.exception (/opt/rust/undici.js:1:198719)
    at lA.clone (/opt/rust/undici.js:39:21106)
    at /var/task/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:181023

@ijjk
Copy link
Member Author

ijjk commented Oct 10, 2024

Thanks for the additional information follow-up fix opened in #71115

ijjk added a commit that referenced this pull request Oct 10, 2024
Follow-up to #70649 this tweaks
the backport to properly clone and return original response so that
pending revalidates aren't sharing an already used response.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants