Laurent Droin created GROOVY-8139:
-------------------------------------

             Summary: GroovyScriptEngineImpl may not be thread safe
                 Key: GROOVY-8139
                 URL: https://issues.apache.org/jira/browse/GROOVY-8139
             Project: Groovy
          Issue Type: Bug
          Components: GroovyScriptEngine
    Affects Versions: 2.4.10
            Reporter: Laurent Droin
            Priority: Minor
         Attachments: InvokeFunctionTest.java

I am wondering if I can safely share an instance of GroovyScriptEngineImpl 
across threads when each thread has a different Bindings AND i need to use 
InvokeFunction.
I believe I need to call the eval method on the engine in every thread so as to 
establish the new context containing thread-specific Bindings. During the call 
to Eval, the functions in my script are turned into some 'method closure' 
objects containing (between other things) the thread-specific Bindings. 
However, those 'method closure' are kept in a map that is global to the script 
engine (this map is called 'globalClosures').
I think that the "method closure" is added to the map twice, once for each 
thread, under the same key (the name of the function) and whatever thread 
updates the map last is the one that imposes its Bindings to the function.
When later, the function is invoked in each thread, the same Bindings is used.

I have attached a small sample of code demonstrating the issue.
The output is sometimes:
The task that should return 42 returned : 43
The task that should return 43 returned : 43

and sometimes:
The task that should return 42 returned : 42
The task that should return 43 returned : 42

depending on what thread updates the globalClosures last.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to