Skip to content

Commit e5bfd88

Browse files
committed
using a nested bracket for gracefulClose
to ensure that cancel functions are called.
1 parent 1fd5bf7 commit e5bfd88

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

Network/Socket/Shutdown.hs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,26 @@ recvEOFevent :: Socket -> Int -> Ptr Word8 -> IO ()
9393
recvEOFevent s tmout0 buf = do
9494
tmmgr <- Ev.getSystemTimerManager
9595
tvar <- newTVarIO False
96-
E.bracket (setup tmmgr tvar) teardown $ \(wait, _) -> do
97-
waitRes <- wait
98-
case waitRes of
99-
TimeoutTripped -> return ()
100-
-- We don't check the (positive) length.
101-
-- In normal case, it's 0. That is, only FIN is received.
102-
-- In error cases, data is available. But there is no
103-
-- application which can read it. So, let's stop receiving
104-
-- to prevent attacks.
105-
MoreData -> void $ recvBufNoWait s buf bufSize
96+
E.bracket (setupTimeout tmmgr tvar) (cancelTimeout tmmgr) $ \_ -> do
97+
E.bracket (setupRead s) cancelRead $ \(rxWait,_) -> do
98+
let toWait = readTVar tvar >>= check
99+
wait = atomically ((toWait >> return TimeoutTripped)
100+
<|> (rxWait >> return MoreData))
101+
waitRes <- wait
102+
case waitRes of
103+
TimeoutTripped -> return ()
104+
-- We don't check the (positive) length.
105+
-- In normal case, it's 0. That is, only FIN is received.
106+
-- In error cases, data is available. But there is no
107+
-- application which can read it. So, let's stop receiving
108+
-- to prevent attacks.
109+
MoreData -> void $ recvBufNoWait s buf bufSize
106110
where
107-
setup tmmgr tvar = do
108-
-- millisecond to microsecond
109-
key <- Ev.registerTimeout tmmgr (tmout0 * 1000) $
110-
atomically $ writeTVar tvar True
111-
(evWait, evCancel) <- waitAndCancelReadSocketSTM s
112-
let toWait = do
113-
tmout <- readTVar tvar
114-
check tmout
115-
toCancel = Ev.unregisterTimeout tmmgr key
116-
wait = atomically ((toWait >> return TimeoutTripped)
117-
<|> (evWait >> return MoreData))
118-
cancel = evCancel >> toCancel
119-
return (wait, cancel)
120-
teardown (_, cancel) = cancel
111+
-- millisecond to microsecond
112+
tmout = tmout0 * 1000
113+
setupTimeout tmmgr tvar =
114+
Ev.registerTimeout tmmgr tmout $ atomically $ writeTVar tvar True
115+
cancelTimeout = Ev.unregisterTimeout
116+
setupRead = waitAndCancelReadSocketSTM
117+
cancelRead (_,cancel) = cancel
121118
#endif

0 commit comments

Comments
 (0)