Author: ecki
Date: Wed Sep 23 20:39:32 2015
New Revision: 1704932

URL: http://svn.apache.org/viewvc?rev=1704932&view=rev
Log:
[VFS-480] Avoid leaks by startng SoftRefsReleaseThread more reliable.

Modified:
    
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java?rev=1704932&r1=1704931&r2=1704932&view=diff
==============================================================================
--- 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
 (original)
+++ 
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
 Wed Sep 23 20:39:32 2015
@@ -55,8 +55,7 @@ public class SoftRefFilesCache extends A
           new HashMap<Reference<FileObject>, FileSystemAndNameKey>(100);
     private final ReferenceQueue<FileObject> refQueue = new 
ReferenceQueue<FileObject>();
 
-    private final AtomicReference<SoftRefReleaseThread> softRefReleaseThread =
-            new AtomicReference<SoftRefReleaseThread>();
+    private volatile SoftRefReleaseThread softRefReleaseThread = null; // 
@GuardedBy("lock")
 
     private final Lock lock = new ReentrantLock();
 
@@ -122,31 +121,33 @@ public class SoftRefFilesCache extends A
 
     private void startThread()
     {
-        Thread thread;
-        SoftRefReleaseThread newThread;
-        do
+        // Double Checked Locking is allowed when volatile
+        if (softRefReleaseThread != null)
         {
-            newThread = null;
-            thread = softRefReleaseThread.get();
-            if (thread != null)
+            return;
+        }
+
+        synchronized (lock)
+        {
+            if (softRefReleaseThread == null)
             {
-                break;
+                softRefReleaseThread = new SoftRefReleaseThread();
+                softRefReleaseThread.start();
             }
-            newThread = new SoftRefReleaseThread();
-        } while (softRefReleaseThread.compareAndSet(null, newThread));
-        if (newThread != null)
-        {
-            newThread.start();
         }
     }
 
     private void endThread()
     {
-        final SoftRefReleaseThread thread = 
softRefReleaseThread.getAndSet(null);
-        if (thread != null)
+        synchronized (lock)
         {
-            thread.requestEnd = true;
-            thread.interrupt();
+            final SoftRefReleaseThread thread = softRefReleaseThread;
+            softRefReleaseThread = null;
+            if (thread != null)
+            {
+                thread.requestEnd = true;
+                thread.interrupt();
+            }
         }
     }
 

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1704932&r1=1704931&r2=1704932&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Wed Sep 23 20:39:32 2015
@@ -26,6 +26,9 @@
 <!--       <action issue="VFS-443" dev="ggregory" type="update" 
due-to="nickallen"> -->
 <!--        [Local] Need an easy way to convert from a FileObject to a File. 
-->
 <!--       </action> -->
+      <action issue="VFS-480" dev="ecki" type="fix">
+        Make startup of SoftRefsFileCache cleaner thread work and less racy to 
avoid leaks.
+     </action>
       <action issue="VFS-549" dev="ecki" type="fix">
         Use File.seperator instead of getProperty("file.separator").
      </action>


Reply via email to