Skip to content

Commit 5506ecf

Browse files
i556354GoeLin
i556354
authored andcommitted
8254350: CompletableFuture.get may swallow InterruptedException
Backport-of: 43dc3f7
1 parent cc88f4c commit 5506ecf

File tree

3 files changed

+173
-8
lines changed

3 files changed

+173
-8
lines changed

src/java.base/share/classes/java/util/concurrent/CompletableFuture.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,8 @@ public boolean block() {
18071807
* interrupted.
18081808
*/
18091809
private Object waitingGet(boolean interruptible) {
1810+
if (interruptible && Thread.interrupted())
1811+
return null;
18101812
Signaller q = null;
18111813
boolean queued = false;
18121814
Object r;
@@ -1818,25 +1820,25 @@ private Object waitingGet(boolean interruptible) {
18181820
}
18191821
else if (!queued)
18201822
queued = tryPushStack(q);
1823+
else if (interruptible && q.interrupted) {
1824+
q.thread = null;
1825+
cleanStack();
1826+
return null;
1827+
}
18211828
else {
18221829
try {
18231830
ForkJoinPool.managedBlock(q);
18241831
} catch (InterruptedException ie) { // currently cannot happen
18251832
q.interrupted = true;
18261833
}
1827-
if (q.interrupted && interruptible)
1828-
break;
18291834
}
18301835
}
1831-
if (q != null && queued) {
1836+
if (q != null) {
18321837
q.thread = null;
1833-
if (!interruptible && q.interrupted)
1838+
if (q.interrupted)
18341839
Thread.currentThread().interrupt();
1835-
if (r == null)
1836-
cleanStack();
18371840
}
1838-
if (r != null || (r = result) != null)
1839-
postComplete();
1841+
postComplete();
18401842
return r;
18411843
}
18421844

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.ForkJoinPool;
26+
import java.util.concurrent.ThreadLocalRandom;
27+
import static java.util.concurrent.TimeUnit.DAYS;
28+
29+
/*
30+
* @test
31+
* @bug 8254350
32+
* @run main LostInterrupt
33+
* @summary CompletableFuture.get may swallow interrupt status
34+
* @key randomness
35+
*/
36+
37+
// TODO: Rewrite as a CompletableFuture tck test ?
38+
39+
/**
40+
* Submits a task that completes immediately, then invokes CompletableFuture.get
41+
* with the interrupt status set. CompletableFuture.get should either complete
42+
* immediately with the interrupt status set, or else throw InterruptedException
43+
* with the interrupt status cleared.
44+
*/
45+
public class LostInterrupt {
46+
static final int ITERATIONS = 10_000;
47+
48+
public static void main(String[] args) throws Exception {
49+
ThreadLocalRandom rnd = ThreadLocalRandom.current();
50+
ForkJoinPool executor = new ForkJoinPool(1);
51+
try {
52+
for (int i = 0; i < ITERATIONS; i++) {
53+
CompletableFuture<String> future = new CompletableFuture<>();
54+
boolean timed = rnd.nextBoolean();
55+
executor.execute(() -> future.complete("foo"));
56+
57+
Thread.currentThread().interrupt();
58+
try {
59+
String result = timed ? future.get(1, DAYS) : future.get();
60+
61+
if (!Thread.interrupted())
62+
throw new AssertionError("lost interrupt, run=" + i);
63+
} catch (InterruptedException expected) {
64+
if (Thread.interrupted())
65+
throw new AssertionError(
66+
"interrupt status not cleared, run=" + i);
67+
}
68+
}
69+
} finally {
70+
executor.shutdown();
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.CountDownLatch;
26+
import java.util.concurrent.ThreadLocalRandom;
27+
import java.util.concurrent.atomic.AtomicReference;
28+
29+
/*
30+
* @test
31+
* @bug 8254350
32+
* @run main SwallowedInterruptedException
33+
* @key randomness
34+
*/
35+
36+
public class SwallowedInterruptedException {
37+
static final int ITERATIONS = 100;
38+
39+
public static void main(String[] args) throws Throwable {
40+
for (int i = 1; i <= ITERATIONS; i++) {
41+
System.out.format("Iteration %d%n", i);
42+
43+
CompletableFuture<Void> future = new CompletableFuture<>();
44+
CountDownLatch running = new CountDownLatch(1);
45+
AtomicReference<String> failed = new AtomicReference<>();
46+
47+
Thread thread = new Thread(() -> {
48+
// signal main thread that child is running
49+
running.countDown();
50+
51+
// invoke Future.get, it complete with the interrupt status set or
52+
// else throw InterruptedException with the interrupt status not set.
53+
try {
54+
future.get();
55+
56+
// interrupt status should be set
57+
if (!Thread.currentThread().isInterrupted()) {
58+
failed.set("Future.get completed with interrupt status not set");
59+
}
60+
} catch (InterruptedException ex) {
61+
// interrupt status should be cleared
62+
if (Thread.currentThread().isInterrupted()) {
63+
failed.set("InterruptedException with interrupt status set");
64+
}
65+
} catch (Throwable ex) {
66+
failed.set("Unexpected exception " + ex);
67+
}
68+
});
69+
thread.setDaemon(true);
70+
thread.start();
71+
72+
// wait for thread to run
73+
running.await();
74+
75+
// interrupt thread and set result after an optional (random) delay
76+
thread.interrupt();
77+
long sleepMillis = ThreadLocalRandom.current().nextLong(10);
78+
if (sleepMillis > 0)
79+
Thread.sleep(sleepMillis);
80+
future.complete(null);
81+
82+
// wait for thread to terminate and check for failure
83+
thread.join();
84+
String failedReason = failed.get();
85+
if (failedReason != null) {
86+
throw new RuntimeException("Test failed: " + failedReason);
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)