You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Propagate cancellation on awaiting Completable and Listenable futures, provide ListenableFuture.asDeferred
Rationale: in most common use-cases awaiting the future is required to integrate with future-based API, current coroutine is the only user of this future and after its cancellation this future result is no longer needed. For non-cancelling await future.asDeferred().await() should be used
Fixes#515
Copy file name to clipboardExpand all lines: binary-compatibility-validator/reference-public-api/kotlinx-coroutines-guava.txt
+1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
public final class kotlinx/coroutines/experimental/guava/ListenableFutureKt {
2
+
public static final fun asDeferred (Lcom/google/common/util/concurrent/ListenableFuture;)Lkotlinx/coroutines/experimental/Deferred;
2
3
public static final fun asListenableFuture (Lkotlinx/coroutines/experimental/Deferred;)Lcom/google/common/util/concurrent/ListenableFuture;
3
4
public static final fun await (Lcom/google/common/util/concurrent/ListenableFuture;Lkotlin/coroutines/experimental/Continuation;)Ljava/lang/Object;
4
5
public static final fun future (Lkotlin/coroutines/experimental/CoroutineContext;Lkotlinx/coroutines/experimental/CoroutineStart;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lcom/google/common/util/concurrent/ListenableFuture;
Copy file name to clipboardExpand all lines: integration/kotlinx-coroutines-jdk8/src/future/Future.kt
+5-6
Original file line number
Diff line number
Diff line change
@@ -190,11 +190,8 @@ public suspend fun <T> CompletableFuture<T>.await(): T =
190
190
* This suspending function is cancellable.
191
191
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
192
192
* stops waiting for the completion stage and immediately resumes with [CancellationException][kotlinx.coroutines.experimental.CancellationException].
193
-
*
194
-
* Note, that `CompletionStage` implementation does not support prompt removal of installed listeners, so on cancellation of this wait
195
-
* a few small objects will remain in the `CompletionStage` stack of completion actions until it completes itself.
196
-
* However, the care is taken to clear the reference to the waiting coroutine itself, so that its memory can be
197
-
* released even if the completion stage never completes.
193
+
* This method is intended to be used with one-shot futures, so on coroutine cancellation completion stage is cancelled as well if it is instance of [CompletableFuture].
194
+
* If cancelling given stage is undesired, `stage.asDeferred().await()` should be used instead.
198
195
*/
199
196
publicsuspendfun <T> CompletionStage<T>.await(): T {
200
197
// fast path when CompletableFuture is already done (does not suspend)
@@ -211,7 +208,9 @@ public suspend fun <T> CompletionStage<T>.await(): T {
211
208
val consumer =ContinuationConsumer(cont)
212
209
whenComplete(consumer)
213
210
cont.invokeOnCancellation {
214
-
consumer.cont =null// shall clear reference to continuation
211
+
// mayInterruptIfRunning is not used
212
+
(thisas?CompletableFuture<T>)?.cancel(false)
213
+
consumer.cont =null// shall clear reference to continuation to aid GC
0 commit comments