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

Mocked stream response with multiple enqueues hangs process #2051

Closed
4 tasks done
aduth opened this issue Feb 21, 2024 · 6 comments
Closed
4 tasks done

Mocked stream response with multiple enqueues hangs process #2051

aduth opened this issue Feb 21, 2024 · 6 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@aduth
Copy link

aduth commented Feb 21, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

20.11.1

Reproduction repository

https://github.com/aduth/msw-repro

Reproduction steps

  1. Run npm install
  2. Run node index.js

Current behavior

Output logs "text helloworld" and the process hangs indefinitely.

Expected behavior

Output logs "text helloworld" and the process ends cleanly.

@aduth aduth added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Feb 21, 2024
@aduth
Copy link
Author

aduth commented Feb 21, 2024

With the reproduction example, I'm largely copying directly from the documented example: https://mswjs.io/docs/recipes/streaming

Strangely, calling enqueue once seems to work fine.

Using why-is-node-running, there's a few pointers referencing the clone happening in the fetch interceptor.

@kettanaito
Copy link
Member

Hi, @aduth. Thanks for reporting this!

Interesting. Your use case looks almost identical to this test we have passing:

test('responds with a ReadableStream', async () => {
server.use(
http.get('https://api.example.com/stream', () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode('hello'))
controller.enqueue(encoder.encode('world'))
controller.close()
},
})
return new HttpResponse(stream, {
headers: {
'Content-Type': 'text/event-stream',
},
})
}),
)
const response = await fetch('https://api.example.com/stream')
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.body).toBeInstanceOf(ReadableStream)
expect(response.body!.locked).toBe(false)
expect(await response.text()).toBe('helloworld')
})

Does anything different catch your eye between these two? We should certainly support pushing as many chunks as you need.

@kettanaito
Copy link
Member

Can you try this on the latest minor version of Node.js v18?

I could reproduce your issue on v18.14.2 but the issue is gone on v18.19.0. Node.js can have issues too! In fact, there have been a few in the past that broke FormData. Changing the Node.js version makes your reproduction repository pass (the process exists properly).

@aduth
Copy link
Author

aduth commented Mar 20, 2024

I just re-tested on latest versions of Node.js 18 and 20 (18.19.1 and 20.11.1) and unfortunately still seems to hang in both versions.

Regarding your comment on the similarities, yes, I tried code adapted both from documentation and from those exact tests to try to use in my project, which is why I was surprised when I found it wasn't working.

@kettanaito
Copy link
Member

@aduth, quite strange. Your repo passes for me on v18.19.

This is very unlikely an issue with MSW. I encourage you to look at Node.js/Unidici issues, especially related to request body cloning. For example, I've reported one today to Node.js (nodejs/node#52167), which is actually an active issue in Undici (nodejs/undici#2413).

@aduth
Copy link
Author

aduth commented Mar 21, 2024

That makes sense. If I find time, I'll try testing on another device to see if it's specific to my machine. In the meantime, fair to assume it's something outside MSW.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

2 participants