next.js: TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed

Link to the code that reproduces this issue

https://github.com/willianrod/next-readable-stream-error

To Reproduce

  1. Run the project locally
  2. Go to /auth/login
  3. The app will redirect you to /sample/list after resolving the promise
  4. You should now see the error on your console

Current vs. Expected behavior

Current: Right now, when I’m navigating between pages with server components I’m seeing errors on my development console. Expected: When navigating between pages no errors should be prompted

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:23 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6020
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: 8.6.12
    Relevant Packages:
      next: 13.5.1
      eslint-config-next: 13.5.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

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

App Router

Additional context

There is a discussion with more people with the same issue. The latest request was to update to the latest version 13.5.1 and see if the issue was still happening.

https://github.com/vercel/next.js/discussions/55027

Internal error: TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
    at new NodeError (node:internal/errors:399:5)
    at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1150:13)
    at /path/to/app/readable-error/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:447268
    at ut (/path/to/app/readable-error/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:447318)
    at ct (/path/to/app/readable-error/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:446596)
    at Timeout._onTimeout (/path/to/app/readable-error/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:443766)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Reactions: 18
  • Comments: 28 (8 by maintainers)

Most upvoted comments

Should be fixed by #56012 which carried the react change

"next": "13.5.3-canary.1",

Still happens.

Looks like a form of this error started to happen after:

  • next@13.4.13-canary.4

Could someone else double check by using next@13.4.13-canary.3, verify no error, and then use next@13.4.13-canary.4, to see the error?

I tried updating next and node to the latest versions, but the error was still present. It seems to be related to how caching is handled. Changing caching to dynamic worked for me.

export const dynamic = 'force-dynamic'

upgraded to 13.5.4-canary.2

still I get image

I get the same issue as well. When I run Playwright tests it happens in the same place the old error was happening.

upgraded to 13.5.4-canary.2

still I get image

Has anyone noticed any actual negative effects from this? I’m working on two separate apps more or less simultaneously, and they’re both experiencing this same error but both are performing as they should as far as I can tell, but it is a bit concerning…

Same problem here i don’t use suspense in my layout but i use loading.tsx file this error doesn’t cause any problem in my build (i update next to 13.5.1 but it’s not fixing anything!)

Screenshot 2023-09-20 091244

Screenshot 2023-09-20 093504

@icyJoseph yep, I can confirm that the error started after next@13.4.13-canary.4.

TypeError [ERR_INVALID_STATE]: Invalid state: Unable to enqueue
    at new NodeError (node:internal/errors:399:5)
    at transformStreamDefaultControllerEnqueue (node:internal/webstreams/transformstream:488:11)
    at TransformStreamDefaultController.enqueue (node:internal/webstreams/transformstream:301:5)
    at Timeout._onTimeout (/path/to/app/readable-error/node_modules/next/dist/server/stream-utils/node-web-streams-helper.js:166:32)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  code: 'ERR_INVALID_STATE'
}
- error unhandledRejection: TypeError [ERR_INVALID_STATE]: Invalid state: Unable to enqueue
    at new NodeError (node:internal/errors:399:5)
    at transformStreamDefaultControllerEnqueue (node:internal/webstreams/transformstream:488:11)
    at TransformStreamDefaultController.enqueue (node:internal/webstreams/transformstream:301:5)
    at Timeout._onTimeout (/path/to/app/readable-error/node_modules/next/dist/server/stream-utils/node-web-streams-helper.js:166:32)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  digest: undefined
}
- error TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed

If I would guess, the error is originated on pipeReadable

Diff between next@13.4.13-canary.3 and next@13.4.13-canary.4 https://github.com/vercel/next.js/compare/v13.4.13-canary.3...v13.4.13-canary.4#diff-9be48534a043c68023eef0f61dacd526b677c70ee3b61807b9da74a279aba2e5R41