jcmincke
jcmincke

Reputation: 41

Clojure: Agent calling Agent: suspicion of deadlock?

Consider the following code:

(let 
  [slave-agent (agent 0)
   run-slave (fn [_] (println "running slave agent"))
   run-master (fn [_]
            (loop []
              (println "sending to slave agent")
              (send-off slave-agent run-slave)
              (Thread/sleep 1000)
              (recur)
              )
            )
   master-agent (agent nil)
   ]
   (send-off master-agent run-master)  
  )

Using any combination of send/send-off, I could not have the slave-agent running. However when I run run-master in a classical java.lang.Thread, everything works fine.

Has anyone an idea.

Thank you

Regards

J-C

Upvotes: 3

Views: 211

Answers (1)

kotarak
kotarak

Reputation: 17299

send in a agent action (or transaction) are held till the action (or transaction) completted (and committed). However your run-master never returns. So you just accumulate send-off requests which are actually never submitted for execution. Try something like this:

(defn run-master
  [_]
  (send-off slave-agent run-slave)
  (send-off *agent* run-master)
  (Thread/sleep 1000))

Upvotes: 2

Related Questions