Skip to content

Commit 21b3145

Browse files
arybczakkazu-yamamoto
authored andcommitted
Fix memory leak in getaddrinfo and make it async exception safe
haskell#587 removed the call to `c_freeaddrinfo` in `getaddrinfo`. This restores it and in addition makes the function async exception safe.
1 parent 25b6814 commit 21b3145

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Network/Socket/Info.hsc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module Network.Socket.Info where
99

10+
import Control.Exception (mask, onException)
1011
import Data.List.NonEmpty (NonEmpty(..))
1112
import qualified Data.List.NonEmpty as NE
1213
import Foreign.Marshal.Alloc (alloca, allocaBytes)
@@ -274,11 +275,12 @@ getAddrInfoNE hints node service = alloc getaddrinfo
274275
maybeWith with filteredHints $ \c_hints ->
275276
alloca $ \ptr_ptr_addrs ->
276277
body c_node c_service c_hints ptr_ptr_addrs
277-
getaddrinfo c_node c_service c_hints ptr_ptr_addrs = do
278+
getaddrinfo c_node c_service c_hints ptr_ptr_addrs = mask $ \release -> do
278279
ret <- c_getaddrinfo c_node c_service c_hints ptr_ptr_addrs
279280
if ret == 0 then do
280281
ptr_addrs <- peek ptr_ptr_addrs
281-
ais <- followAddrInfo ptr_addrs
282+
ais <- release (followAddrInfo ptr_addrs) `onException` c_freeaddrinfo ptr_addrs
283+
c_freeaddrinfo ptr_addrs
282284
return ais
283285
else do
284286
err <- gai_strerror ret

0 commit comments

Comments
 (0)