@@ -97,7 +97,7 @@ import Data.Foldable (forM_)
9797import Data.Typeable (Typeable)
9898import Control.Applicative ((<$>))
9999import Control.Exception (throw)
100−import Control.Monad (forever, forM)
100+import Control.Monad (forever, forM, replicateM)
101101import Control.Monad.IO.Class (liftIO)
102102import Control.Concurrent (forkIO, threadDelay, ThreadId)
103103import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar_)
@@ -114,8 +114,12 @@ import Control.Distributed.Process
114114 , expect
115115 , nsendRemote
116116 , receiveWait
117+ , match
117118 , matchIf
118119 , processNodeId
120+ , monitorNode
121+ , unmonitor
122+ , NodeMonitorNotification(..)
119123 )
120124import qualified Control.Distributed.Process.Node as Node
121125 ( LocalNode
@@ -286,12 +290,24 @@ findSlaves :: Backend -> Process [NodeId]
286290findSlaves backend = do
287291 nodes <- liftIO $ findPeers backend 1000000
288292 -- Fire of asynchronous requests for the slave controller
289− forM_ nodes $ \nid -> whereisRemoteAsync nid "slaveController"
293+ refs <- forM nodes $ \nid -> do
294+ whereisRemoteAsync nid "slaveController"
295+ ref <- monitorNode nid
296+ return (nid, ref)
290297 -- Wait for the replies
291− catMaybes <$> forM nodes (\_ ->
298+ catMaybes <$> replicateM (length nodes) (
292299 receiveWait
293300 [ matchIf (\(WhereIsReply label _) -> label == "slaveController")
294− (\(WhereIsReply _ mPid) -> return (processNodeId <$> mPid))
301+ (\(WhereIsReply _ mPid) ->
302+ case mPid of
303+ Nothing ->
304+ return Nothing
305+ Just pid -> do
306+ let nid = processNodeId pid
307+ Just ref = lookup nid refs
308+ unmonitor ref
309+ return (Just nid))
310+ , match (\(NodeMonitorNotification {}) -> return Nothing)
295311 ])
296312
297313-- | Terminate all slaves