简体   繁体   中英

How does Node.js handle unclosed HTTP responses

Consider a trivial express app:

app.get '/hang', (req, res) ->
  console.log 'Request ', n
  n += 1
  # res.send 200

Whoops, I've accidentally commented out the res.send , oh well, let's see what happens!

If I hit /hang with my browser it just sits there spinning, which makes sense. Without the send we're not closing the connection (correct assumption?). Let's see what happens if we hit /hang with a bunch of simultaneous connections :

$ ab -n 1000 -c 1000 http://localhost:3000/hang

Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (104)

The app manages to print out a couple of hundred Request n s and then just gets stuck.

My questions are:

  1. What's happening here?
  2. Will these connections eventually be closed? Or:
  3. Is my server now hosed until it's restarted?

Additional:

You can pass -r to ab to have it ignore socket errors. When I use that I start to see ab listing some requests as being completed:

Completed 100 requests
Completed 200 requests
apr_pollset_poll: The timeout specified has expired (70007)

Could this be the connections being closed by node.js?

What's happening here?

The connections just sit idly doing nothing. They do cost memory to keep open, but negligible CPU.

Will these connections eventually be closed?

Yes, I believe the OS's network layer will eventual close them with a timeout error. Either the client or server could trigger this depending on configuration (keepalives and timeout values in particular).

Is my server now hosed until it's restarted?

No, you can cleanly recover from a certain number of these without a restart. So if in a real server a bug caused 1 out of every 1000 requests to hang without a response, for a given traffic load, it would be manageable. It's basically a function of hung connections per unit of traffic and traffic load to determine whether this is a survivable error or will cascade out of control until your server exhausts resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM