-
Notifications
You must be signed in to change notification settings - Fork 29.3k
Description
Link to the code that reproduces this issue
https://github.com/platformshape/nextjs-body-bug-repro
To Reproduce
npm install && npm run dev
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:
-
Entry Point:
packages/next/src/server/next-server.ts:1715
body: hasRequestBody ? requestData.body.cloneBodyStream() : undefined,
-
Body Cloning Logic:
packages/next/src/server/body-streams.ts:75-88
- Creates PassThrough streams for body cloning
- Sets up event listeners synchronously
-
Error Location: undici's
extractBody
function detects stream as "disturbed"- Occurs during
new NextRequest()
construction - undici validates stream state before processing
- Occurs during
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