Skip to content

Commit ec8a341

Browse files
author
Niall Pemberton
committed
Fix to resolve OutOfMemoryError has now caused another issue. Try adding a *pause* to see if additional times allows the file delete to complete. If not provide a facility to show that the delete failed.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1005707 13f79535-47bb-0310-9956-ffa450edef68
1 parent 073ee87 commit ec8a341

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/main/java/org/apache/commons/io/FileCleaningTracker.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.io.File;
2020
import java.lang.ref.PhantomReference;
2121
import java.lang.ref.ReferenceQueue;
22+
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Collections;
2425
import java.util.HashSet;
26+
import java.util.List;
2527

2628
/**
2729
* Keeps track of files awaiting deletion, and deletes them when an associated
@@ -50,6 +52,10 @@ public class FileCleaningTracker {
5052
* Collection of <code>Tracker</code> instances in existence.
5153
*/
5254
final Collection<Tracker> trackers = Collections.synchronizedSet(new HashSet<Tracker>()); // synchronized
55+
/**
56+
* Collection of File paths that failed to delete.
57+
*/
58+
final List<String> deleteFailures = Collections.synchronizedList(new ArrayList<String>());
5359
/**
5460
* Whether to terminate the thread when the tracking is complete.
5561
*/
@@ -150,6 +156,16 @@ public int getTrackCount() {
150156
return trackers.size();
151157
}
152158

159+
/**
160+
* Return the file paths that failed to delete.
161+
*
162+
* @return the file paths that failed to delete
163+
* @since Commons IO 2.0
164+
*/
165+
public List<String> getDeleteFailures() {
166+
return deleteFailures;
167+
}
168+
153169
/**
154170
* Call this method to cause the file cleaner thread to terminate when
155171
* there are no more objects being tracked for deletion.
@@ -205,7 +221,9 @@ public void run() {
205221
// Wait for a tracker to remove.
206222
Tracker tracker = (Tracker) q.remove(); // cannot return null
207223
trackers.remove(tracker);
208-
tracker.delete();
224+
if (!tracker.delete()) {
225+
deleteFailures.add(tracker.getPath());
226+
}
209227
tracker.clear();
210228
} catch (InterruptedException e) {
211229
continue;
@@ -243,6 +261,15 @@ private static final class Tracker extends PhantomReference<Object> {
243261
this.deleteStrategy = (deleteStrategy == null ? FileDeleteStrategy.NORMAL : deleteStrategy);
244262
}
245263

264+
/**
265+
* Return the path.
266+
*
267+
* @return the path
268+
*/
269+
public String getPath() {
270+
return path;
271+
}
272+
246273
/**
247274
* Deletes the file associated with this tracker instance.
248275
*

src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected void tearDown() throws Exception {
7070
{
7171
theInstance.q = new ReferenceQueue<Object>();
7272
theInstance.trackers.clear();
73+
theInstance.deleteFailures.clear();
7374
theInstance.exitWhenFinished = false;
7475
theInstance.reaper = null;
7576
}
@@ -94,9 +95,10 @@ public void testFileCleanerFile() throws Exception {
9495
r = null;
9596

9697
waitUntilTrackCount();
98+
pauseForDeleteToComplete(new File(path));
9799

98100
assertEquals(0, theInstance.getTrackCount());
99-
assertEquals(false, new File(path).exists());
101+
assertEquals(showFailures(), false, new File(path).exists());
100102
}
101103

102104
public void testFileCleanerDirectory() throws Exception {
@@ -150,10 +152,11 @@ public void testFileCleanerDirectory_ForceStrategy() throws Exception {
150152
obj = null;
151153

152154
waitUntilTrackCount();
155+
pauseForDeleteToComplete(testFile.getParentFile());
153156

154157
assertEquals(0, theInstance.getTrackCount());
155-
assertEquals(false, testFile.exists());
156-
assertEquals(false, testFile.getParentFile().exists());
158+
assertEquals(showFailures(), false, testFile.exists());
159+
assertEquals(showFailures(), false, testFile.getParentFile().exists());
157160
}
158161

159162
public void testFileCleanerNull() throws Exception {
@@ -237,9 +240,10 @@ public void testFileCleanerExitWhenFinished1() throws Exception {
237240
r = null;
238241

239242
waitUntilTrackCount();
243+
pauseForDeleteToComplete(new File(path));
240244

241245
assertEquals("10-Track Count", 0, theInstance.getTrackCount());
242-
assertEquals("11-testFile exists", false, new File(path).exists());
246+
assertEquals("11-testFile exists " + showFailures(), false, new File(path).exists());
243247
assertEquals("12-exitWhenFinished", true, theInstance.exitWhenFinished);
244248
assertEquals("13-reaper.isAlive", false, theInstance.reaper.isAlive());
245249
}
@@ -262,9 +266,10 @@ public void testFileCleanerExitWhenFinished2() throws Exception {
262266
r = null;
263267

264268
waitUntilTrackCount();
269+
pauseForDeleteToComplete(new File(path));
265270

266271
assertEquals(0, theInstance.getTrackCount());
267-
assertEquals(false, new File(path).exists());
272+
assertEquals(showFailures(), false, new File(path).exists());
268273
assertEquals(false, theInstance.exitWhenFinished);
269274
assertEquals(true, theInstance.reaper.isAlive());
270275

@@ -278,6 +283,16 @@ public void testFileCleanerExitWhenFinished2() throws Exception {
278283
}
279284

280285
//-----------------------------------------------------------------------
286+
private void pauseForDeleteToComplete(File file) throws Exception {
287+
int count = 0;
288+
while(file.exists() && count++ < 20) {
289+
Thread.sleep(500L);
290+
}
291+
}
292+
private String showFailures() throws Exception {
293+
return "Failed to delete " + theInstance.deleteFailures.size() + " files";
294+
}
295+
281296
private void waitUntilTrackCount() throws Exception {
282297
int count = 0;
283298
while(theInstance.getTrackCount() != 0 && count++ < 5) {

0 commit comments

Comments
 (0)