Comments (3)
Root cause
The root cause seems to be a bad interaction between CompletionStage.asDeferred
and CompletableFuture
. Specifically:
- When
future
completes, it calls theCompletionStage.handle
callback at . - The callback calls
result.completeExceptionally
. - Completion of
result
triggerscancelFutureOnCompletion
at . CompletableFuture.cancel
triggerspostComplete
at https://github.com/openjdk/jdk/blob/jdk-21%2B26/src/java.base/share/classes/java/util/concurrent/CompletableFuture.java#L2512, which in turn runs (1) for the nextDeferred
, recursively.
Normally CompletableFuture runs all the callbacks iteratively, but due to triggering the cancel()
function in a recursive context, it starts running callbacks recursively, and causes a stack overflow.
from kotlinx.coroutines.
Workaround
The above issue can be worked around by breaking the recursive CompletableFuture.cancel
call. For example:
fun <T> CompletableFuture<T>.safeAsDeferred(): Deferred<T> {
val parent = this
val safeCompletableFuture = object : CompletableFuture<T>() {
override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
return if (!parent.isDone) {
parent.cancel(mayInterruptIfRunning)
} else {
parent.isCancelled
}
}
}
parent.handle {
result, exception ->
if (exception == null) {
safeCompletableFuture.complete(result)
} else {
safeCompletableFuture.completeExceptionally(exception)
}
}
return safeCompletableFuture.asDeferred()
}
When used in place of asDeferred
in the reproducer above, this fixes the issue:
$ JAVA_HOME=/opt/homebrew/Cellar/openjdk@21/21.0.3/libexec/openjdk.jdk/Contents/Home kotlin test.main.kts 10000
Expected additional transformation to run. Actual: true
Expected 10000 Deferred<Unit> to complete exceptionally. Actual: 10000
And if you don't care about cancelling the Deferred
, you can simply create another chained CompletableFuture
, since apparently CompletableFuture
cancellations doesn't propagate upstream:
future.thenApply { it }.asDeferred()
from kotlinx.coroutines.
Great job figuring out the problem!
I think this is worth reporting to the JDK maintainers as well. As far as I can see, nothing in the docs indicates that this may happen. Though nothing says that it won't, I think:
- https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/CompletableFuture.html#cancel(boolean) says that a successful cancellation will complete the dependents as well.
- https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/CompletableFuture.html#handle(java.util.function.BiFunction) says that it creates a dependent future that will synchronously invoke the handler in the current thread.
- The top-level docs say: "When two or more threads attempt to complete, completeExceptionally, or cancel a CompletableFuture, only one of them succeeds." It doesn't say what happens when one thread calls
cancel
and then callscancel
again before the first one finishes.
I think this should be fixed on the side of the JDK as well by detecting the same thread entering the cancellation procedure more than once. That said, we can work around this explicitly on our side: #4173 The downside of this workaround is that when several threads cancel coroutines related to one future, only one of them will deal with the work related to the cancellation now (as opposed to many threads sharing the load), but I don't think that's a significant issue.
from kotlinx.coroutines.
Related Issues (20)
- Flow collection silently hangs when dispatcher throws an exception
- Suggestion for a potential new Flow's `timeout` extension HOT 3
- Mutex is unlocked on cancellation HOT 2
- Crash on GraalVM at `1.9.0-RC` HOT 11
- Non-linearizable behavior in `cancel` + `awaitClose` inside of `produce` HOT 1
- Inherit from `kotlinx.coroutines` warning even with interface delegation ? HOT 1
- Consider stabilizing `CoroutineStart.ATOMIC` HOT 2
- 1.9.0-RC: `kotlinx-coroutines-core/jvm/src/internal/ProbesSupport.kt` calls ` kotlinx.coroutines.debug.internal.probeCoroutineResumed` HOT 1
- 1.9.0-RC: DispatchersToStringTest.testLimitedParallelism fails when CORE_POOL_SIZE == 2 HOT 3
- CancellableContinuation.invokeOnCancellation should accept a suspend callback HOT 16
- ActorCoroutine with ConflatedBufferedChannel capactiy HOT 1
- [Performance impacting] Upgrade Android Build rules to use standard android library Gradle plugin
- Flow.catch skips upstream exceptions HOT 7
- 1.9.0-RC: `kotlinx-coroutines-core/concurrent/src/...` contains common (expect) and platform (actual) files HOT 3
- TextMeasurer.measure only works in Dispatchers.Main on ios HOT 1
- stateFlow unable to receive updated values ββusing collect HOT 4
- StateFlow, different values, compareAndSet return false HOT 3
- 1.9.0-RC test task:compileDemoDebugUnitTestKotlin error Suspension functions can only be called within coroutine body. HOT 1
- Improve invokeOnCompletion and invokeOnCancellation API HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.coroutines.