Skip to content

Commit 8fb0064

Browse files
author
Paul Sandoz
committed
8029164: Race condition in CompletableFuture.thenCompose with asynchronous task
Reviewed-by: dl, chegar, mduigou
1 parent 7d90d5b commit 8fb0064

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ else if (UNSAFE.compareAndSwapObject
20042004
}
20052005
if (dst == null)
20062006
dst = new CompletableFuture<U>();
2007-
if (e == null || ex != null)
2007+
if (ex != null)
20082008
dst.internalComplete(null, ex);
20092009
}
20102010
helpPostComplete();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2013, 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 org.testng.Assert;
25+
import org.testng.annotations.Test;
26+
27+
import java.util.concurrent.CompletableFuture;
28+
import java.util.concurrent.CountDownLatch;
29+
30+
31+
/**
32+
* @test
33+
* @bug 8029164
34+
* @run testng ThenComposeAsyncTest
35+
* @run testng/othervm -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 ThenComposeAsyncTest
36+
* @summary Test that CompletableFuture.thenCompose works correctly if the
37+
* composing task is complete before composition
38+
*/
39+
@Test
40+
public class ThenComposeAsyncTest {
41+
42+
public void testThenComposeAsync() throws Exception {
43+
// Composing CompletableFuture is complete
44+
CompletableFuture<String> cf1 = CompletableFuture.completedFuture("one");
45+
46+
// Composing function returns a CompletableFuture executed asynchronously
47+
CountDownLatch cdl = new CountDownLatch(1);
48+
CompletableFuture<String> cf2 = cf1.thenCompose(str -> CompletableFuture.supplyAsync(() -> {
49+
while (true) {
50+
try {
51+
cdl.await();
52+
break;
53+
}
54+
catch (InterruptedException e) {
55+
}
56+
}
57+
return str + ", two";
58+
}));
59+
60+
// Ensure returned CompletableFuture completes after call to thenCompose
61+
// This guarantees that any premature internal completion will be
62+
// detected
63+
cdl.countDown();
64+
65+
String val = cf2.get();
66+
Assert.assertNotNull(val);
67+
Assert.assertEquals(val, "one, two");
68+
}
69+
}

0 commit comments

Comments
 (0)