[ji] avoid already initialized constant warnings#8250
Merged
kares merged 4 commits intojruby:masterfrom Jun 5, 2024
Merged
Conversation
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
b1a5e69 to
5a3466e
Compare
| sleep 0.01; mod::Field | ||
| sleep 0.01; mod::ThreadInfo | ||
| sleep 0.01; mod::LockInfo | ||
| sleep 0.01; mod::Array |
Contributor
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
yeah, that's a good point. although this actually does reproduce both types of warnings fairly reliably...
| 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 |
Contributor
There was a problem hiding this comment.
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 😄
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
similar to how
java_importworks 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 }"