vercel / vercel/next.js

Response body object should not be disturbed or locked" error with Node.js middleware and large request bodies

Open
#83,453 14 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

linear: next Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/platformshape/nextjs-body-bug-repro

To Reproduce
  1. npm install && npm run dev
  2. chmod +x test.sh && ./test.sh 500 && ./test.sh 500

The issue is intermittent and never fails on the first request after launching the server. It does however fail after a few tries.

Current vs. Expected behavior

Next.js v15.5.2 throws a fatal error when using Node.js runtime middleware with large request bodies (>350MB):

TypeError: Response body object should not be disturbed or locked
    at extractBody (node:internal/deps/undici/undici:5574:17)
    at new Request (node:internal/deps/undici/undici:9801:48)
    at new NextRequest (/path/to/next/server/chunks/node_modules_next_dist_*.js)

This results in a 500 Internal Server Error and prevents processing of large file uploads through middleware.

Expected: Large file uploads processed successfully through middleware to API route.

Actual:

  • Files ≤350MB: Work correctly
  • Files >350MB: Fatal error with 500 status

Test Results

File Size Status Notes
100MB ✅ Works No issues
300MB ✅ Works No issues
350MB ✅ Works Threshold
375MB ❌ Fails Consistent failure
400MB+ ❌ Fails Always fails

Error Details

Console Error:

TypeError: Response body object should not be disturbed or locked
    at extractBody (node:internal/deps/undici/undici:5574:17)
    at new Request (node:internal/deps/undici/undici:9801:48)
    at new NextRequest (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:5368:14)
    at NextRequestAdapter.fromNodeNextRequest (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:5539:16)
    at handler (/private/tmp/my-app/.next/server/chunks/node_modules_next_dist_eb0c80da._.js:8127:246)

Server Logs:

Middleware: POST http://localhost:3000/api/upload
⨯ [TypeError: Response body object should not be disturbed or locked] {
  page: '/api/upload'
}
POST /api/upload 500 in 1063ms
Provide environment information
- **Next.js version**: 15.5.2
- **Node.js version**: v23.1.0 (affects all Node versions)
- **Operating System**: macOS (affects all platforms)
- **Runtime**: Node.js middleware (not Edge runtime)
Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Analysis

Code Path Investigation

The error originates from Next.js's Node.js middleware body cloning mechanism:

  1. Entry Point: packages/next/src/server/next-server.ts:1715

    body: hasRequestBody ? requestData.body.cloneBodyStream() : undefined,
    
  2. Body Cloning Logic: packages/next/src/server/body-streams.ts:75-88

    • Creates PassThrough streams for body cloning
    • Sets up event listeners synchronously
  3. Error Location: undici's extractBody function detects stream as "disturbed"

    • Occurs during new NextRequest() construction
    • undici validates stream state before processing
Behavioral Observations
  • Size Dependency: Error threshold consistently around 350-375MB
  • Race Condition: Intermittent failures suggest timing-related issue
  • Runtime Specific: Only affects runtime: "nodejs", not "edge"
  • Middleware Required: Error only occurs when Node.js middleware is present
  • Stream State: Middleware executes successfully before error occurs
Undici Interaction

The error originates from Node.js's internal undici module checking if the request body stream has been "disturbed" (consumed or locked). This suggests a conflict between:

  • Next.js's body cloning PassThrough streams
  • undici's stream state validation
  • Timing of large body processing

Impact

This bug affects any Next.js application using:

  • Node.js runtime middleware
  • Large request bodies (file uploads, API proxies, form data)
  • Body processing in API routes after middleware

Environment Variants Tested

  • Works: Edge runtime middleware
  • Works: No middleware
  • Works: Small request bodies with Node.js middleware
  • Fails: Large request bodies with Node.js middleware

Additional Notes

  • Bug appears to be related to body stream management in Node.js runtime
  • Error is not recoverable - results in complete request failure
  • No client-side JavaScript required to reproduce
  • Issue is server-side only

NEXT-4732

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction at https://github.com/platformshape/nextjs-body-bug-repro and run npm install && npm run dev, then ./test.sh 500 twice. Read packages/next/src/server/next-server.ts around line 1715 and packages/next/src/server/body-streams.ts around lines 75-88, focusing on the Node.js middleware body clone path. Done means requests above 350MB reach the API route without the undici disturbed-or-locked error or a 500 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, node.js, typescript
Domain
api, backend, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.