Patch for kjas: 'clean' exit of jvm (#75290)

Koos Vriezen koos.vriezen at xs4all.nl
Sat Feb 28 20:52:10 GMT 2004


Hi,

Attached a patch for cleanly exiting the jvm when java process is killed by
the unloading of javaappletviewer lib (not by the server timeout).
There are two problems here. First the destroyApplet is done in a thread,
because no protocal call may block the communication with the kpart (this
is even more true if KIO is used). So if a jvm shutdown is requested, all
these thread must be 'join'ed for a while and KProcess::wait must be used.
Second, the jvm shutdown request and other calls like destroyApplet (*)
itself, never reached the jvm. Reason is that I/O is async and closing the
context, destroying the applets and unloading the javaappletviewer is one
big chain, not allowing the other protocol calls to pass. I fixed this
by calling KProcess::slotSendData(int) directly, risking loss of data but
that was lost anyway (and a test with a page with three applets, works
correctly).
As this is a bug, I would like to backport this (together with a small
java-1.3 compatibility fix).

(*) only if there were more applets, however destroyContext and
    shutdownJVM never reaches the jvm.

Koos
-------------- next part --------------
Index: kjavaprocess.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/java/kjavaprocess.cpp,v
retrieving revision 1.48
diff -u -3 -p -r1.48 kjavaprocess.cpp
--- kjavaprocess.cpp	30 Nov 2003 13:05:00 -0000	1.48
+++ kjavaprocess.cpp	28 Feb 2004 17:09:42 -0000
@@ -319,6 +319,12 @@ void KJavaProcess::killJVM()
    javaProcess->kill();
 }
 
+void KJavaProcess::flushBuffers()
+{
+    while ( !d->BufferList.isEmpty() )
+        slotSendData(0);
+}
+
 /*  In this method, read one command and send it to the d->appletServer
  *  then return, so we don't block the event handling
  */
Index: kjavaprocess.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/java/kjavaprocess.h,v
retrieving revision 1.20
diff -u -3 -p -r1.20 kjavaprocess.h
--- kjavaprocess.h	13 Aug 2003 19:47:44 -0000	1.20
+++ kjavaprocess.h	28 Feb 2004 17:09:42 -0000
@@ -117,6 +117,11 @@ public:
      */
     void send( char cmd_code, const QStringList& args, const QByteArray& data );
 
+    /**
+     * Writes all pending data to JVM
+     **/
+    void flushBuffers();
+
 protected slots:
     /**
      * This slot is called whenever something is written to stdin of the process.
Index: kjavaappletserver.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/java/kjavaappletserver.cpp,v
retrieving revision 1.68
diff -u -3 -p -r1.68 kjavaappletserver.cpp
--- kjavaappletserver.cpp	29 Nov 2003 13:05:21 -0000	1.68
+++ kjavaappletserver.cpp	28 Feb 2004 17:09:42 -0000
@@ -439,6 +439,8 @@ void KJavaAppletServer::quit()
     QStringList args;
 
     process->send( KJAS_SHUTDOWN_SERVER, args );
+    process->flushBuffers();
+    process->wait( 10 );
 }
 
 void KJavaAppletServer::slotJavaRequest( const QByteArray& qb )
Index: org/kde/kjas/server/KJASAppletStub.java
===================================================================
RCS file: /home/kde/kdelibs/khtml/java/org/kde/kjas/server/KJASAppletStub.java,v
retrieving revision 1.53
diff -u -3 -p -r1.53 KJASAppletStub.java
--- org/kde/kjas/server/KJASAppletStub.java	19 Feb 2004 18:33:21 -0000	1.53
+++ org/kde/kjas/server/KJASAppletStub.java	28 Feb 2004 17:09:42 -0000
@@ -380,6 +380,18 @@ public final class KJASAppletStub
         destroyThread.start();
     }
 
+    static void waitForDestroyThreads()
+    {
+        Thread [] ts = new Thread[Thread.activeCount()];
+        Thread.enumerate(ts);
+        for (int i = 0; i < ts.length; i++)
+            if (ts[i].getName().equals("applet destroy thread")) {
+                try {
+                    ts[i].join(10000);
+                } catch (InterruptedException ie) {}
+            }
+    }
+
     /**
     * get the Applet managed by this stub.
     * @return the Applet or null if the applet could not be loaded
Index: org/kde/kjas/server/KJASProtocolHandler.java
===================================================================
RCS file: /home/kde/kdelibs/khtml/java/org/kde/kjas/server/KJASProtocolHandler.java,v
retrieving revision 1.51
diff -u -3 -p -r1.51 KJASProtocolHandler.java
--- org/kde/kjas/server/KJASProtocolHandler.java	23 Jan 2004 12:18:53 -0000	1.51
+++ org/kde/kjas/server/KJASProtocolHandler.java	28 Feb 2004 17:09:42 -0000
@@ -227,6 +228,7 @@ public class KJASProtocolHandler
         if( cmd_code_value == ShutdownServerCode )
         {
             Main.debug( "shutDownServer received" );
+            KJASAppletStub.waitForDestroyThreads();
             System.exit( 1 );
         }
         else



More information about the kfm-devel mailing list