8000 Safely creating socket · postgres-haskell/postgres-wire@1bb7f44 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bb7f44

Browse files
Safely creating socket
1 parent fa38fed commit 1bb7f44

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/Database/PostgreSQL/Driver/Connection.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ receiverThread
207207
-> NotificationHandler
208208
-> IO ()
209209
receiverThread msgFilter rawConn dataChan allChan modeRef ntfHandler =
210-
receiveLoop []
210+
receiveLoop []
211211
where
212212
receiveLoop :: [V.Vector (Maybe B.ByteString)] -> IO ()
213213
receiveLoop acc = do

src/Database/PostgreSQL/Driver/RawConnection.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module Database.PostgreSQL.Driver.RawConnection where
33

44
import Control.Monad (void)
5+
import Control.Exception (bracketOnError)
56
import Safe (headMay)
67
import Data.Monoid ((<>))
78
import System.Socket (socket, AddressInfo(..), getAddressInfo, socketAddress,
@@ -38,6 +39,12 @@ createRawConnection settings
3839
| "/" `B.isPrefixOf` host = unixConnection host
3940
| otherwise = tcpConnection
4041
where
42+
createAndConnect Nothing creating = throwAuthErrorInIO AuthInvalidAddress
43+
createAndConnect (Just address) creating =
44+
bracketOnError creating close $ \s -> do
45+
connect s address
46+
pure . Right $ constructRawConnection s
47+
4148
unixConnection dirPath = do
4249
let mAddress = socketAddressUnixPath $ makeUnixPath dirPath
4350
createAndConnect mAddress (socket :: IO (Socket Unix Stream Unix))
@@ -48,12 +55,6 @@ createRawConnection settings
4855
:: IO [AddressInfo Inet Stream TCP])
4956
createAndConnect mAddress (socket :: IO (Socket Inet Stream TCP))
5057

51-
createAndConnect Nothing creating = throwAuthErrorInIO AuthInvalidAddress
52-
createAndConnect (Just address) creating = do
53-
s <- creating
54-
connect s address
55-
pure . Right $ constructRawConnection s
56-
5758
portStr = BS.pack . show $ settingsPort settings
5859
host = settingsHost settings
5960
makeUnixPath dirPath =

0 commit comments

Comments
 (0)
0