Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.6.1
-
Fix Version/s: JRuby 1.6.2
-
Component/s: Core Classes/Modules
-
Labels:None
-
Environment:independent - observed on windows and linux
-
Testcase included:yes
-
Number of attachments :
Description
The method Array#pack is no longer thread-safe. That is, if called on different arrays in different threads it may not encode the array properly. In particular, it may encode one array using the converter for another call.
I have attached a simple test program, which starts two threads each in a tight loop calling pack and checking the result. But, they call pack with different arguments. It will print each time it gets the wrong result. (NOTE: the program will not terminate until you stop it w/CTRL-C or equivalent.)
Looking at the code in org.jruby.util.Pack.java, the issue arises here (line 1830 in jruby 1.6.0):
Converter converter = converters[type];
if (converter != null)
{ executor.setConverter(converter); idx = encode(runtime, occurrences, result, list, idx, executor); continue; }the variable executor is really executor18, a static variable in this class. Hence, it is shared across all calls to pack. But, this shared static variable has state, in particular from executor.setConverter().
I am attaching two potential patches to resolve this.
jruby-pack.patch
threadsafe-pack.patch:
Apologies for not having written proper unit tests for this.