Issue Type: Bug Bug
Affects Versions: JRuby 1.6.7
Assignee: Unassigned
Attachments: ThreadRegexTest2.java
Components: Core Classes/Modules, Interpreter
Created: 01/Jul/12 4:12 PM
Description:

Test case:

import org.jruby.CompatVersion;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.embed.EmbedEvalUnit;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.ScriptingContainer;


public class ThreadRegexTest2 implements Runnable {

    private static ScriptingContainer makeRuntime() {
        ScriptingContainer runtime = new ScriptingContainer(LocalContextScope.CONCURRENT);
        runtime.setCompileMode(CompileMode.JIT);
        runtime.setCompatVersion(CompatVersion.RUBY1_9);

        runtime.runScriptlet(
            "module RegexTest\n" +
            "  CHECK = {\n" +
            "    :email => lambda{|str|\n" +
            "      raise 'bad email' unless str =~ /@/\n" +
            "      str.downcase\n" +
            "    },\n" +
            "    :username => lambda{|str|\n" +
            "      str[/\\A(\\w+)\\z/, 1] or raise 'bad username'\n" +
            "    }\n" +
            "  }\n" +
            "\n" +
            "  def self.check(type, str)\n" +
            "    raise 'is nil!' if str.nil?\n" +
            "    CHECK[type].call(str)\n" +
            "  end\n" +
            "end\n"
        );
        return runtime;
    }

    private final ScriptingContainer runtime;

    public ThreadRegexTest2(ScriptingContainer runtime) {
        this.runtime = runtime;
    }

    public void run() {
        System.out.println("running thread " + Thread.currentThread().getId());
        EmbedEvalUnit eval = runtime.parse(
            "RegexTest.check(:email, 'f...@example.com')\n" +
            "RegexTest.check(:username, 'foo')\n"
        );
        while (true) eval.run();
    }

    public static void main(String[] args) {
        ScriptingContainer runtime = makeRuntime();
        new Thread(new ThreadRegexTest2(runtime)).start();
        new Thread(new ThreadRegexTest2(runtime)).start();
    }
}

When run, it prints out:

running thread 11
running thread 12
RuntimeError: bad username
  RegexTest at <script>:8
       call at org/jruby/RubyProc.java:258
      check at <script>:14
     (root) at <script>:2
Exception in thread "Thread-1" org.jruby.embed.EvalFailedException: (RuntimeError) bad username
	at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:132)
	at ThreadRegexTest2.run(ThreadRegexTest2.java:48)
	at java.lang.Thread.run(Thread.java:680)
Caused by: org.jruby.exceptions.RaiseException: (RuntimeError) bad username

If only one thread is launched, it runs fine. I'm not sure if LocalContextScope.CONCURRENT is the right option to use here (I don't fully understand the documentation for LocalContextScope), but I had similar problems with LocalContextScope.THREADSAFE.

The fact that the regex matching happens inside a lambda appears to be significant.

Thanks,
Martin

Environment: Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
Mac OS X 10.7.4
Project: JRuby
Labels: concurrency
Priority: Major Major
Reporter: Martin Kleppmann
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

Reply via email to