Can't send from agent error handler?

17 views
Skip to first unread message

Shawn Hoover

unread,
Jun 23, 2010, 11:27:13 AM6/23/10
to clo...@googlegroups.com
My first thought for an agent error handler was to send the exception to another agent.

user=> (let [handler (agent nil)
            a (agent 42
                     :error-handler
                     (fn [_ ex] (send handler
                                      (fn [_] (println ex)))))]
          (send a (fn [_] (throw (Exception. "bad news"))))
          (await a)
          @a)
42
;; No println!
user=> 

Is this the way it should work? I saw some stuff about it in the wiki [1], but it's hard to tell what was decided explicitly.

Shawn


ka

unread,
Jun 24, 2010, 6:51:26 AM6/24/10
to Clojure
I'm also facing the same problem -

(let [handler (agent 50)
a (agent 42
:error-handler
(fn [_ ex]
(do
(println "Inside agent a error handler fn" (Thread/
currentThread))
(send handler
(fn [_] (do (println ex (Thread/currentThread)))
60))
(println "Inside agent a error handler fn - after
sending to the handler agent")
(await handler)
(println "CODE DOESN'T REACH HERE ??")
(println "State of handler agent:" @handler))))]
(send a (fn [_]
(println "Inside agent a function" (Thread/
currentThread))
(throw (Exception. "bad news"))))
(Thread/sleep 1000)
;(println "Errors with a:" (agent-error a))
;(println "Errors with handler:" (agent-error handler))
(shutdown-agents)
(println "Errors with a:" (agent-error a))
(println "Errors with handler:" (agent-error handler))
(println @a)
(println @handler))

- Thanks

Chouser

unread,
Jun 29, 2010, 10:07:37 AM6/29/10
to clo...@googlegroups.com
On Wed, Jun 23, 2010 at 11:27 AM, Shawn Hoover <shawn....@gmail.com> wrote:
> My first thought for an agent error handler was to send the exception to
> another agent.
> user=> (let [handler (agent nil)
>             a (agent 42
>                      :error-handler
>                      (fn [_ ex] (send handler
>                                       (fn [_] (println ex)))))]
>           (send a (fn [_] (throw (Exception. "bad news"))))
>           (await a)
>           @a)
> 42
> ;; No println!
> user=>
> Is this the way it should work? I saw some stuff about it in the wiki [1],
> but it's hard to tell what was decided explicitly.

Thanks for the report. Ticket is here:
http://www.assembla.com/spaces/clojure/tickets/390

Please try out the patch there and see if it works and/or causes
other problems. Thanks.

--Chouser
http://joyofclojure.com/

Chouser

unread,
Jun 29, 2010, 10:23:09 AM6/29/10
to clo...@googlegroups.com

With the patch in http://www.assembla.com/spaces/clojure/tickets/390
your example will get a little further, but it has another
problem. The error handler is still executed serially with agent
action, as if it was itself an agent action, but you may not use
'await' inside an agent action. Using await there throws an
exception, but since you're already in an error handler, that
exception is silently thrown away and you still won't see the
"CODE DOESN'T REACH HERE" message. If you wrap your await call
in its own exception handler, you'll be able to see that's what's
going on.

--Chouser
http://joyofclojure.com/

Reply all
Reply to author
Forward
0 new messages