vegardit / haxe-concurrent

A haxelib for basic platform-agnostic concurrency support
https://vegardit.github.io/haxe-concurrent/
Apache License 2.0
82 stars 14 forks source link

[jvm] ExceptionInInitializerError #27

Closed acarioni closed 11 months ago

acarioni commented 1 year ago

When I run my tests with haxe-concurrent 5.1.3 and haxe 4.3.1, I get the following error. However if I compile the same tests with haxe 4.3-rc.1, they work fine. Unfortunately I am not able to reproduce the error by means of a simple test, but the basic execution flow is trivial: I create an instance of Executor in a static variable and, when I try to call the method submit on it from the method Test.delay, the exception ExceptionInInitializerError is thrown.

java.lang.ExceptionInInitializerError
          at utest.Test.delay(test-util/utest/Test.hx:152)
          at utest.Runner.runCurrent(test-util/utest/Runner.hx:129)
          at utest.Runner.gotoFirstTest(test-util/utest/Runner.hx:85)
          at utest.Runner$Closure_evtRun_0.invoke(test-util/utest/Runner.hx:60)
          at utest.Runner$Closure_evtRun_0.invoke(test-util/utest/Runner.hx)
          at hx.concurrent.lock.AbstractAcquirable.execute(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/lock/Acquirable.hx:55)
          at utest.Runner.evtRun(test-util/utest/Runner.hx:55)
          at utest.Runner$Closure_run_0.invoke(test-util/utest/Runner.hx:52)
          at utest.Runner$Closure_run_0.invoke(test-util/utest/Runner.hx)
          at hx.concurrent.lock.AbstractAcquirable.execute(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/lock/Acquirable.hx:55)
          at utest.Runner.run(test-util/utest/Runner.hx:50)
          at haxe.root.TestAll.main(test/TestAll.hx:93)
          at haxe.root.TestAll.main(test/TestAll.hx:1)
      Caused by: java.lang.ClassCastException: class hx.concurrent.thread.ThreadPool$Closure_onStart_0 cannot be cast to class java.lang.Runnable (hx.concurrent.thread.ThreadPool$Closure_onStart_0 is in unnamed module of loader 'app'; java.lang.Runnable is in module java.base of loader 'bootstrap')
          at sys.thread.Thread$NativeHaxeThread.<init>(/Users/acarioni/haxe/versions/4.3.1/std/java/_std/sys/thread/Thread.hx:160)
          at sys.thread.Thread$HaxeThread.create(/Users/acarioni/haxe/versions/4.3.1/std/java/_std/sys/thread/Thread.hx:104)
          at hx.concurrent.thread.ThreadPool.onStart(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/thread/ThreadPool.hx:102)
          at hx.concurrent.ServiceBase$Closure_start_0.invoke(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/Service.hx:65)
          at hx.concurrent.ServiceBase$Closure_start_0.invoke(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/Service.hx)
          at hx.concurrent.lock.AbstractAcquirable.execute(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/lock/Acquirable.hx:55)
          at hx.concurrent.ServiceBase.start(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/Service.hx:58)
          at hx.concurrent.thread.ThreadPool.<init>(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/thread/ThreadPool.hx:61)
          at hx.concurrent.executor.ThreadPoolExecutor.<init>(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/executor/ThreadPoolExecutor.hx:41)
          at hx.concurrent.executor.Executor.create(/Users/acarioni/haxe/haxe_libraries/haxe-concurrent/5.1.3/haxelib/src/hx/concurrent/executor/Executor.hx:36)
          at utest.Test$Test_Fields_.<clinit>(test-util/utest/Test.hx:10)
          ... 13 more
sebthom commented 1 year ago

This looks like a bug in the JVM target itself and you would need to report it directly at https://github.com/HaxeFoundation/haxe/issues. Does it work with the Java target?

acarioni commented 1 year ago

The java target is deprecated and my project can't compile with it.

acarioni commented 1 year ago

This is the issue that I opened on the haxe project.

sebthom commented 1 year ago

@acarioni can you give me a code example where the compilation fails. I am somehow not able to reproduce it.

The following compiles fine for me when using the jvm target.

import hx.concurrent.executor.Executor;

class Test {

   static final executor = Executor.create(2);

   public static function main() {
      executor.submit(function() {
         trace("hi");
      }, FIXED_RATE(100));
   }
}
acarioni commented 1 year ago

I tried hard to reproduce the issue by means of a simple example but unfortunately I wasn’t able. However I can reproduce it systematically when I compile my open source project. In order to compile my project, you need to install a few tools (like Ant), but I can assist you if you need help. You can find some other details in the issue thread on the haxe project.

sebthom commented 1 year ago

If you locally modify the ThreadPool.hx file and replace the occurrences of (function() { with (function():Void {, does it then work?

acarioni commented 1 year ago

I replaced the (only) occurrence of Threads.spawn(function() { with Threads.spawn(function(): Void { but I have the same error (I use hx.concurrent 5.1.3).

sebthom commented 1 year ago

Ok, then I am out of ideas. I hoped by adding the return type explicitly it may help the Haxe compiler to generate code that is compatible with the java Runnable interface. Then we have to wait that the upstream issue you reported gets resolved directly by the haxe team

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If the issue is still valid, please add a respective comment to prevent this issue from being closed automatically. Thank you for your contributions.