Author: nextgens
Date: 2008-08-31 10:30:37 +0000 (Sun, 31 Aug 2008)
New Revision: 22270

Modified:
   trunk/freenet/src/freenet/support/io/TempBucketFactory.java
Log:
TempBucket: prevent double-free and prevent migration on already freed buckets!

Modified: trunk/freenet/src/freenet/support/io/TempBucketFactory.java
===================================================================
--- trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2008-08-31 
08:42:16 UTC (rev 22269)
+++ trunk/freenet/src/freenet/support/io/TempBucketFactory.java 2008-08-31 
10:30:37 UTC (rev 22270)
@@ -74,6 +74,7 @@
                private short osIndex;
                /** A timestamp used to evaluate the age of the bucket and 
maybe consider it for a migration */
                public final long creationTime;
+               private boolean hasBeenFreed = false;

                public TempBucket(long now, Bucket cur) {
                        if(cur == null)
@@ -101,7 +102,7 @@
                private final void migrateToFileBucket() throws IOException {
                        Bucket toMigrate = null;
                        synchronized(this) {
-                               if(!isRAMBucket())
+                               if(!isRAMBucket() || hasBeenFreed)
                                        // Nothing to migrate! We don't want to 
switch back to ram, do we?                                      
                                        return;
                                toMigrate = currentBucket;
@@ -331,11 +332,18 @@
                }

                public synchronized void free() {
+                       if(hasBeenFreed) return;
+                       hasBeenFreed = true;
+                       
                        Closer.close(os);
                        closeInputStreams(true);
                        currentBucket.free();
-                       if(isRAMBucket())
+                       if(isRAMBucket()) {
                                _hasFreed(currentSize);
+                               synchronized(ramBucketQueue) {
+                                       ramBucketQueue.remove(this);
+                               }
+                       }
                }
        }



Reply via email to