oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.29k stars 1.63k forks source link

`graal_tear_down_isolate` stuck when `Java2D Disposer` is running #5339

Open k-x7 opened 1 year ago

k-x7 commented 1 year ago

Describe the issue I have created a small shared lib using native image, which call ImageIO.read(...). when I call graal_tear_down_isolate from the C code the program will not return anything after printing an error :

Exception while removing reference.

My assumption is Disposer when the thread interrupted it will just print the error and call wait() somewhere else, which will stuck in waiting state or the infinite while loop.

I tried to get Java2D Disposer thread and call interrupt() many times without success.

Steps to reproduce the issue

  1. create a new java class GetImageInfo.java:
    
    import org.graalvm.nativeimage.IsolateThread;
    import org.graalvm.nativeimage.c.function.CEntryPoint;
    import org.graalvm.nativeimage.c.type.CCharPointer;
    import org.graalvm.nativeimage.c.type.CTypeConversion;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;

public class GetImageInfo {

// For Testing
public static void main(String[] args) {
    printImageInfo(args[0]);
}

@CEntryPoint(name = "get_image_info")
private static void GetImageInfo(IsolateThread thread, CCharPointer path) {
    String Path = CTypeConversion.toJavaString(path);
    printImageInfo(Path);
}

private static void printImageInfo(String path) {
    System.out.println("loading img in path: " + path);
    try {
        BufferedImage input = ImageIO.read(new File(path));
        System.out.println("Height: " + input.getHeight() + ", Width: " + input.getWidth());
    } catch (IOException e) {
        System.err.println(e);
    }
}

}

2. run `javac`:

javac GetImageInfo.java

3. create config file for JNI and reflection:

java -agentlib:native-image-agent=config-output-dir=META-INF/native-image GetImageInfo PATH_TO_ANY_IMAGE.jpg

output:

loading img in path: sample.jpg Height: 720, Width: 1200


4. create a shared lib:

native-image -H:Name=libgetimageinfo GetImageInfo --shared


5. create a c file `main.c`:
```c
#include <stdio.h>
#include <stdlib.h>

#include "libgetimageinfo.h"

int main(int argc, char **argv) {
    // printf("[*] using libgetimageinfo main:\n");
    // run_main(argc, argv);

    printf("[*] using libgetimageinfo centrypoint:\n");
    graal_isolate_t *isolate = NULL;
    graal_isolatethread_t *thread = NULL;

    if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
        fprintf(stderr, "initialization error\n");
        return 1;
    }

    get_image_info(thread, argv[1]);

    if (graal_tear_down_isolate(thread) != 0) {
        fprintf(stderr, "shutdown error\n");
        return 1;
    }

    // if (graal_detach_all_threads_and_tear_down_isolate(thread) != 0) {
    //     fprintf(stderr, "shutdown error\n");
    //     return 1;
    // }
}
  1. compile main.c:

    gcc -L$(pwd) -I$(pwd) -Wall -o main main.c -lgetimageinfo
  2. run main:

    
    env LD_LIBRARY_PATH=$(pwd) ./main PATH_TO_ANY_IMAGE.jpg

[*] using libgetimageinfo centrypoint: loading img in path: qrcode.jpg Height: 720, Width: 1200 Exception while removing reference.


now the program stuck forever.

8. calling `ps` in the running program will show the following:

ps -T -p $(pidof main)

PID    SPID TTY          TIME CMD

1091924 1091924 pts/12 00:02:43 main 1091924 1091931 pts/12 00:00:00 Java2D Disposer


**Describe GraalVM and your environment:**
Tested on release and last snapshot, Linux amd64:

openjdk version "17.0.5" 2022-10-18 OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08) OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08, mixed mode, sharing)

openjdk 19 2022-09-20 OpenJDK Runtime Environment GraalVM CE 23.0.0-dev (build 19+36-jvmci-23.0-b01) OpenJDK 64-Bit Server VM GraalVM CE 23.0.0-dev (build 19+36-jvmci-23.0-b01, mixed mode, sharing)

oubidar-Abderrahim commented 1 year ago

Hi, Thank you for reporting this issue, this is tracked internally on GR 42292.

zingermax commented 1 year ago

experiencing similar issue (win and macos)

k-x7 commented 1 year ago

@oubidar-Abderrahim is there any update?

k-x7 commented 1 year ago

@oubidar-Abderrahim is there any update?

oubidar-Abderrahim commented 1 year ago

The team is still investigating the issue.

k-x7 commented 1 year ago

any update you may have would be greatly appreciated

wirthi commented 1 year ago

Hi @k-x7

we are currently working on #4921 to quite generally improve support for AWT and related technologies. Once that is merged (work is in progress already, so expect that to be merged in the next months, but no definite ETA), we will have a look at this. Could easily be affected by the current changes so we will have a look once the major changes are in.

-- Christian

nmammeri commented 4 months ago

any updates on this?? Facing similar issue while using a dependency that relies on awt. Even tried it with the latest graalvm dev builds, doesn't work. The issue described by @k-x7 is still reproducible with:

native-image 23 2024-09-17
GraalVM Runtime Environment GraalVM CE 23-dev+24.1 (build 23+24-jvmci-b01)
Substrate VM GraalVM CE 23-dev+24.1 (build 23+24, serial gc)