On Tuesday 16 December 2008 06:02, MikeM wrote:
> You can try this:
>
> (let [t (new Thread (fn[] (shutdown-agents)))]
>    (.. java.lang.Runtime (getRuntime) (addShutdownHook t)))
>
> It works for me, but seems to take a long time to complete the
> shutdown.

I tried adding this to the module that starts the agents in question 
(the question of the proper place for such code being left aside for 
now):

(def *shutdown-hook* (atom nil))

(when-not @*shutdown-hook*
 (let [shutdown-thread (new Thread shutdown-agents)]
  (swap! *shutdown-hook* (fn [old] shutdown-thread))
  (when (identical? @*shutdown-hook* shutdown-thread)
   (.. Runtime (getRuntime) (addShutdownHook shutdown-thread))))
)

I think this is doing what I think it's doing (reliably registering only 
one agent shutdown handler Thread), but exiting the REPL via CTRL-D 
still hangs. I can't say it hangs literally forever, obviously, but for 
longer than I'm willing to wait, certainly. So then it occurred to me 
to try calling (System/exit 0), and that exits immediately. So then I 
took out the code to set up the shutdown handler and found the 
(System/exit 0) _still_ effects an immediate exit from the REPL.

Lastly, if I just execute Mike's code directly:

 (let [shutdown-thread (new Thread shutdown-agents)]
  (.. Runtime (getRuntime) (addShutdownHook shutdown-thread)))

the hang upon CTRL-D / EOF at the REPL still happens.


So I am still confused about the right way to deal with agents in order 
to get reliable and prompt exit from the REPL and why an  EOF from the 
terminal hangs (only if agents were started and regardless of whether a 
shutdown hook is in place) but (System/exit 0) does not hang (again, 
regardless of whether agents were started and whether a shutdown hook 
is in place).

What am I missing?


> Your question made me wonder if the agent thread pools should use
> daemon threads - then this wouldn't be an issue.

Yes. That seems plausible, but I don't know enough about the possible 
subtleties to say for sure.


Randall Schulz

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to