Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk16 Public archive

Commit 43dc3f7

Browse files
author
Martin Buchholz
committed
8254350: CompletableFuture.get may swallow InterruptedException
Reviewed-by: alanb, dl
1 parent 6d79ec8 commit 43dc3f7

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,8 @@ public boolean block() {
18711871
* interrupted.
18721872
*/
18731873
private Object waitingGet(boolean interruptible) {
1874+
if (interruptible && Thread.interrupted())
1875+
return null;
18741876
Signaller q = null;
18751877
boolean queued = false;
18761878
Object r;
@@ -1882,25 +1884,25 @@ private Object waitingGet(boolean interruptible) {
18821884
}
18831885
else if (!queued)
18841886
queued = tryPushStack(q);
1887+
else if (interruptible && q.interrupted) {
1888+
q.thread = null;
1889+
cleanStack();
1890+
return null;
1891+
}
18851892
else {
18861893
try {
18871894
ForkJoinPool.managedBlock(q);
18881895
} catch (InterruptedException ie) { // currently cannot happen
18891896
q.interrupted = true;
18901897
}
1891-
if (q.interrupted && interruptible)
1892-
break;
18931898
}
18941899
}
1895-
if (q != null && queued) {
1900+
if (q != null) {
18961901
q.thread = null;
1897-
if (!interruptible && q.interrupted)
1902+
if (q.interrupted)
18981903
Thread.currentThread().interrupt();
1899-
if (r == null)
1900-
cleanStack();
19011904
}
1902-
if (r != null || (r = result) != null)
1903-
postComplete();
1905+
postComplete();
19041906
return r;
19051907
}
19061908

Lines changed: 73 additions & 0 deletions
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+
}
Lines changed: 90 additions & 0 deletions
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)