Reported by cemer...@snowtide.com, Jun 01, 2009
There has been intermittent chatter over the past months from a couple of
people on the group (e.g.
http:and in #clojure about some clojure scripts hanging, either for a constant
time (usually reported as a minute or so with no CPU util) or seemingly
forever (or until someone kills the process).
I just hit a similar situation in our compilation process, which invokes
clojure.lang.Compile from ant. The build process for this particular
project had taken 15 second or so, but after adding a couple of pmap calls,
that build time jumped to ~1:15, with roughly zero CPU utilization over the
course of that last minute.
Adding a call to Agent.shutdown() in the finally block in
clojure.lang.Compile/main resolved the problem; a patch including this
change is attached. I wouldn't suspect anyone would have any issues with
such a change.
In general, it doesn't seem like everyone should keep tripping over this
problem in different directions. It's a very difficult thing to debug if
you're not attuned to how clojure's concurrency primitives work under the
hood, and I would bet that newer users would be particularly affected.
After discussion in #clojure, rhickey suggested adding a
*auto-shutdown-agents* var, which:
- if true when exiting one of the main entry points (clojure.main, or the
legacy script/repl entry points), Agent.shutdown() would be called,
allowing for the clean exit of the application
- would be bound by default to true
- could be easily set to false for anyone with an advanced use-case that
requires agents to remain active after the main thread of the application
This would obviously not help anyone initializing clojure from a different
entry point, but this may represent the best compromise between
least-surprise and maximal functionality for advanced users.
In addition to the above, it perhaps might be worthwhile to change the
keepalive values used to create the Threadpools used by c.l.Actor's
Executors. Currently, Actor uses a default thread pool executor, which
results in a 60s keepalive. Lowering this to something much smaller (1s?
5s?) would additionally minimize the impact of Agent's threadpools on Java
applications that embed clojure directly (and would therefore not benefit
from *auto-shutdown-agents* as currently conceived, leading to puzzling
'hanging' behaviour). I'm not in a position to determine what impact this
would have on performance due to thread churn, but it would at least
minimize what would be perceived as undesirable behaviour by users that are
less familiar with the implementation details of Agent and code that
depends on it.
Comment 1 by cemer...@snowtide.com, Jun 01, 2009
Just FYI, I'd be happy to provide patches for either of the suggestions mentioned