git.iko.soy/distributed-process
on master help sign in
Merge pull request #183 from tweag/call-monitor-race

Fix monitor race in 'call'.

commitf5f67eadba01eef6b6d941ebbf75f833f5bb9bda authorAlexander Vershilov <alexander.vershilov@gmail.com> dateTue 16 Jun 2015, 19:18 (11 years ago) parent39ed0826ca74ea
1 file changed+9−5
src/Control/Distributed/Process/Internal/Spawn.hs+9−5
src/Control/Distributed/Process/Internal/Spawn.hs+9−5
@@ -38,6 +38,8 @@ import Control.Distributed.Process.Internal.Closure.BuiltIn
3838 , cpSend
3939 , cpNewChan
4040 , cpDelayed
41+ , returnCP
42+ , sdictUnit
4143 )
4244import Control.Distributed.Process.Internal.Primitives
4345 ( -- Basic messaging
@@ -109,12 +111,14 @@ call :: Serializable a
109111 -> Process a
110112call dict nid proc = do
111113 us <- getSelfPid
112− (pid, mRef) <- spawnMonitor nid (proc `bindCP` cpSend dict us)
113− -- We are guaranteed to receive the reply before the monitor notification
114− -- (if a reply is sent at all)
115− -- NOTE: This might not be true if we switch to unreliable delivery.
114+ (pid, mRef) <- spawnMonitor nid (proc `bindCP`
115+ cpSend dict us `seqCP`
116+ -- Delay so the process does not terminate
117+ -- before the response arrives.
118+ cpDelayed us (returnCP sdictUnit ())
119+ )
116120 mResult <- receiveWait
117− [ match (return . Right)
121+ [ match $ \a -> send pid () >> return (Right a)
118122 , matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
119123 (\(ProcessMonitorNotification _ _ reason) -> return (Left reason))
120124 ]