Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8139

GroovyScriptEngineImpl may not be thread safe

    XMLWordPrintableJSON

Details

    Description

      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.

      Attachments

        1. InvokeFunctionTest.java
          3 kB
          Laurent Droin

        Activity

          People

            Unassigned Unassigned
            laurent1 Laurent Droin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: