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

Fixing socket hang up error on node side for slow response. #1752

Merged
merged 10 commits into from Sep 16, 2019

Conversation

ryouaki
Copy link
Contributor

@ryouaki ryouaki commented Aug 23, 2018

Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.

And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.

At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.

And then these socket which be hang up will devoring CPU little by little.ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.

So I change the code to use the timeout event for connect timeout on nodejs side.

My issue
I think this issue is the same reason

@ryouaki
Copy link
Contributor Author

ryouaki commented Aug 27, 2018

@mzabriskie can you help me?

@ryouaki
Copy link
Contributor Author

ryouaki commented Aug 27, 2018

@nickuraltsev could you help me?

@RikkiGibson
Copy link
Contributor

It looks like request.setTimeout sets a read timeout handler on the underlying socket. It's not the same thing as a request timeout.

Is the issue is that aborting a request before the connection is established doesn't properly dispose of the socket?

@ryouaki
Copy link
Contributor Author

ryouaki commented Sep 3, 2018

@RikkiGibson Yes,abort a request before the connection, and does not release socket.

When the server side is too slow and no responed for a long time, sometimes setTimeout will be invoked before connection, and "socket hang up" will be got. This only appear at connection does not block setTimeout.

I think for Borwser the request timeout is not the same as connection timeout, But on nodejs I think it is the same. and sometimes the connection block setTimeout. So if we set 10s for timeout ,but the connection block the event loop about 20s, the setTimeout will be fire after 20s.

poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate.

@RikkiGibson
Copy link
Contributor

The request timeout applies to receiving the entire HTTP response.

The socket timeout, set by request.setTimeout, applies to any activity on the socket (individual reads and writes). The browser doesn't provide any way to set this.

I said "read timeout" previously because a lot of languages will let you configure read and write timeouts separately, while node appear to combine them into one. Sorry for any confusion.

Let's use the setTimeout function like before so that we don't change the meaning of the timeout property. In the timeout handler, we can do something like the following:

if (request.socket.connecting) {
  request.socket.on('connect', function() {
    request.abort();
  }
} else {
  request.abort();
}

Does that seem reasonable?

@ryouaki
Copy link
Contributor Author

ryouaki commented Sep 5, 2018

@RikkiGibson Thank you for your explain,

Maybe my english is not good enough. Yes you want to do timeout like borwser .But sometimes the io event will block setTimeout. the setTimeout will not be invoked in time.

for my case like I said before:

  1. I set 10s for timeout by setTimeout function.
  2. Send a request to a low server . and the server very busy. and no respond.
  3. The connection will block setTimeout function. until connection finish. Nodejs event loop
  4. Connection finish after 20s, the setTimeout function will be invoked after 20s.

So, when connection block setTimeout function, The timeout will be fired more than millseconds you set before.

This is my problem.

@ryouaki
Copy link
Contributor Author

ryouaki commented Sep 11, 2018

Nobody else?

@ryouaki
Copy link
Contributor Author

ryouaki commented Sep 11, 2018

@blackswanny
Copy link

I have this issue as well. We have plenty of requests open at the same time

@freitasskeeled
Copy link

Hi there,

Is there any updates on this PR? I think I'm suffering from the same problem.

Thanks

@ryouaki
Copy link
Contributor Author

ryouaki commented Apr 4, 2019

suffering

I have the same question......

@hangaoke1
Copy link

mark

@vivek-kandhvar
Copy link

Any update?

@snowdream
Copy link

I have the same question

@dalewang1995
Copy link

mark

@goulderb
Copy link

Is any priority going to be given to this issue? I'm hitting this in production and it's forcing us off axios.

@ryouaki
Copy link
Contributor Author

ryouaki commented Aug 29, 2019

Is any priority going to be given to this issue? I'm hitting this in production and it's forcing us off axios.

this pr need another code review

mooyoul added a commit to mooyoul/axios that referenced this pull request Apr 5, 2020
mooyoul added a commit to mooyoul/axios that referenced this pull request Apr 6, 2020
@axios axios locked and limited conversation to collaborators May 3, 2020
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.

None yet