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

TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed #55608

Closed
1 task done
willianrod opened this issue Sep 19, 2023 · 28 comments · Fixed by #55684
Closed
1 task done

TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed #55608

willianrod opened this issue Sep 19, 2023 · 28 comments · Fixed by #55684
Labels
bug Issue was opened via the bug report template. locked

Comments

@willianrod
Copy link

willianrod commented Sep 19, 2023

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.

#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
@willianrod willianrod added the bug Issue was opened via the bug report template. label Sep 19, 2023
@icyJoseph
Copy link
Contributor

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

Could someone else double check by using [email protected], verify no error, and then use [email protected], to see the error?

@willianrod
Copy link
Author

@icyJoseph yep, I can confirm that the error started after [email protected].

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 [email protected] and [email protected]
v13.4.13-canary.3...v13.4.13-canary.4#diff-9be48534a043c68023eef0f61dacd526b677c70ee3b61807b9da74a279aba2e5R41

@icyJoseph
Copy link
Contributor

icyJoseph commented Sep 19, 2023

Ah I had selected the wrong tags when comparing... 🤦‍♂️

Screenshot 2023-09-19 at 23 28 38

I was deeply puzzled...

@Hardel-DW
Copy link

Hardel-DW commented Sep 19, 2023

https://github.com/Hardel-DW/next-issue

Same problem with Suspense and "loading.tsx" file.

I encounter the same problem when I reload my page before the request is completed, resulting in error messages. If I quickly reload it several times, it seems that the requests accumulate. When I utilize "loading.tsx," the error appears to be different.

image
azd

image

@FaryarKankash
Copy link

FaryarKankash commented Sep 20, 2023

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

@andycansdale
Copy link

Yup, we're seeing the same in 13.5.1. I've not been able to track down the source of the error.

@Hardel-DW
Copy link

Problem still present in 13.5.2

@fadjrie
Copy link

fadjrie commented Sep 20, 2023

experiencing that problem, how to fix?

@jridgewell
Copy link
Contributor

This should be fixed by facebook/react#27405

@cheryl-c-tirerack
Copy link

cheryl-c-tirerack commented Sep 22, 2023

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

Still happens.

@igloo1505
Copy link

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...

@wriddhi
Copy link

wriddhi commented Sep 26, 2023

@igloo1505 I'm facing this issue too but without having downsides in performance or code execution flow. I'm building an application for production. Have you seen/felt any real consequences/crashes issue with this yet or found a workaround? I really don't wanna rollback next to 13.4.5.

@huozhi
Copy link
Member

huozhi commented Sep 26, 2023

Should be fixed by #56012 which carried the react change

@huozhi huozhi closed this as completed Sep 26, 2023
@rexedge
Copy link

rexedge commented Sep 26, 2023

Screenshot 2023-09-26 at 15 58 15

I am facing the same issue but my app works fine.

@amyfranz
Copy link

I'm also still getting it on Next.js 13.5.3

@huozhi
Copy link
Member

huozhi commented Sep 27, 2023

The fix is landed in 13.5.4-canary.2

@cheryl-c-tirerack
Copy link

cheryl-c-tirerack commented Sep 27, 2023

upgraded to 13.5.4-canary.2

still I get
image

@jridgewell
Copy link
Contributor

@cheryl-c-tirerack That's a different issue, it just happen to originate from the same file. I'm not sure where we need to handle it.

@huozhi
Copy link
Member

huozhi commented Sep 27, 2023

@cheryl-c-tirerack would be possible for you to file a reproduction in new issue? That would be super helpful for investigation 🙏

@cheryl-c-tirerack
Copy link

cheryl-c-tirerack commented Sep 27, 2023

@cheryl-c-tirerack would be possible for you to file a reproduction in new issue? That would be super helpful for investigation 🙏

I wouldn't begin to know what to reproduce minimally. I have a full backend running locally, and many nested dynamic paths-- all proprietary. I'm using app directory, with TanStack/react-query.
It happens on any page that touches my api.
Exactly as the original issue, my api calls are in server components.
It happens right after navigating to a new endpoint.
The description and the error are exactly as shown on this OP, without any authorization.

@dstarosta
Copy link

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.

@Dev-Harmattan
Copy link

I get the same issue. is there any fix for this ?
⨯ Internal error: TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed at new NodeError (node:internal/errors:405:5) at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1152:13) at /Users/sodiq/Desktop/potfolio-project/discord-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:461602

@V-iktor
Copy link

V-iktor commented Sep 28, 2023

I get the same issue. is there any fix for this ?
⨯ Internal error: TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed at new NodeError (node:internal/errors:405:5) at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1152:13) at /Users/sodiq/Desktop/potfolio-project/discord-app/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:1:461602

Try updating to the latest React canary - now I get the correct error message instead of ERR_INVALID_STATE

yarn add [email protected]

The correct message I get after upgrading

TypeError: fetch failed

@luistak
Copy link

luistak commented Sep 29, 2023

Same here on 13.5.2 but debugging the error stacktrace i caught that this error occurs in this order:

image

  1. `Server component Loading
  2. Error: The render was aborted by the server without a reason.
Error: The render was aborted by the server without a reason.
    at nR (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:67960)
    at Object.cancel (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:71720)
    at readableByteStreamControllerCancelSteps (node:internal/webstreams/readablestream:3022:37)
    at [kCancel] (node:internal/webstreams/readablestream:1166:12)
    at ensureIsPromise (node:internal/webstreams/util:192:19)
    at readableStreamCancel (node:internal/webstreams/readablestream:1887:5)
    at node:internal/webstreams/readablestream:1439:15
    at complete (node:internal/webstreams/readablestream:1323:9)
    at shutdownWithAnAction (node:internal/webstreams/readablestream:1319:5)
    at node:internal/webstreams/readablestream:1438:14
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  1. TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed
    at new NodeError (node:internal/errors:405:5)
    at ReadableByteStreamController.enqueue (node:internal/webstreams/readablestream:1152:13)
    at u (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:486)
    at nP (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:67693)
    at nR (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:68650)
    at Object.cancel (/app/app/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:45:71720)
    at readableByteStreamControllerCancelSteps (node:internal/webstreams/readablestream:3022:37)
    at [kCancel] (node:internal/webstreams/readablestream:1166:12)
    at ensureIsPromise (node:internal/webstreams/util:192:19)
    at readableStreamCancel (node:internal/webstreams/readablestream:1887:5)
    at node:internal/webstreams/readablestream:1439:15
    at complete (node:internal/webstreams/readablestream:1323:9)
    at shutdownWithAnAction (node:internal/webstreams/readablestream:1319:5)
    at node:internal/webstreams/readablestream:1438:14
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  1. SocketError: other side closed
node:internal/deps/undici/undici in TLSSocket.onSocketEnd at line 9790:26
In App
node:events in TLSSocket.emit at line 526:35
In App
node:domain in TLSSocket.emit at line 489:12
In App
Called from: /app/app/node_modules/dd-trace/packages/datadog-instrumentations/src/net.js in TLSSocket.emit
node:internal/streams/readable in endReadableNT at line 1359:12
In App
node:internal/process/task_queues in process.processTicksAndRejections at line 82:21

Which Sentry identified that may be related to this other issue #49896:
image

@Zanzofily
Copy link

Zanzofily commented Oct 6, 2023

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'

@amiryxe
Copy link

amiryxe commented Oct 6, 2023

I get this error when I refresh a page quickly and one after another.

@DevOkone
Copy link

DevOkone commented Oct 8, 2023

Je reçois aussi le problème sur Next.js 13.5.3 mais j'ai l'impression que l'erreur ce déclenche lorsque j'utilise suspense sur une page
mais le code fonctionne bien en production

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.