Commits

Anonymous committed 1dffe6f

Issue number: CACHE-217 DiskPersistenceListener deadlocks cache if process has no rights to delete cache file
Obtained from: Christoph Kutzinski
Submitted by: Lars Torunski

Comments (0)

Files changed (1)

src/java/com/opensymphony/oscache/plugins/diskpersistence/AbstractDiskPersistenceListener.java

             // Use default value
         }
     }
+    
+    // try 30s to delete the file
+    private static final long DELETE_THREAD_SLEEP = 500;
+    private static final int DELETE_COUNT = 60; 
 
     protected void remove(File file) throws CachePersistenceException {
+        int count = DELETE_COUNT;
         try {
             // Loop until we are able to delete (No current read).
             // The cache must ensure that there are never two concurrent threads
             // doing write (store and delete) operations on the same item.
             // Delete only should be enough but file.exists prevents infinite loop
-            while (!file.delete() && file.exists()) {
-                ;
+            while (file.exists() && !file.delete() && count != 0) {
+                count--;
+                try {
+                    Thread.sleep(DELETE_THREAD_SLEEP);
+                } catch (InterruptedException ignore) {
+                } 
             }
         } catch (Exception e) {
             throw new CachePersistenceException("Unable to remove '" + file + "' from the cache: " + e);
         }
+        if (file.exists() && count == 0) {
+            throw new CachePersistenceException("Unable to delete '" + file + "' from the cache. "+DELETE_COUNT+" attempts at "+DELETE_THREAD_SLEEP+" milliseconds intervals.");
+        }
     }
 
     /**
                 }
             }
         } catch (Exception e) {
-            while (file.exists() && !file.delete()) {
-                ;
+            int count = DELETE_COUNT;
+            while (file.exists() && !file.delete() && count != 0) {
+                count--;
+                try {
+                    Thread.sleep(DELETE_THREAD_SLEEP);
+                } catch (InterruptedException ignore) {
+                } 
             }
             throw new CachePersistenceException("Unable to write '" + file + "' in the cache. Exception: " + e.getClass().getName() + ", Message: " + e.getMessage());
         }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.