Skip to content

Set socket connecting state to always be false - #1182

Merged
gr2m merged 1 commit into
nock:masterfrom
griffinmyers:will/socket-delay
Aug 13, 2018
Merged

gr2m merged 1 commit into
nock:masterfrom
griffinmyers:will/socket-delay

Conversation

@griffinmyers

@griffinmyers griffinmyers commented Aug 3, 2018 •

Copy link
Copy Markdown
Contributor

Background

I recently encountered the socketDelay bug noted in #1174 (at least, fairly sure). More specifically, I observed breakage running my test suite on node versions >=10.7.0.

nodejs/node#21204 looks like an extremely likely upstream cause of the behavior I'm experiencing with nock. It was released in node v10.7.0 so the timing fits as well.

Notably, before that change, if a request's socket attribute was truthy and writeable, req.setTimeout would invoke socket.setTimeout without inspecting any connection state, like socket.connecting (ref). Now, req.setTimeout always checks the current state of the socket (ref):

  • If a socket is currently connecting, we defer registering the timeout until after the socket emits the 'connect' event.
  • If a socket is not currently connecting, we immediately register a timeout.

I'm not super well versed in nock's guiding philosophy, but poking around the code it didn't seem like the mock socket implementation was particularly full featured or attempted to accurately model real socket behavior. For instance, socket.connecting is set to true here, but is never set to false. Further, the only time it looks like the 'connect' event will be emitted is as a side-effect of registering the 'socket' event on req (ref), which is a bit weird (and fails to set socket.connecting to false even though it ought to be given the docs)

Suggested Fix

Because the life-cycle of a socket isn't really modeled in nock, my suggested fix is to simply pretend as if the socket is already connected at the time of issuing the request. At least, this fixes the issues I'm experiencing.

A few tests appear to be broken on master, but this patch doesn't appear to introduce any testing regressions, and fixes a handful in fact. There looks to a cert expiration issue effecting some of the tests, I'll post back with any thoughts as I dig a bit more into it.

Happy to discuss alternative ideas!

edit: looks like the CA expiration was already tackled in https://github.com/nock/nock/pull/1181/files !

fixes #1174

@gr2m

gr2m commented Aug 7, 2018

Copy link
Copy Markdown
Member

Hi @griffinmyers thanks for the pull request :) I’m catching up after a leave, I can’t promise when I get the time to look into it, but I’m very open to improve the implementation of the socket mocking. I’m not very familiar with it, so if you would be okay with helping out in case things break around it in future, I’d be happy to move forward with whatever you suggest, as long as current tests pass

@gr2m

gr2m commented Aug 7, 2018

Copy link
Copy Markdown
Member

When you rebase on latest master then the SSL-related errors should resolve

@griffinmyers

Copy link
Copy Markdown
Contributor Author

Awesome, thanks @gr2m -- just pushed an update. Looks like this change fixes up 41 tests that break on master on node >=10.7.0.

Happy to lurk and pitch in with any future socket issues.

@gr2m
gr2m merged commit ca1eea0 into nock:master Aug 13, 2018
@nockbot

nockbot commented Aug 13, 2018

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 9.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@griffinmyers
griffinmyers deleted the will/socket-delay branch August 13, 2018 22:19
@lock

lock Bot commented Sep 13, 2018

Copy link
Copy Markdown

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

@lock lock Bot locked as resolved and limited conversation to collaborators Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants