Author: toad
Date: 2007-04-14 19:47:08 +0000 (Sat, 14 Apr 2007)
New Revision: 12711

Modified:
   trunk/freenet/src/freenet/node/InsertHandler.java
Log:
CHK insert handler should not complete until the data has been received.
Sometimes the data won't be sent, therefore we need to wait for it to be 
received.
(Most of the time it will be sent, so we only need to wait for the sends; but 
if they fail, or if it terminates here...)

Modified: trunk/freenet/src/freenet/node/InsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/InsertHandler.java   2007-04-14 19:33:16 UTC 
(rev 12710)
+++ trunk/freenet/src/freenet/node/InsertHandler.java   2007-04-14 19:47:08 UTC 
(rev 12711)
@@ -326,6 +326,18 @@
                        // May need to commit anyway...
                }
         }
+
+       synchronized(this) {
+               if(receiveStarted) {
+                       while(!receiveCompleted) {
+                               try {
+                                               wait(100*1000);
+                                       } catch (InterruptedException e) {
+                                               // Ignore
+                                       }
+                       }
+               }
+       }

         if(code != CHKInsertSender.TIMED_OUT && code != 
CHKInsertSender.GENERATED_REJECTED_OVERLOAD && 
                        code != CHKInsertSender.INTERNAL_ERROR && code != 
CHKInsertSender.ROUTE_REALLY_NOT_FOUND &&
@@ -381,18 +393,30 @@

        /** Has the receive failed? If so, there's not much more that can be 
done... */
     private boolean receiveFailed;
+    
+    private boolean receiveStarted;
+    private boolean receiveCompleted;

     public class DataReceiver implements Runnable {

         public void run() {
+               synchronized(this) {
+                       receiveStarted = true;
+               }
                if(logMINOR) Logger.minor(this, "Receiving data for 
"+InsertHandler.this);
             try {
                 br.receive();
                 if(logMINOR) Logger.minor(this, "Received data for 
"+InsertHandler.this);
+               synchronized(InsertHandler.this) {
+                       receiveCompleted = true;
+                       InsertHandler.this.notifyAll();
+               }
                 maybeCommit();
             } catch (RetrievalException e) {
                synchronized(InsertHandler.this) {
+                       receiveCompleted = true;
                        receiveFailed = true;
+                       InsertHandler.this.notifyAll();
                }
                 // Cancel the sender
                 sender.receiveFailed(); // tell it to stop if it hasn't 
already failed... unless it's sending from store


Reply via email to