+1 vote
in Other by
Is there a way to ensure that the entire Java process will exit if there is a Hotspot crash?

We are hosting a native library in a remote Windows Java process (java.exe), for purposes of process isolation. However, we have discovered that even when there is a hotspot crash, although the main thread "dies" with a hotspot crash, the process itself does not die. We have to kill it in Task Manager.

We think this may be because the native library itself creates its own threads which are keeping the process alive.

We want the entire Java process to die if there is a hotspot crash.

Our current work around is to have another thread which is reading the output of the spawned process (we have to read the console output anyway to stop the process from blocking). We modified it to also look explicitly for a VM crash:

private static String HOTSPOT_CRASH = "# A fatal error has been detected by the Java Runtime Environment";

public void run() {

    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;

        do {

            line = reader.readLine();

            if (line != null) {

                logger.info(prefix + ": " + line);

            }

            if(line.contains(HOTSPOT_CRASH)) {

                logger.error(String.format("FATAL Hotspot crash detected. Killing child process '%s'...", hostProcess));

                hostProcess.destroy();

            }

        } while (line != null);

        reader.close();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

But this is a hack: If the JVM knows enough to log out that there's been a crash, it would be much better if it could be told to terminate the current process too.

Is there a way do do this? Ideally it would be a command line option to the JVM.

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Usually, a VM will exit when a fatal error has been detected but you can prevent this in your code. Here are a couple of clues:

Check the parent process. On Unix, a child can only die when the parent processes the SIGCHLD signal (which tells the parent that the child has terminated; this allows the parent process to read the exit code, for example).

Make sure you process the output and that you call waitFor() at least once after the child has died. That should clean up the child process.

Look for non-daemon threads. If you have any non-daemon threads in your Java application, they can prevent the VM from exiting.

But a VM error will terminate all threads, so my guess is that you never clean up the child process.
...