Skip to content

Commit 2bbdd4e

Browse files
committed
IO-488 FileUtils.waitFor(...) swallows thread interrupted status
Patch by Björn Buchner, testcase by me git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1686477 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0bfb0f2 commit 2bbdd4e

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,19 +1706,26 @@ private static File[] verifiedListFiles(File directory) throws IOException {
17061706
public static boolean waitFor(final File file, final int seconds) {
17071707
int timeout = 0;
17081708
int tick = 0;
1709-
while (!file.exists()) {
1710-
if (tick++ >= 10) {
1711-
tick = 0;
1712-
if (timeout++ > seconds) {
1713-
return false;
1709+
boolean wasInterrupted = false;
1710+
try {
1711+
while (!file.exists()) {
1712+
if (tick++ >= 10) {
1713+
tick = 0;
1714+
if (timeout++ > seconds) {
1715+
return false;
1716+
}
1717+
}
1718+
try {
1719+
Thread.sleep(100);
1720+
} catch (final InterruptedException ignore) {
1721+
wasInterrupted = true;
1722+
} catch (final Exception ex) {
1723+
break;
17141724
}
17151725
}
1716-
try {
1717-
Thread.sleep(100);
1718-
} catch (final InterruptedException ignore) {
1719-
// ignore exception
1720-
} catch (final Exception ex) {
1721-
break;
1726+
} finally {
1727+
if (wasInterrupted) {
1728+
Thread.currentThread().interrupt();
17221729
}
17231730
}
17241731
return true;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.commons.io;
1818

1919
import java.io.File;
20+
import java.util.concurrent.CountDownLatch;
21+
import java.util.concurrent.atomic.AtomicBoolean;
2022

2123
import org.apache.commons.io.testtools.FileBasedTestCase;
2224

@@ -52,4 +54,23 @@ public void testWaitFor() {
5254
FileUtils.waitFor(new File(""), 2);
5355
}
5456

57+
public void testWaitForInterrupted() throws InterruptedException {
58+
final AtomicBoolean wasInterrupted = new AtomicBoolean(false);
59+
final CountDownLatch started = new CountDownLatch(1);
60+
Runnable thread = new Runnable() {
61+
@Override
62+
public void run() {
63+
started.countDown();
64+
FileUtils.waitFor(new File(""), 2);
65+
wasInterrupted.set( Thread.currentThread().isInterrupted());
66+
}
67+
};
68+
Thread thread1 = new Thread(thread);
69+
thread1.start();
70+
started.await();
71+
thread1.interrupt();
72+
thread1.join();
73+
assertTrue( wasInterrupted.get() );
74+
}
75+
5576
}

0 commit comments

Comments
 (0)