Why are client HTTP connections not getting closed?

Viewed 59

I have a node.js (12.18.4) server application which listens for HTTP connections. Our tests use mocha and request, and we make many connections to the server. At the end of the testing, we shut down the server but first we make sure that all connections have been closed by calling server.getConnections and asserting that the value is 0. The vast majority of the time it is, but sometimes the value returned is non-zero, usually around 60-70. We make far more than 60-70 requests during the tests. No tests failed or got stuck.

How can I determine why the server thinks these connections are still around?

1 Answers

I question the value of asserting on the number of active server connections — if all of your tests have completed successfully, what difference does it make if your OS is slow to clean up some TCP connections?

At any rate, I'd ditch the getConnections check and just call server.close(cb).

The close callback is invoked immediately if there are no active connections, or it waits until all remaining connections have closed before firing. A test timeout should occur if the connections don't drain.

Related