javahalt

Java Runtime#halt() does not stop the JVM


This is a client/server application using RMI. Once a RMI request is received by the server for destroy() - I need to abort the VM. I am using Runtime.getRuntime().halt(0) for this purpose. But after the call to halt() the VM still exists and the only way I can kill it is by using Force Kill on OSX.

The stacktrace for the calling thread is as follows:

"RMI TCP Connection(3)-192.168.1.4" daemon prio=5 tid=7f99a7103000 nid=0x11d467000 runnable [11d464000]
   java.lang.Thread.State: RUNNABLE
    at java.lang.Shutdown.halt0(Native Method)
    at java.lang.Shutdown.halt(Shutdown.java:95)
    - locked <7f44c90d0> (a java.lang.Shutdown$Lock)
    at java.lang.Runtime.halt(Runtime.java:256)
    at net.sourceforge.marathon.runtime.JavaRuntime.destroy(JavaRuntime.java:178)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at net.sourceforge.rmilite.impl.RemoteInvocationHandlerImpl.invoke(RemoteInvocationHandlerImpl.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

What may be the issue?


Solution

  • I tried multiple things to make this work.

    1. Added Runtime#runFinalization before the call to halt().
    2. Invoked halt() in SwingUtilities.invokeAndWait
    3. Invoked halt() in SwingUtilities.invokeLater

    None of these worked. Finally,

    1. Invoked halt() in a timer task using Timer#schedule

    This worked and is consistently working till now. Thanks to everyone for the replies.