Skip to content

Commit 6116009

Browse files
committed
Deprecate CancellableContinuation.cancelFutureOnCancellation
1 parent 6232da0 commit 6116009

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

kotlinx-coroutines-core/jvm/src/Executors.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package kotlinx.coroutines
22

3-
import kotlinx.coroutines.flow.*
43
import kotlinx.coroutines.internal.*
54
import java.io.Closeable
65
import java.util.concurrent.*
@@ -145,7 +144,7 @@ internal class ExecutorCoroutineDispatcherImpl(override val executor: Executor)
145144
)
146145
// If everything went fine and the scheduling attempt was not rejected -- use it
147146
if (future != null) {
148-
continuation.cancelFutureOnCancellation(future)
147+
continuation.invokeOnCancellation(CancelFutureOnCancel(future))
149148
return
150149
}
151150
// Otherwise fallback to default executor
@@ -201,3 +200,12 @@ private class DisposableFutureHandle(private val future: Future<*>) : Disposable
201200
}
202201
override fun toString(): String = "DisposableFutureHandle[$future]"
203202
}
203+
204+
private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler {
205+
override fun invoke(cause: Throwable?) {
206+
// Don't interrupt when cancelling future on completion, because no one is going to reset this
207+
// interruption flag and it will cause spurious failures elsewhere
208+
if (cause != null) future.cancel(false)
209+
}
210+
override fun toString() = "CancelFutureOnCancel[$future]"
211+
}

kotlinx-coroutines-core/jvm/src/Future.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ import java.util.concurrent.*
1212
* invokeOnCancellation { if (it != null) future.cancel(false) }
1313
* ```
1414
*/
15+
@Deprecated(
16+
"This function does not do what its name implies: it will not cancel the future if just cancel() was called.",
17+
level = DeprecationLevel.WARNING,
18+
replaceWith = ReplaceWith("this.invokeOnCancellation { future.cancel(false) }")
19+
)
1520
public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>): Unit =
16-
invokeOnCancellation(handler = CancelFutureOnCancel(future))
21+
invokeOnCancellation(handler = PublicCancelFutureOnCancel(future))
1722

18-
private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler {
23+
private class PublicCancelFutureOnCancel(private val future: Future<*>) : CancelHandler {
1924
override fun invoke(cause: Throwable?) {
2025
// Don't interrupt when cancelling future on completion, because no one is going to reset this
2126
// interruption flag and it will cause spurious failures elsewhere

0 commit comments

Comments
 (0)