network 3.2.14.0 → 3.2.15.0
raw patch · 6 files changed
+71/−3 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +5/−0
- Network/Socket/Syscall.hs +28/−2
- Network/Socket/Wait.hs +11/−0
- network.cabal +1/−1
- tests/Network/SocketSpec.hs +24/−0
- tests/Network/Test/Common.hs +2/−0
CHANGELOG.md view
@@ -1,5 +1,10 @@ # CHANGELOG for network +## Version 3.2.15.0++* acceptWith: keep to the give-up when another waiter takes the connection+ [#663](https://github.com/haskell/network/pull/663)+ ## Version 3.2.14.0 * dontfrag for WINE
Network/Socket/Syscall.hs view
@@ -11,12 +11,11 @@ #if defined(mingw32_HOST_OS) import Control.Exception (bracket)-import Foreign (FunPtr) import Foreign.Marshal.Alloc (alloca) import GHC.Conc (asyncDoProc) import Network.Socket.Cbits #else-import Foreign.C.Error (getErrno, eINTR, eINPROGRESS)+import Foreign.C.Error (getErrno, eAGAIN, eINTR, eINPROGRESS, eWOULDBLOCK) import GHC.Conc (threadWaitWrite) #endif @@ -277,6 +276,33 @@ setCloseOnExecIfNeeded new_fd return new_fd # endif /* HAVE_ADVANCED_SOCKET_FLAGS */+#endif++#if !defined(mingw32_HOST_OS)+-- | 'accept' which does not wait: 'Nothing' if no connection is queued.+acceptNoWait :: SocketAddress sa => Socket -> IO (Maybe (Socket, sa))+acceptNoWait listing_sock = withNewSocketAddress $ \new_sa sz ->+ withFdSocket listing_sock $ \listing_fd -> do+ r <- E.bracketOnError (with (fromIntegral sz) $ tryAccept listing_fd new_sa)+ (mapM_ c_close) $ mapM $ \new_fd -> do+# ifndef HAVE_ADVANCED_SOCKET_FLAGS+ setNonBlockIfNeeded new_fd+ setCloseOnExecIfNeeded new_fd+# endif+ mkSocket new_fd+ forM r $ \new_sock -> (,) new_sock <$> peekSocketAddress new_sa+ where+ tryAccept fd sa ptr_len = do+# ifdef HAVE_ADVANCED_SOCKET_FLAGS+ new_fd <- c_accept4 fd sa ptr_len (sockNonBlock .|. sockCloexec)+# else+ new_fd <- c_accept fd sa ptr_len+# endif+ if new_fd /= -1 then return (Just new_fd) else do+ err <- getErrno+ if err == eINTR then tryAccept fd sa ptr_len+ else if err == eAGAIN || err == eWOULDBLOCK then return Nothing+ else throwSocketError "Network.Socket.accept" #endif foreign import CALLCONV unsafe "socket"
Network/Socket/Wait.hs view
@@ -198,7 +198,18 @@ -- -- @since 3.2.10.0 acceptWith :: SocketAddress sa => Socket -> STM a -> IO (Either a (Socket, sa))+#if defined(mingw32_HOST_OS) acceptWith s giveUp = withReadable s giveUp $ accept s+#else+acceptWith s giveUp = loop+ where+ -- Another waiter may take the connection first: then wait again,+ -- still under the give-up, rather than block in accept.+ loop = withReadable s giveUp (acceptNoWait s) >>= \r -> case r of+ Left a -> return $ Left a+ Right (Just c) -> return $ Right c+ Right Nothing -> loop+#endif -- | 'recvBuf' which the caller can give up on. --
network.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.18 name: network-version: 3.2.14.0+version: 3.2.15.0 license: BSD3 license-file: LICENSE maintainer: Kazu Yamamoto, Tamar Christina
tests/Network/SocketSpec.hs view
@@ -22,7 +22,9 @@ import GHC.IO.Exception (IOErrorType (NoSuchThing, ResourceVanished)) import System.IO.Error (ioeGetErrorString, ioeGetErrorType, tryIOError) import System.IO.Temp (withSystemTempDirectory)+#if !defined(mingw32_HOST_OS) import System.Posix.Types (Fd(..))+#endif import Foreign.C.Types () import System.Timeout (timeout) @@ -478,6 +480,26 @@ connect cli $ SockAddrInet port $ tupleToHostAddress (127, 0, 0, 1) bracket (fst <$> accept sock) close $ \_ -> return () + -- One connection wakes both waiters; the one which finds it taken+ -- must still keep to its give-up rather than wait for the next.+ it "keeps to the give-up of an accept which another thread beat" $+ withListener $ \sock port -> do+ stops <- replicateM 2 $ newTVarIO False+ done <- newEmptyMVar+ forM_ (zip [0 :: Int ..] stops) $ \(i, stop) -> forkIO $ do+ r <- acceptWith sock (check =<< readTVar stop)+ case r :: Either () (Socket, SockAddr) of+ Left () -> putMVar done (i, False)+ Right (s, _) -> close s >> putMVar done (i, True)+ threadDelay 100000+ bracket (socket AF_INET Stream defaultProtocol) close $ \cli -> do+ connect cli $ SockAddrInet port $ tupleToHostAddress (127, 0, 0, 1)+ (winner, True) <- takeMVar done+ let loser = 1 - winner+ threadDelay 100000+ atomically $ writeTVar (stops !! loser) True+ timeout 1000000 (takeMVar done) `shouldReturn` Just (loser, False)+ it "tells a socket closed under an accept from other errors" $ do sock <- socket AF_INET Stream defaultProtocol bind sock $ SockAddrInet 0 $ tupleToHostAddress (127, 0, 0, 1)@@ -660,8 +682,10 @@ cmsgidGen :: Gen CmsgId cmsgidGen = biasedGen (\g -> CmsgId <$> g <*> g) cmsgidPatterns arbitrary +#if !defined(mingw32_HOST_OS) genFds :: Gen [Fd] genFds = listOf (Fd <$> arbitrary)+#endif -- pruned lists of pattern synonym values for each type to generate values from
tests/Network/Test/Common.hs view
@@ -38,7 +38,9 @@ import Network.Socket import System.Directory import System.Timeout (timeout)+#if !defined(mingw32_HOST_OS) import Test.Hspec+#endif serverAddr :: String serverAddr = "127.0.0.1"