Skip to content

fix(ClientRequest): inherit the connection _handle for passthrough requests#706

Merged
kettanaito merged 7 commits intomainfrom
Michael/fix-mock-socket-has-no-handle
May 27, 2025
Merged

fix(ClientRequest): inherit the connection _handle for passthrough requests#706
kettanaito merged 7 commits intomainfrom
Michael/fix-mock-socket-has-no-handle

Conversation

@mikicho
Copy link
Copy Markdown
Member

@mikicho mikicho commented Feb 8, 2025

Since the MockSocket never actually connects to a server, it does not initialize its _handle property. As a result, this push creates a connect listener with every push.

@kettanaito Please guide me in the leftovers:

  1. Fix the test name.
  2. Should the test be in this file?
  3. this._handle is private; there is an undocumented handle option in the Socket constructor. How do you think we should handle this?

Fixes: nock/nock#2830

@mikicho mikicho requested a review from kettanaito February 8, 2025 23:42
@rossipedia
Copy link
Copy Markdown

I wonder if this could be the underlying cause of mswjs/msw#2405?

@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Mar 21, 2025

Yes.. it might fix this as well. but I'm not sure about it

it('does ', async () => {
const url = httpServer.http.url('/')
const warningListener = vi.fn()
process.on('warning', warningListener)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indirect assertion. I think you are onto something with that connect event. Can we somehow add a listener to that instead and assert that it's being called once? That would make a nice compliance test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delightful comment! Fixed.

Copy link
Copy Markdown
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great discovery! It needs more discussion before we proceed with the fix. Would be grateful for your opinion on the comments I left.

@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Apr 19, 2025

@kettanaito WDYT?

@kettanaito kettanaito changed the title fix(ClientRequest): MockSocket does not have _handle in passthrough fix(ClientRequest): MockSocket does not have _handle in passthrough May 27, 2025
@kettanaito kettanaito changed the title fix(ClientRequest): MockSocket does not have _handle in passthrough fix(ClientRequest): preserve the connection _handle for passthrough requests May 27, 2025
@kettanaito kettanaito changed the title fix(ClientRequest): preserve the connection _handle for passthrough requests fix(ClientRequest): inherit the connection _handle for passthrough requests May 27, 2025
Copy link
Copy Markdown
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few tiny adjustments. This looks great. Let's see what the tests tell us.

Object.defineProperty(this, '_handle', {
value: socket._handle,
enumerable: true,
writable: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_handle has to be writable. Node overrides it themselves a few times during the request's life cycle.

@kettanaito kettanaito merged commit 99967d7 into main May 27, 2025
2 checks passed
@kettanaito kettanaito deleted the Michael/fix-mock-socket-has-no-handle branch May 27, 2025 12:42
@kettanaito
Copy link
Copy Markdown
Member

Released: v0.38.7 🎉

This has been released in v0.38.7!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@saikumarrs
Copy link
Copy Markdown

Hello @kettanaito @mikicho
I believe this change has introduced another bug. I'm seeing the following error when I upgraded to the latest version of msw which uses 0.38.7.

node: ../deps/uv/src/unix/stream.c:456: uv__stream_destroy: Assertion `!uv__io_active(&stream->io_watcher, POLLIN | POLLOUT)' failed.

I use msw in my test suites using jest and I'm observing the worker is terminated with SIGTERM.

I locally patched the files to comment these changes and it worked.
Please look into it.
I'd be happy to open an issue for this if it seems legit.

@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Jun 3, 2025

@saikumarrs Thank you for reporting this! Could you please open an issue with a minimal reproduction?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible EventEmitter memory leak when used together with MongoDBContainer (Testcontainers)

4 participants