Ruby: Why do CLOSE_WAIT TCP connections in Puma not go down as expected?

March 28, 2019 00:36

Updated

Issue

Using Ruby SDK in Puma or Unicorn cluster mode, with multiple workers of one thread each, as the SDK is sending treatment events, CLOSE_WAIT TCP Connections usually increase, this can be detected using the command below:

lsof -l | grep CLOSE_WAIT | wc -l

however, when no SDK treatment calls placed, the CLOSE_WAIT TCP Connections count do not go down as expected.

Root Cause

This might be caused by SDK threads not terminating properly, which will keep the client connection waiting for the server to send the final ack signal.

Solution

Puma will spawn new process for every group of incoming requests, to terminate all running threads before Puma closes the process, add the following code in config/puma.rb