@@ -372,7 +372,7 @@ data ValidRemoteEndPointState = ValidRemoteEndPointState
372372 { _remoteOutgoing :: !Int
373373 , _remoteIncoming :: !IntSet
374374 , remoteSocket :: !N.Socket
375− , sendOn :: [ByteString] -> IO ()
375+ , remoteSendLock :: !(MVar ())
376376 , _pendingCtrlRequests :: !(IntMap (MVar (Either IOException [ByteString])))
377377 , _nextCtrlRequestId :: !ControlRequestId
378378 }
@@ -727,11 +727,12 @@ handleConnectionRequest transport sock = handle handleException $ do
727727 tryCloseSocket sock
728728 return Nothing
729729 else do
730+ sendLock <- newMVar ()
730731 let vst = ValidRemoteEndPointState
731732 { remoteSocket = sock
733+ , remoteSendLock = sendLock
732734 , _remoteOutgoing = 0
733735 , _remoteIncoming = IntSet.empty
734− , sendOn = sendMany sock
735736 , _pendingCtrlRequests = IntMap.empty
736737 , _nextCtrlRequestId = 0
737738 }
@@ -1039,11 +1040,12 @@ setupRemoteEndPoint params (ourEndPoint, theirEndPoint) hints = do
10391040 (connectTimeout hints)
10401041 didAccept <- case result of
10411042 Right (sock, ConnectionRequestAccepted) -> do
1043+ sendLock <- newMVar ()
10421044 let vst = ValidRemoteEndPointState
10431045 { remoteSocket = sock
1046+ , remoteSendLock = sendLock
10441047 , _remoteOutgoing = 0
10451048 , _remoteIncoming = IntSet.empty
1046− , sendOn = sendMany sock
10471049 , _pendingCtrlRequests = IntMap.empty
10481050 , _nextCtrlRequestId = 0
10491051 }
@@ -1386,6 +1388,11 @@ findRemoteEndPoint ourEndPoint theirAddress findOrigin = go
13861388 ourState = localState ourEndPoint
13871389 ourAddress = localAddress ourEndPoint
13881390
1391+-- | Send a payload over a heavyweight connection (thread safe)
1392+sendOn :: ValidRemoteEndPointState -> [ByteString] -> IO ()
1393+sendOn vst bs = withMVar (remoteSendLock vst) $ \() ->
1394+ sendMany (remoteSocket vst) bs
1395+
13891396--------------------------------------------------------------------------------
13901397-- Scheduling actions --
13911398--------------------------------------------------------------------------------