informatikr / hedis

A Redis client library for Haskell.
http://hackage.haskell.org/package/hedis
BSD 3-Clause "New" or "Revised" License
327 stars 121 forks source link

Leaking sockets when connect fails #143

Closed bflyblue closed 4 years ago

bflyblue commented 4 years ago

In the case where a sock is created but unable to connect, the socket is not closed and is leaked in the following line:

https://github.com/informatikr/hedis/blob/eb62c5d4b40822e5f9724918150f6105359dca24/src/Database/Redis/ProtocolPipelining.hs#L98

On my system the IPv6 address is tried first and fails before the IPv4 address connects, resulting in many un-closed sockets until the process runs out of file descriptors, as seen in lsof:

meerkat 5238 root   40u     sock                0,9       0t0 654985 protocol: TCPv6
meerkat 5238 root   41u     sock                0,9       0t0 661617 protocol: TCPv6
...
meerkat 5238 root  743u     sock                0,9       0t0 693816 protocol: TCPv6

As a quick test I changed line 98 to the following and can confirm it fixes my issue:

Left err -> NS.close sock >> return (Left err)
C37H40O9 commented 4 years ago

Looks like bracketOnError doesn't trigger because we use try inside. Anyway I made a PR

k-bx commented 4 years ago

Thanks! Uploaded as 0.12.13