Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

As a part of Apache Storm we have some code that can load a clojure function from java using the following code.

`

public static IFn loadClojureFn(String namespace, String name) {
    try {
        clojure.lang.Compiler.eval(RT.readString("(require '" + namespace + ")"));
    } catch (Exception e) {
        //if playing from the repl and defining functions, file won't exist
    }
    return (IFn) RT.var(namespace, name).deref();
}

`

If this function is called from multiple different threads at the same time, trying to import the same namespace, I will occasionally get some very odd errors. NOTE: I had to modify the catch to actually print out the error message it was getting (We should not be eating exceptions either way).

{verbatim}
2016-01-07 16:26:09.305 b.s.u.Utils (link: WARN) Loading namespace failed
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: sentence-spout in this context, compiling:(storm/starter/clj/word_count.clj:21:1)

at clojure.lang.Compiler.analyze(Compiler.java:6543) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6485) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3791) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6725) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6524) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6485) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.eval(Compiler.java:6786) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.load(Compiler.java:7227) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.loadResourceScript(RT.java:371) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.loadResourceScript(RT.java:362) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.load(RT.java:446) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RT.load(RT.java:412) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load$fn__5448.invoke(core.clj:5866) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load.doInvoke(core.clj:5865) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.invoke(RestFn.java:408) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_one.invoke(core.clj:5671) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_lib$fn__5397.invoke(core.clj:5711) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_lib.doInvoke(core.clj:5710) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.applyTo(RestFn.java:142) ~(link: clojure-1.7.0.jar:?)
at clojure.core$apply.invoke(core.clj:632) ~(link: clojure-1.7.0.jar:?)
at clojure.core$load_libs.doInvoke(core.clj:5749) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.applyTo(RestFn.java:137) ~(link: clojure-1.7.0.jar:?)
at clojure.core$apply.invoke(core.clj:632) ~(link: clojure-1.7.0.jar:?)
at clojure.core$require.doInvoke(core.clj:5832) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.RestFn.invoke(RestFn.java:408) ~(link: clojure-1.7.0.jar:?)
at clojure.core$eval114.invoke(NO_SOURCE_FILE:0) ~(link: ?:?)
at clojure.lang.Compiler.eval(Compiler.java:6782) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.eval(Compiler.java:6745) ~(link: clojure-1.7.0.jar:?)
at backtype.storm.utils.Utils.loadClojureFn(Utils.java:602) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.clojure.ClojureBolt.prepare(ClojureBolt.java:57) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.daemon.executor$fn__8297$fn__8310.invoke(executor.clj:785) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at backtype.storm.util$async_loop$fn__556.invoke(util.clj:482) (link: storm-core-0.11.0-SNAPSHOT.jar:0.11.0-SNAPSHOT)
at clojure.lang.AFn.run(AFn.java:22) (link: clojure-1.7.0.jar:?)
at java.lang.Thread.run(Thread.java:745) (link: ?:1.8.0_60)

Caused by: java.lang.RuntimeException: Unable to resolve symbol: sentence-spout in this context

at clojure.lang.Util.runtimeException(Util.java:221) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.resolveIn(Compiler.java:7019) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.resolve(Compiler.java:6963) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6924) ~(link: clojure-1.7.0.jar:?)
at clojure.lang.Compiler.analyze(Compiler.java:6506) ~(link: clojure-1.7.0.jar:?)
... 33 more

{verbatim}

If I make the static java function synchronized the issue goes away. It always seems to blow up when parsing a few specific macros getting confused that a specific symbol cannot be resolved.

The namespace trying to be loaded.
https://github.com/apache/storm/blob/a99d9c11be005ade7c308bebdda71c7fb0111acc/examples/storm-starter/src/clj/storm/starter/clj/word_count.clj

The macros that we seem to get exceptions on.
https://github.com/apache/storm/blob/a99d9c11be005ade7c308bebdda71c7fb0111acc/storm-core/src/clj/backtype/storm/clojure.clj#L77-L138

And like I said it look like it is a threading issue of some sort. When I added the synchronized keyword everything works.

4 Answers

0 votes
by

Comment made by: hiredman

calling require from clojure isn't thread safe either, no different from this

0 votes
by

Comment made by: jafingerhut

Is this issue perhaps solved by using the new serialized-require that has been added in Clojure 1.10?

0 votes
by

Comment made by: alexmiller

The plan is to rework require in future release so that serialized-require is no longer needed. Maybe like that, maybe some other way. This issue is basically the placeholder for the real work. Adding the lock in serialized-require is a big hammer and we would like to make that a bit more surgical.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1876 (reported by alex+import)
...