HaxeFoundation / HaxeFoundation/haxe
haxe.Timer not working when called from a thread without event loop
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
**Environment**
macos monterey 12.1
haxe 4.2.4
The class `haxe.Timer` throws the exception NoEventLoopException when it is called from a thread without an event loop. The error affects the threaded targets (I’ve tried with java, cs and python) and it is particularly insidious because haxe.Timer is part of the standard library and can be used not only by the user code but also by third-party libraries.
The issue can be reproduced by creating a program like this
```haxe
package foo;
class Main {
public static function main() {
sys.thread.Thread.create(() -> haxe.Timer.delay(() -> trace("hello"), 0));
Sys.sleep(1);
}
}
```
The reported error (for the target java) is
```
Exception in thread "Thread-0" Event loop is not available. Refer to sys.thread.Thread.runWithEventLoop.
at sys.thread.Thread$Thread_Impl_.get_events(/usr/local/lib/haxe/std/java/_std/sys/thread/Thread.hx:74)
at haxe.Timer.new(/usr/local/lib/haxe/std/haxe/Timer.hx:76)
at haxe.Timer.delay(/usr/local/lib/haxe/std/haxe/Timer.hx:141)
at foo.Main$Closure_main_0.invoke(foo/Main.hx:5)
at foo.Main$Closure_main_0.run(foo/Main.hx)
at java.base/java.lang.Thread.run(Thread.java:834)
at sys.thread.Thread$NativeHaxeThread.run(/usr/local/lib/haxe/std/java/_std/sys/thread/Thread.hx:166)
```
Contributor guide
Research direction
Start with haxe/Timer.hx and sys.thread.Thread.runWithEventLoop, then compare the target-specific thread implementations, including the Java path shown in the stack trace. Reproduce the provided example on the threaded targets and trace how haxe.Timer.delay handles a thread without an event loop. Done means the example no longer raises NoEventLoopException and the delayed callback behaves as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100