I ran into an interesting problem when upgrading JRuby from 1.5.2 to 1.6.1. It was creeping me out all weekend until I boiled it down to a handful of lines. It seems that a bug may have been introduced somewhere that causes a failure when getEngineByName is called a few too many times. For instance, the following super-simple code works in 1.5.2 but fails in 1.6.1 after roughly 10-20 iterations:
ScriptEngineManager factory = new ScriptEngineManager();
for (int i = 0; i < 10000; i++) {
System.out.println(i);
ScriptEngine engine = factory.getEngineByName("jruby");
engine.eval("puts 'hello'");
}
Result in 1.6.1 after #16:
NameError: uninitialized constant #<Class:0x101a41cc7>::ARGV
const_missing at org/jruby/RubyModule.java:2526
Exception in thread "main" java.lang.NullPointerException
at org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java:110)
at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:93)
at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:154)
at JRubyTestFailure.main(JRubyTestFailure.java:16)
To avoid the problem, all you need to do is move the getEngineByName up outside the loop:
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("jruby");
for (int i = 0; i < 10000; i++) {
System.out.println(i);
engine.eval("puts 'hello'");
}
Unfortunately this is not so easy for my application, for architectural reasons. I may have to put separate ScriptEngine instances in a ThreadLocal. Not understanding why this is failin开发者_C百科g worries me though.
Any ideas on why I'm getting this "NameError: uninitialized constant" error from? Nope I haven't tried checking out the source yet...
精彩评论