On Jan 30, 2014, at 8:22 PM, Michael Blume <blume.m...@gmail.com> wrote:

> Recently we had an app fail because, as it was starting up, one thread was 
> trying to require a clojure namespace, and another was trying to use a 
> PersistentHashSet. Somehow these two threads wound up in deadlock. I've 
> reproduced the problem with minimal code here: 
> 
> https://github.com/MichaelBlume/deadlock
> 
> I've reproduced this behavior with clojure 1.4.0, 1.5.1, and 1.6.0-alpha3 
> (though the attached project uses 1.5.1, it should be trivial to change its 
> dependency)
> 
> Is this expected behavior? Is the take-away don't use clojure data structures 
> from java code? Make sure the clojure runtime is warm before using clojure 
> data structures?

The deadlock is happening while the RT class's initializer is running. One way 
to avoid the deadlock that should be completely reliable is to arrange for RT 
to be initialized during the initialization of your main class. Referencing RT 
in the initializer for a static field will accomplish that.

Clojure's main class does this and avoids this deadlock. 
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/main.java#L20

Here's a program similar to the one you posted that runs properly.

package deadlock;

import clojure.lang.PersistentHashSet;
import clojure.lang.RT;
import clojure.lang.Var;

public class DeadLockMain {
    final static Var P = RT.var("clojure.core", "println");     // RT will be 
initialized before main starts
    public static void main(String[] args) {
        new Thread(new Runnable() {
                public void run() {
                    P.invoke(PersistentHashSet.EMPTY.cons(Integer.valueOf(6)));
                }
            }).start();
        P.invoke(PersistentHashSet.EMPTY.cons(Integer.valueOf(5)));
    }
}

--Steve

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to