Author: toad
Date: 2007-11-27 13:27:10 +0000 (Tue, 27 Nov 2007)
New Revision: 15961

Modified:
   trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java
   trunk/freenet/src/freenet/node/fcp/FCPConnectionOutputHandler.java
Log:
Fix deadlock.
Exit immediately when the connection is closed and no more messages to send.
wait() not wait(10k)
Don't leak a socket.

Modified: trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java        
2007-11-27 12:24:20 UTC (rev 15960)
+++ trunk/freenet/src/freenet/node/fcp/FCPConnectionHandler.java        
2007-11-27 13:27:10 UTC (rev 15961)
@@ -103,6 +103,7 @@
                        requests[i].onLostConnection();
                if((client != null) && !client.hasPersistentRequests())
                        server.unregisterClient(client);
+               outputHandler.onClosed();
        }

        public synchronized boolean isClosed() {

Modified: trunk/freenet/src/freenet/node/fcp/FCPConnectionOutputHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPConnectionOutputHandler.java  
2007-11-27 12:24:20 UTC (rev 15960)
+++ trunk/freenet/src/freenet/node/fcp/FCPConnectionOutputHandler.java  
2007-11-27 13:27:10 UTC (rev 15961)
@@ -44,14 +44,16 @@
        private void realRun() throws IOException {
                OutputStream os = new 
BufferedOutputStream(handler.sock.getOutputStream(), 4096);
                while(true) {
-                       FCPMessage msg;
-                       synchronized(outQueue) {
-                               while(true) {
+                       boolean closed;
+                       FCPMessage msg = null;
+                       while(true) {
+                               closed = handler.isClosed();
+                               synchronized(outQueue) {
                                        if(outQueue.isEmpty()) {
-                                               if(handler.isClosed()) return;
+                                               if(closed) break;
                                                os.flush();
                                                try {
-                                                       outQueue.wait(10000);
+                                                       outQueue.wait();
                                                } catch (InterruptedException 
e) {
                                                        // Ignore
                                                }
@@ -61,11 +63,14 @@
                                        break;
                                }
                        }
-                       msg.send(os);
-                       if(handler.isClosed()) {
-                               os.flush();
-                               os.close();
-                               return;
+                       if(msg == null) {
+                               if(closed) {
+                                       os.flush();
+                                       os.close();
+                                       return;
+                               }
+                       } else {
+                               msg.send(os);
                        }
                }
        }
@@ -80,4 +85,10 @@
                }
        }

+       public void onClosed() {
+               synchronized(outQueue) {
+                       outQueue.notifyAll();
+               }
+       }
+
 }


Reply via email to