Skip to content

Accessing Encoding from java code is broken on new thread #7820

@ntkme

Description

@ntkme

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
    • jruby 9.4.2.0 (3.1.0) 2023-03-08 90d2913 OpenJDK 64-Bit Server VM 17.0.4+8-LTS on 17.0.4+8-LTS +jit [arm64-darwin]
  • Operating system and platform (e.g. uname -a)
    • Darwin MacBook.local 22.5.0 Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:24 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6000 arm64
    • Also tested in GitHub Actions that this fails on all platforms in the same way.

Expected Behavior

require 'java'

# This works in main thread
runtime = org.jruby.Ruby.getGlobalRuntime
puts runtime.evalScriptlet('Encoding::UTF_8')

Thread.new do
  # However, the same code is broken in new thread
  runtime = org.jruby.Ruby.getGlobalRuntime
  puts runtime.evalScriptlet('Encoding::UTF_8')
end.join

The code above should print UTF-8 twice:

UTF-8
UTF-8

Actual Behavior

UTF-8
warning: thread "Ruby-0-Thread-1: repro.rb:6" terminated with exception (report_on_exception is true):
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
	at org.jruby.Ruby.evalScriptlet(org/jruby/Ruby.java:884)
	at org.jruby.Ruby.evalScriptlet(org/jruby/Ruby.java:860)
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(jdk/internal/reflect/NativeMethodAccessorImpl.java:77)
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(jdk/internal/reflect/DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:568)
	at org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(org/jruby/javasupport/JavaMethod.java:344)
	at org.jruby.javasupport.JavaMethod.invokeDirect(org/jruby/javasupport/JavaMethod.java:205)
	at repro.invokeOther4:evalScriptlet(repro.rb:8)
	at repro.<main>(repro.rb:8)
	at org.jruby.RubyProc.call(org/jruby/RubyProc.java:352)
	at java.lang.Thread.run(java/lang/Thread.java:833)
Unhandled Java exception: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
                      evalScriptlet at org/jruby/Ruby.java:884
                      evalScriptlet at org/jruby/Ruby.java:860
                            invoke0 at jdk/internal/reflect/NativeMethodAccessorImpl.java:-2
                             invoke at jdk/internal/reflect/NativeMethodAccessorImpl.java:77
                             invoke at jdk/internal/reflect/DelegatingMethodAccessorImpl.java:43
                             invoke at java/lang/reflect/Method.java:568
  invokeDirectWithExceptionHandling at org/jruby/javasupport/JavaMethod.java:344
                       invokeDirect at org/jruby/javasupport/JavaMethod.java:205
         invokeOther4:evalScriptlet at repro.rb:8
                             <main> at repro.rb:8
                               call at org/jruby/RubyProc.java:352
                                run at java/lang/Thread.java:833

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions