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
The supervisorScope is designed to prevent child coroutine failures from propagating up and crashing the parent. However, when a child coroutine is launched within a withContext block inside a supervisorScope, the expected exception handling behavior is broken.
The withContext block, in this scenario, behaves as if it creates a new coroutine scope, isolating the child coroutine from the supervisorScope's supervision.
Expected Behavior:
When an exception occurs within a child coroutine launched inside a withContext block that resides within a supervisorScope, the supervisorScope should catch the exception and prevent it from propagating further up the hierarchy. The supervisorScope and its parent coroutines should remain active.
Actual Behavior:
Instead of being handled by the supervisorScope, the exception propagates up past the supervisorScope and crashes the parent coroutine or even the runBlocking scope. This effectively bypasses the supervisorScope's exception handling mechanism, rendering it useless in this specific scenario.
Code Example:
importkotlinx.coroutines.*funmain() = runBlocking {
println("start")
supervisorScope { // SupervisorScope started
withContext(this.coroutineContext) { // withContext creates an UndispatchedCoroutine, child of SupervisorScope
launch { // Child coroutine of UndispatchedCoroutinethrowException("Error in child coroutine")
}
} // withContext block ends
} // supervisorScope block ends (exception should be handled here, but propagates to runBlocking)println("end") // This line is not reached
}
The text was updated successfully, but these errors were encountered:
Describe the bug
The supervisorScope is designed to prevent child coroutine failures from propagating up and crashing the parent. However, when a child coroutine is launched within a withContext block inside a supervisorScope, the expected exception handling behavior is broken.
The withContext block, in this scenario, behaves as if it creates a new coroutine scope, isolating the child coroutine from the supervisorScope's supervision.
Expected Behavior:
When an exception occurs within a child coroutine launched inside a withContext block that resides within a supervisorScope, the supervisorScope should catch the exception and prevent it from propagating further up the hierarchy. The supervisorScope and its parent coroutines should remain active.
Actual Behavior:
Instead of being handled by the supervisorScope, the exception propagates up past the supervisorScope and crashes the parent coroutine or even the runBlocking scope. This effectively bypasses the supervisorScope's exception handling mechanism, rendering it useless in this specific scenario.
Code Example:
The text was updated successfully, but these errors were encountered: