Skip to content

[ji] avoid already initialized constant warnings#8250

Merged
kares merged 4 commits intojruby:masterfrom
kares:ji-include-package-warnings
Jun 5, 2024
Merged

[ji] avoid already initialized constant warnings#8250
kares merged 4 commits intojruby:masterfrom
kares:ji-include-package-warnings

Conversation

@kares
Copy link
Member

@kares kares commented May 21, 2024

similar to how java_import works we should check whether the constant was already set and not set it again - this effectively avoids emitting a lot of "already initialized constant" in a multi threaded environment.

on top of that using synchronization we avoid "already initialized constant" even in rare cases (which could have still happened with Java proxy class initialization in general).

reproducer for the warnings is easy to spot esp. with include_package:

jruby -v -e "mod = Module.new { include_package 'java.lang.reflect' }; threads = (0..10).map { Thread.start { sleep(0.01); mod::Array } }.each { |t| t.join }"

jruby 9.4.6.0 (3.1.4) 2024-02-20 576fab2c51 OpenJDK 64-Bit Server VM 11.0.14.1+1 on 11.0.14.1+1 +jit [x86_64-linux]
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array
-e:1: warning: already initialized constant #<Module:0x5957245>::Array

kares added 2 commits May 21, 2024 14:43
similar to how `java_import` works we should check whether the constant
was already set and not set it again - this effectively avoids emitting
a lot of "already initialized constant" in a multi threaded environment
@kares kares force-pushed the ji-include-package-warnings branch from b1a5e69 to 5a3466e Compare May 21, 2024 12:43
@kares kares changed the title [ji] include_package should not same class again [ji] include_package should not set same class again May 21, 2024
sleep 0.01; mod::Field
sleep 0.01; mod::ThreadInfo
sleep 0.01; mod::LockInfo
sleep 0.01; mod::Array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better concurrency test could be to have each spawned thread wait on something like a CountDownLatch before trying to access each imported Java class, and have the main thread release the latches one at a time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's a good point. although this actually does reproduce both types of warnings fairly reliably...

@kares kares marked this pull request as ready for review May 22, 2024 12:38
@kares kares changed the title [ji] include_package should not set same class again [ji] avoid already initialized constant warnings May 22, 2024
@kares kares added this to the JRuby 9.4.8.0 milestone May 22, 2024
output = with_stderr_captured do
threads = (0..100).map do
latch = java.util.concurrent.CountDownLatch.new(1)
waiting_thread_count = java.util.concurrent.atomic.AtomicInteger.new
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making this one a CountDownLatch too? Waiting for something to happen N times is what they're designed for, even if I do often end up using them with N = 1 😄

@kares kares merged commit 9aa01b5 into jruby:master Jun 5, 2024
@kares kares deleted the ji-include-package-warnings branch June 5, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants