git.iko.soy/distributed-process
on master help sign in
Fix potential deadlock
commit74293ef1b72e8dbcb7f8693ee3739cabb6ecb558 authorEdsko de Vries <edsko@edsko.net> dateSat 12 May 2012, 18:38 (14 years ago) parent9679355
2 files changed+18−11
network-transport-2/src/Network/Transport/TCP.hs+14−11 network-transport-2/tests/TestTransport.hs+4
network-transport-2/src/Network/Transport/TCP.hs+14−11
@@ -406,18 +406,21 @@ apiCloseEndPoint transport ourEndPoint = do
406406 -- Close the remote socket and return the set of all incoming connections
407407 tryCloseRemoteSocket :: RemoteEndPoint -> IO IntSet
408408 tryCloseRemoteSocket theirEndPoint = do
409− -- Like above, we take but don't put back
410− st <- takeMVar (remoteState theirEndPoint)
411409 -- We make an attempt to close the connection nicely (by sending a CloseSocket first)
412− case st of
413− RemoteEndPointInvalid _ -> do return IntSet.empty
414− RemoteEndPointValid conn -> do tryIO $ do
415− sendOn conn [encodeInt32 CloseSocket]
416− N.sClose (remoteSocket conn)
417− return (conn ^. remoteIncoming)
418− RemoteEndPointClosing _ conn -> do tryIO $ N.sClose (remoteSocket conn)
419− return IntSet.empty
420− RemoteEndPointClosed -> do return IntSet.empty
410+ modifyMVar (remoteState theirEndPoint) $ \st ->
411+ case st of
412+ RemoteEndPointInvalid _ ->
413+ return (st, IntSet.empty)
414+ RemoteEndPointValid conn -> do
415+ tryIO $ do
416+ sendOn conn [encodeInt32 CloseSocket]
417+ N.sClose (remoteSocket conn)
418+ return (RemoteEndPointClosed, conn ^. remoteIncoming)
419+ RemoteEndPointClosing _ conn -> do
420+ tryIO $ N.sClose (remoteSocket conn)
421+ return (RemoteEndPointClosed, IntSet.empty)
422+ RemoteEndPointClosed ->
423+ return (RemoteEndPointClosed, IntSet.empty)
421424
422425-- | Special case of 'apiConnect': connect an endpoint to itself
423426connectToSelf :: LocalEndPoint -> IO (Either (FailedWith ConnectErrorCode) Connection)
network-transport-2/tests/TestTransport.hs+4
@@ -582,6 +582,10 @@ testCloseEndPoint transport _ = do
582582 -- Close the endpoint
583583 closeEndPoint endpoint
584584 ErrorEvent (ErrorEventEndPointClosed [cid'']) <- receive endpoint; True <- return $ cid == cid''
585+
586+ -- Attempt to send should fail with connection closed
587+ Left (FailedWith SendConnectionClosed _) <- send conn ["ping2"]
588+
585589 return ()
586590
587591 putMVar clientDone ()