Skip to content

Concurrent requires can lead to StringIndexOutOfBoundsException #8958

@jsvd

Description

@jsvd

Environment Information

jruby 9.4.13.0 (3.1.4) 2025-06-10 9938a3461f OpenJDK 64-Bit Server VM 21.0.6+7-LTS on 21.0.6+7-LTS +jit [arm64-darwin]

Download logstash for your architecture at https://www.elastic.co/downloads/logstash

Gemfile:

source "https://rubygems.org"

gem "logstash-core", :path => "./logstash-9.1.1"
gem "logstash-input-file"
gem "awesome_print"

script.rb

Thread.abort_on_exception = true

require 'logstash-core'
require 'logstash/pipeline'

t1 = Thread.new do
 require 'awesome_print'
end
Thread.new do
  require 'logstash/inputs/file'
end
t1.join
sleep 1

Expected Behavior

Script should load without issues.

Actual Behavior

❯ bundle exec ruby script.rb
warning: thread "Ruby-0-Thread-1: script.rb:6" terminated with exception (report_on_exception is true):LoadError: load error: awesome_print/formatters/dir_formatter -- java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 16
Did you mean?  awesome_print/formatters/dir_formatter
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
    <module:Formatters> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:10
  <module:AwesomePrint> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:2
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:1
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatter.rb:6
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print.rb:17
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at script.rb:7
LoadError: load error: awesome_print/formatters/dir_formatter -- java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 16
Did you mean?  awesome_print/formatters/dir_formatter
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
    <module:Formatters> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:10
  <module:AwesomePrint> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:2
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatters.rb:1
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print/formatter.rb:6
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at /Users/joaoduarte/experiments/jruby_required_concurrency/my_debug/vendor/bundle/jruby/3.1.0/gems/awesome_print-1.9.2/lib/awesome_print.rb:17
                require at org/jruby/RubyKernel.java:1187
                require at /Users/joaoduarte/.rvm/rubies/jruby-9.4.13.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:144
                 <main> at script.rb:7

If I print the cause instead I get:

Index -1 out of bounds for length 16
java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
java.base/java.lang.String.checkIndex(String.java:4832)
java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46)
java.base/java.lang.String.charAt(String.java:1555)
org.jruby.dist/org.jruby.runtime.load.LibrarySearcher.loadedFeatureWithPath(LibrarySearcher.java:704)
org.jruby.dist/org.jruby.runtime.load.LibrarySearcher.isFeature(LibrarySearcher.java:612)
org.jruby.dist/org.jruby.runtime.load.LibrarySearcher.findLibraryForRequire(LibrarySearcher.java:115)
org.jruby.dist/org.jruby.runtime.load.LoadService.searchForRequire(LoadService.java:744)
org.jruby.dist/org.jruby.runtime.load.LoadService.smartLoadInternal(LoadService.java:550)
org.jruby.dist/org.jruby.runtime.load.LoadService.require(LoadService.java:423)
org.jruby.dist/org.jruby.RubyKernel.requireCommon(RubyKernel.java:1194)
org.jruby.dist/org.jruby.RubyKernel.require(RubyKernel.java:1187)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions