Comments (16)
I now realize the Kotlin cancellation is synchronous, and any suspend functions called during the catch (e: CancellationException) { ... }
will not really be executed when the job is cancelled.
Any other solutions to this situation? I can think of 2:
- Block the thread on cancellation (seems bad):
suspend fun waitForEvent(): Unit = suspendCancellableCoroutine { continuation ->
setWaitForEventCallback { continuation.resume(Unit) }
continuation.invokeOnCancellation {
runBlocking { clearWaitForEventCallback().await() } // Block the thread that cancels.
}
}
- Have a lock that ensures synchronization with the next call (seems error prone):
val cancellationLock = Mutex(false)
suspend fun waitForEvent() {
lock.withLock {} // Wait for previous cancellation.
suspendCancellableCoroutine { continuation ->
setWaitForEventCallback { continuation.resume(Unit) }
cancellationLock.lock() // Lock only after setWaitForEventCallback.
continuation.invokeOnCancellation {
clearWaitForEventCallback().addListener({ cancellationLock.tryUnlock() }, <Executor not from this Job, which may have been cancelled>)
}
}
cancellationLock.tryUnlock() // Finished successfully.
}
fun Mutex.tryUnlock() {
try { lock.unlick() }
catch (e: IllegalStateException) {}
}
from kotlinx.coroutines.
Related Issues (20)
- BlockHound false positive in kotlin.jvm.internal.Reflection.renderLambdaToString HOT 3
- java.lang.NullPointerException: Cannot invoke "kotlinx.coroutines.flow.Flow.collect when bumping up coroutines 1.6.4 to 1.8.1 HOT 3
- Improve the API reference HOT 3
- Coroutine on EventLoop dispatcher fails to yield to a task which is resuming after a delay HOT 5
- TestScope swallows an exception thrown from `launch` outside `runTest` HOT 4
- Exceptions being swallowed during tests / UncaughtExceptionsBeforeTest HOT 5
- 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 1
- 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
- ActorCoroutine with ConflatedBufferedChannel capactiy HOT 1
- Calling CompletionStage.asDeferred() on the same future many times causes stack overflows HOT 3
- [Performance impacting] Upgrade Android Build rules to use standard android library Gradle plugin
- Flow.catch skips upstream exceptions HOT 3
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.