@@ -91,6 +91,12 @@ import Control.Distributed.Process.Internal.StrictMVar
9191 , putMVar
9292 , takeMVar
9393 )
94+import Control.Distributed.Process.Internal.ThreadPool
95+ ( newThreadPool
96+ , submitTask
97+ , lookupWorker
98+ , ThreadPool
99+ )
94100import Control.Concurrent.Chan (newChan, writeChan, readChan)
95101import qualified Control.Concurrent.MVar as MVar (newEmptyMVar, takeMVar)
96102import Control.Concurrent.STM
@@ -264,12 +270,14 @@ createBareLocalNode endPoint rtable = do
264270 , _localConnections = Map.empty
265271 }
266272 ctrlChan <- newChan
273+ sendPool <- newThreadPool
267274 let node = LocalNode { localNodeId = NodeId $ NT.address endPoint
268275 , localEndPoint = endPoint
269276 , localState = state
270277 , localCtrlChan = ctrlChan
271278 , localEventBus = MxEventBusInitialising
272279 , remoteTable = rtable
280+ , localSendPool = sendPool
273281 }
274282 tracedNode <- startMxAgent node
275283
@@ -613,6 +621,8 @@ handleIncomingMessages node = go initConnectionState
613621 , ctrlMsgSignal = Died nid DiedDisconnect
614622 }
615623 let notLost k = not (k `Set.member` (st ^. incomingFrom theirAddr))
624+ liftIO $ lookupWorker (localSendPool node) (NodeId theirAddr)
625+ >>= maybe (return ()) killThread
616626 closeImplicitReconnections node nid
617627 go ( (incomingFrom theirAddr ^= Set.empty)
618628 . (incoming ^: Map.filterWithKey (const . notLost))
@@ -669,6 +679,12 @@ data NCState = NCState
669679 , _registeredOnNodes :: !(Map ProcessId [(NodeId,Int)])
670680 }
671681
682+submitSendPool :: LocalNode -> NodeId -> IO () -> IO ()
683+submitSendPool node nid task = submitTask (localSendPool node) fork nid task
684+ where
685+ fork io =
686+ forkIO $ io `Exception.catch` \(NodeClosedException _) -> return ()
687+
672688newtype NC a = NC { unNC :: StateT NCState (ReaderT LocalNode IO) a }
673689 deriving ( Applicative
674690 , Functor
@@ -704,7 +720,7 @@ ncSendToProcessAndTrace shouldTrace pid msg = do
704720 node <- ask
705721 if processNodeId pid == localNodeId node
706722 then ncEffectLocalSendAndTrace shouldTrace node pid msg
707− else liftIO $ sendBinary node
723+ else liftIO $ submitSendPool node (processNodeId pid) $ sendBinary node
708724 (NodeIdentifier $ localNodeId node)
709725 (NodeIdentifier $ processNodeId pid)
710726 WithImplicitReconnect
@@ -717,7 +733,7 @@ ncSendToNode to msg = do
717733 node <- ask
718734 liftIO $ if to == localNodeId node
719735 then writeChan (localCtrlChan node) $! msg
720− else sendBinary node
736+ else submitSendPool node to $ sendBinary node
721737 (NodeIdentifier $ localNodeId node)
722738 (NodeIdentifier to)
723739 WithImplicitReconnect