Skip to content

Race condition with Node.js middleware body cloning #85416

@lucasadrianof

Description

@lucasadrianof

Link to the code that reproduces this issue

https://github.com/lucasadrianof/nextjs-middleware-bug

To Reproduce

  1. pnpm install from the project folder.
  2. pnpm build. This will build the project and add a random delay to the body cloning process to make the bug happen more consistently.
  3. pnpm start.
  4. Open http://localhost:3000/ in your browser.
  5. Click the Send Large Payload button.
  6. The request will fail. You should see a SyntaxError: Unexpected end of JSON input in your terminal.
~/Code/Tests/nextjs-middleware-bug main* ⇡
❯ pnpm start

> nextjs-middleware-bug@0.1.0 start /Users/lucas/Code/Tests/nextjs-middleware-bug
> node .next/standalone/server.js

   ▲ Next.js 16.0.1-canary.3
   - Local:        http://localhost:3000
   - Network:      http://0.0.0.0:3000

 ✓ Starting...
 ✓ Ready in 37ms
Proxy - body length: 50333 bytes
 ⨯ SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>) {
  digest: '3042448573'
}

Current vs. Expected behavior

Current behavior:

Image

Expected behavior:

Image

The request body cloning should be properly awaited before invoking the server action handler. This is the output I get once I run the script to apply the bugfix:

To reproduce it, you can follow the steps from the previous section, but run pnpm bugfix before pnpm start. That will patch the file with the missing await, fixing the previous error.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Wed Sep 17 21:41:26 PDT 2025; root:xnu-12377.1.9~141/RELEASE_ARM64_T6041
  Available memory (MB): 24576
  Available CPU cores: 14
Binaries:
  Node: 22.13.0
  npm: 11.2.0
  Yarn: N/A
  pnpm: 10.7.1
Relevant Packages:
  next: 16.0.0 // Latest available version is detected (16.0.0).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: N/A
Next.js Config:
  output: standalone

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

Middleware, Server Actions

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

next start (local)

Additional context

Hello!

We noticed this bug in our production app, where we're running Next.js with the output: standalone option with Docker.

This bug is really hard to be reproduced locally, since it happens in a race condition, when there's some sort of delay between cloning the request body. It also depends on the request body being relatively large. It was specially tricky to reproduce it while trying to debug it, since the await in the debugging would invalidate the race condition 100% of the times.

So to make this consistently reproducible locally, I added a random delay in that specific function.

This error basically happens because we're not awaiting the body to be fully cloned before invoking the server action, in here:

requestData.body.finalize()
.

That method returns a Promise, as defined in here:

finalize(): Promise<void>
.

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.Server ActionsRelated to Server Actions.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions