HaxeFoundation / HaxeFoundation/haxe

[jvm/java] sys.io.Process.getPid() does not work on Windows nor when using Java 17

Open
#10,938 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

`sys.io.Process.getPid()` relies on reflecting on an internal `pid` field which is only available for Java on Linux.
As of Java 9 the `java.lang.Process` class was extended by a `long pid()` method returning the pid of a process.
It would be great if `sys.io.Process.getPid()` would delegate to the `java.lang.Process.pid()` for the jvm/java targets and only fallback to reflecting on the internal `pid` file in case Java 8 is used.

----

Executing `sys.io.Process.getPid()` on Java 11 or newer results in:
```java
Exception in thread "main" java.lang.ClassCastException: class haxe.lang.Closure cannot be cast to class java.lang.Number (haxe.lang.Closure is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
at haxe.lang.Runtime.toInt(Runtime.java:127)
at sys.io.Process.getPid(Process.java:218)
```
----
Changing `sys.io.Process.getPid()` to:
```haxe
public function getPid():Int {
if (Reflect.hasField(proc, "pid")) {
final pid:Dynamic = Reflect.field(proc, "pid");
if (Type.typeof(pid) == TFunction)
return Reflect.callMethod(proc, pid, []);
return pid;
}
return -1;
}
```

1. works with Java 8 and 11 using the `java` target, but emits the following warnings on Java 11:
```
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by haxe.lang.Runtime (file:/D:/workspaces/projects-haxe/haxe-concurrent/target/java/TestRunner-Debug.jar) to method java.lang.ProcessImpl.pid()
WARNING: Please consider reporting this to the maintainers of haxe.lang.Runtime
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
```
2. fails using the `jvm` target:
```
java.lang.IllegalAccessException: class haxe.jvm.Closure cannot access a member of class java.lang.ProcessImpl (in module java.base) with modifiers "public"
```
3. fails on Java 17, where it throws:
```
java.lang.reflect.InaccessibleObjectException: Unable to make public long java.lang.ProcessImpl.pid() accessible: module java.base does not "opens java.lang" to unnamed module @1ef7fe8e
```

I also tried playing around with `untyped __java__` but could not get anyting working, also it does not seem to support the jvm target.

Any help is greatly appreciated.

Contributor guide

Open the contributing guide

Research direction

Start by locating the sys.io.Process.getPid() implementations for the jvm and java targets, then reproduce the failures on Java 8, 11, and 17, including Windows where available. Done means Java 9+ uses java.lang.Process.pid(), Java 8 retains a working fallback, and the reflective-access failures no longer occur.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.