-
-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initialization timing issue for static variable #1671
Conversation
currentPlatform variable initialized in static block of this class. But it's also accessed via constructor for validate unsupported environment. This variable possible not initialized yet when access make a instance use constructor. I'm already met this kind of issue when I run unit test use this library. I think one more variable init checking required, at least If constructor want to check unsupported or not.
It is my understanding that the order of operations in Java executes static variable initialization first (in source code order) followed by instance variable initialization, and only after that, the constructor. So I don't see where there's a problem with the existing code. Your change introduces lazy instantiation which is known to be not thread safe, so the suggestion as-is makes things worse. If lazy instantiation were needed using the But I don't see a need to do that for a |
@dbwiddis It's occured only our CI process in gitlab. I'm using 5.2.3 version. And this problem appeared after upgrade to latest version. |
Odd problem, can you show us your code? The JNA stuff is pure java and according to the java spec any acces to a static class member will be preceded by execution of the static initializers unless a class instance has aready been created in which case the static initilizers are executed before the constructor. |
The error message "Operating system not supported: JNA Platform type" should have been followed by a number. The Please check that you have the latest version of JNA (and JNA-platform) as a dependency, although that shouldn't matter here If you are using a supported operating system, then the bug is in JNA properly identifying it, which it does with JVM properties. Can you answer a few more questions:
|
@dbwiddis What number is reported in the exception after "JNA Platform type [this value]" What operating system are you using? What JDK are you using (vendor and version)? What is the contents of System.getProperty("os.name") ? |
@mprins Here is my code. class StorageOnSystemSpec : FunSpec({
test("display as string") {
println(StorageOnSystem(SystemInfo()).diskAndPartition())
}
}) Error occured when call a constructor |
Well, this is very interesting because 2 is indeed Windows. From JNA's Platform class (as expected):
and
called by OSHI
So the It looks like you're calling this from Kotlin code. I wonder if there's some subtle difference there. I'm baffled as to how this can occur. |
Could you point us to the CI log for the case where this is failing? |
This StackOverflow post may be related. Or not. |
It has been suggested that using a static method rather than a static initialization block may fix this. Please update this PR to do something like:
and then define
|
ok, I will do that. |
I updated this PR. |
Thanks! I went ahead and merged this. It should be in SNAPSHOT shortly to aid your testing. I may tweak the class ordering later (to move the method below the constructor) but that shouldn't change the behavior. |
oshi#1671 not worked for me. Run CI with snapshot version but I saw same error message below. ``` [ERROR] display as string Time elapsed: 0.016 s <<< ERROR! java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 Caused by: java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 ``` JNA could recognize current os in static block when failed on a constructor of SystemInfo class. So I tried check unknown environment by JNA method instead of OSHI. At the result my CI passed successfully. I'm not sure what is exact reason. But I just think that JNA and OSHI depends on many static initialization and it's make not easy to assume proper working. In my opinion, static blocks and variables should be minimized. But if OSHI have important reason to check unsupported environment in constructor this MR need to approved. There is remained riddle why OSHI's static variable and JNA static method result don't have consistency. Anyway, This change working for me. Please consider enough.
…1680) * Initialization timing issue for static variable currentPlatform variable initialized in static block of this class. But it's also accessed via constructor for validate unsupported environment. This variable possible not initialized yet when access make a instance use constructor. I'm already met this kind of issue when I run unit test use this library. I think one more variable init checking required, at least If constructor want to check unsupported or not. * Initialize static variable from static method instead of static block * Initialize static variable from static method instead of static block #1671 not worked for me. Run CI with snapshot version but I saw same error message below. ``` [ERROR] display as string Time elapsed: 0.016 s <<< ERROR! java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 Caused by: java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 ``` JNA could recognize current os in static block when failed on a constructor of SystemInfo class. So I tried check unknown environment by JNA method instead of OSHI. At the result my CI passed successfully. I'm not sure what is exact reason. But I just think that JNA and OSHI depends on many static initialization and it's make not easy to assume proper working. In my opinion, static blocks and variables should be minimized. But if OSHI have important reason to check unsupported environment in constructor this MR need to approved. There is remained riddle why OSHI's static variable and JNA static method result don't have consistency. Anyway, This change working for me. Please consider enough. * Update SystemInfo.java * Add comment to explain why constructor is empty Co-authored-by: sam <sam@jennifersoft.com> Co-authored-by: Daniel Widdis <widdis@gmail.com>
* Initialization timing issue for static variable currentPlatform variable initialized in static block of this class. But it's also accessed via constructor for validate unsupported environment. This variable possible not initialized yet when access make a instance use constructor. I'm already met this kind of issue when I run unit test use this library. I think one more variable init checking required, at least If constructor want to check unsupported or not. * Initialize static variable from static method instead of static block Co-authored-by: sam <sam@jennifersoft.com>
…1680) * Initialization timing issue for static variable currentPlatform variable initialized in static block of this class. But it's also accessed via constructor for validate unsupported environment. This variable possible not initialized yet when access make a instance use constructor. I'm already met this kind of issue when I run unit test use this library. I think one more variable init checking required, at least If constructor want to check unsupported or not. * Initialize static variable from static method instead of static block * Initialize static variable from static method instead of static block #1671 not worked for me. Run CI with snapshot version but I saw same error message below. ``` [ERROR] display as string Time elapsed: 0.016 s <<< ERROR! java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 Caused by: java.lang.UnsupportedOperationException: Operating system not supported: JNA Platform type 2 ``` JNA could recognize current os in static block when failed on a constructor of SystemInfo class. So I tried check unknown environment by JNA method instead of OSHI. At the result my CI passed successfully. I'm not sure what is exact reason. But I just think that JNA and OSHI depends on many static initialization and it's make not easy to assume proper working. In my opinion, static blocks and variables should be minimized. But if OSHI have important reason to check unsupported environment in constructor this MR need to approved. There is remained riddle why OSHI's static variable and JNA static method result don't have consistency. Anyway, This change working for me. Please consider enough. * Update SystemInfo.java * Add comment to explain why constructor is empty Co-authored-by: sam <sam@jennifersoft.com> Co-authored-by: Daniel Widdis <widdis@gmail.com>
currentPlatform variable initialized in static block of this class.
But it's also accessed via constructor for validate unsupported environment.
This variable possible not initialized yet when access make a instance use constructor.
I'm already met this kind of issue when I run unit test use this library.
I think one more variable init checking required, at least If constructor want to check unsupported or not.