Hi,
Just changed to use unidirectional shutdown does not solve the problem completely. There will still be some connections leaked in CLOSE_WAIT state. I finally find the reason. In the createSession function in TLS.hs, the function 'accept ssl' can fail because the underlining socket connection may have been terminated. If this happens, the createSession function won't return a NetworkSession, it will raise an exception, and the code in the bracket function in SimpleBackend.hs won't execute and the socket won't be closed!
The simplest solution is changing
accept ssl
to:
accept ssl `catch` \(_::SomeException) -> return()
So it will create an valid NetworkSession even the socket connection has been terminated. Then the handler will raise another exception and the code in the bracket function will be executed to finally close the socket.
This is not the best solution in performance, because it has the handler read or write to the socket even it already knows the connection is terminated, and only close the socket when the second exception is raised.
But it's simple and it works.
Eric