@@ -47,7 +47,7 @@ foreign import CALLCONV unsafe "shutdown"
47
47
--
48
48
-- Since: 3.1.1.0
49
49
gracefulClose :: Socket -> Int -> IO ()
50
- gracefulClose s tmout = sendRecvFIN `E.finally` close s
50
+ gracefulClose s tmout0 = sendRecvFIN `E.finally` close s
51
51
where
52
52
sendRecvFIN = do
53
53
-- Sending TCP FIN.
@@ -57,21 +57,18 @@ gracefulClose s tmout = sendRecvFIN `E.finally` close s
57
57
Right () -> do
58
58
-- Waiting TCP FIN.
59
59
E. bracket (mallocBytes bufSize) free recvEOFloop
60
- -- milliseconds. Taken from BSD fast clock value.
61
- clock = 200
62
- recvEOFloop buf = loop 0
60
+ recvEOFloop buf = loop 1 0
63
61
where
64
- loop delay = do
62
+ loop delay tmout = do
65
63
-- We don't check the (positive) length.
66
64
-- In normal case, it's 0. That is, only FIN is received.
67
65
-- In error cases, data is available. But there is no
68
66
-- application which can read it. So, let's stop receiving
69
67
-- to prevent attacks.
70
68
r <- recvBufNoWait s buf bufSize
71
- let delay' = delay + clock
72
- when (r == - 1 && delay' < tmout) $ do
73
- threadDelay (clock * 1000 )
74
- loop delay'
69
+ when (r == - 1 && tmout < tmout0) $ do
70
+ threadDelay (delay * 1000 )
71
+ loop (delay * 2 ) (tmout + delay)
75
72
-- Don't use 4092 here. The GHC runtime takes the global lock
76
73
-- if the length is over 3276 bytes in 32bit or 3272 bytes in 64bit.
77
74
bufSize = 1024
0 commit comments