Skip to content

Commit

Permalink
Fix memory leak in getaddrinfo and make it async exception safe
Browse files Browse the repository at this point in the history
haskell#587 removed the call to `c_freeaddrinfo`
in `getaddrinfo`.

This restores it and in addition makes the function async exception safe.
  • Loading branch information
arybczak authored and kazu-yamamoto committed Nov 27, 2024
1 parent 25b6814 commit 21b3145
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Network/Socket/Info.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

module Network.Socket.Info where

import Control.Exception (mask, onException)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Foreign.Marshal.Alloc (alloca, allocaBytes)
Expand Down Expand Up @@ -274,11 +275,12 @@ getAddrInfoNE hints node service = alloc getaddrinfo
maybeWith with filteredHints $ \c_hints ->
alloca $ \ptr_ptr_addrs ->
body c_node c_service c_hints ptr_ptr_addrs
getaddrinfo c_node c_service c_hints ptr_ptr_addrs = do
getaddrinfo c_node c_service c_hints ptr_ptr_addrs = mask $ \release -> do
ret <- c_getaddrinfo c_node c_service c_hints ptr_ptr_addrs
if ret == 0 then do
ptr_addrs <- peek ptr_ptr_addrs
ais <- followAddrInfo ptr_addrs
ais <- release (followAddrInfo ptr_addrs) `onException` c_freeaddrinfo ptr_addrs
c_freeaddrinfo ptr_addrs
return ais
else do
err <- gai_strerror ret
Expand Down

0 comments on commit 21b3145

Please sign in to comment.