Skip to content

Commit 10ab2cb

Browse files
committed
using timeout for recvEOFloop of gracefulClose
1 parent 868cc4c commit 10ab2cb

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

Network/Socket/Shutdown.hs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ module Network.Socket.Shutdown (
88
, gracefulClose
99
) where
1010

11-
import Control.Concurrent (threadDelay, yield)
11+
import Control.Concurrent (yield)
1212
import qualified Control.Exception as E
1313
import Foreign.Marshal.Alloc (mallocBytes, free)
14+
import System.Timeout
1415

1516
#if !defined(mingw32_HOST_OS)
1617
import Control.Concurrent.STM
@@ -83,18 +84,7 @@ bufSize :: Int
8384
bufSize = 1024
8485

8586
recvEOFloop :: Socket -> Int -> Ptr Word8 -> IO ()
86-
recvEOFloop s tmout0 buf = loop 1 0
87-
where
88-
loop delay tmout = do
89-
-- We don't check the (positive) length.
90-
-- In normal case, it's 0. That is, only FIN is received.
91-
-- In error cases, data is available. But there is no
92-
-- application which can read it. So, let's stop receiving
93-
-- to prevent attacks.
94-
r <- recvBufNoWait s buf bufSize
95-
when (r == -1 && tmout < tmout0) $ do
96-
threadDelay (delay * 1000)
97-
loop (delay * 2) (tmout + delay)
87+
recvEOFloop s tmout0 buf = void $ timeout tmout0 $ recvBuf s buf bufSize
9888

9989
#if !defined(mingw32_HOST_OS)
10090
data Wait = MoreData | TimeoutTripped

0 commit comments

Comments
 (0)