Skip to content

Commit 3ccef81

Browse files
Merge pull request #43 from haskell-distributed/fd/close-remote-endpoints
Discard remote endpoints when they close or fail.
2 parents 26a3e1e + 5027a25 commit 3ccef81

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/Network/Transport/TCP.hs

+26-3
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ data ControlHeader =
449449
| CloseConnection
450450
-- | Request to close the connection (see module description)
451451
| CloseSocket
452+
-- | Sent by an endpoint when it is closed.
453+
| CloseEndPoint
452454
-- | Message sent to probe a socket
453455
| ProbeSocket
454456
-- | Acknowledgement of the ProbeSocket message
@@ -747,9 +749,7 @@ apiCloseEndPoint transport evs ourEndPoint =
747749
return closed
748750
RemoteEndPointValid vst -> do
749751
sched theirEndPoint $ do
750-
void $ tryIO $ sendOn vst [ encodeInt32 CloseSocket
751-
, encodeInt32 (vst ^. remoteMaxIncoming)
752-
]
752+
void $ tryIO $ sendOn vst [ encodeInt32 CloseEndPoint ]
753753
-- Release probing resources if probing.
754754
forM_ (remoteProbing vst) id
755755
tryCloseSocket (remoteSocket vst)
@@ -929,6 +929,29 @@ handleIncomingMessages (ourEndPoint, theirEndPoint) = do
929929
Just CloseSocket -> do
930930
didClose <- recvInt32 sock >>= closeSocket sock
931931
unless didClose $ go sock
932+
Just CloseEndPoint -> do
933+
let closeRemoteEndPoint vst = do
934+
forM_ (remoteProbing vst) id
935+
-- close incoming connections
936+
forM_ (Set.elems $ vst ^. remoteIncoming) $
937+
writeChan ourChannel . ConnectionClosed . connId
938+
-- report the endpoint as gone if we have any outgoing
939+
-- connections
940+
when (vst ^. remoteOutgoing > 0) $ do
941+
let code = EventConnectionLost (remoteAddress theirEndPoint)
942+
writeChan ourChannel . ErrorEvent $
943+
TransportError code "The remote endpoint was closed."
944+
removeRemoteEndPoint (ourEndPoint, theirEndPoint)
945+
modifyMVar_ theirState $ \s -> case s of
946+
RemoteEndPointValid vst -> do
947+
closeRemoteEndPoint vst
948+
return RemoteEndPointClosed
949+
RemoteEndPointClosing resolved vst -> do
950+
closeRemoteEndPoint vst
951+
putMVar resolved ()
952+
return RemoteEndPointClosed
953+
_ -> return s
954+
tryCloseSocket sock
932955
Just ProbeSocket -> do
933956
forkIO $ sendMany sock [encodeInt32 ProbeSocketAck]
934957
go sock

0 commit comments

Comments
 (0)