-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parent coroutine is frozen when child coroutine on background thread is launched #1745
Comments
Adding the below test to MainDispatcherTest under nativeDarwin shows what is problematic:
|
Can you, please, give more details on how freezing this freezing affects your code. The test you've posted, as far as I can see, behaves correctly. You see, You have the following comment in your issue:
However, I cannot reproduce the second part of your problem that "any other object referenced in this lambda is now frozen as well". To be sure, I've created the following test and it runs just fine:
|
The test you wrote above works. If I change it slightly, then the test hangs:
If I run the above on the simulator and replace |
@brendanw This test hangs because you cannot do |
I will work on trying to reproduce the case I am seeing in my app in a test; if I cannot reproduce in a test I will close this issue. For reference, I am using https://github.com/freeletics/CoRedux to build a class |
I thought that the trailing lambda parameter passed to If this true, then the Please help me understand the flaw in my logic. |
Kotlin lambdas don't keep a reference to their enclosing lambdas. They only capture ("close over") objects from the outer lambdas that are explicitly referenced in their code. So, in the
Then, |
@brendanw See this comment: #1648 (comment) |
@Test
fun testParentIsFrozen() = runTest {
// create a mutable object referenced by this lambda
val mutable = mutableListOf<Int>()
val mutable2 = mutableListOf<Int>()
// run a child coroutine in another thread
val result = withContext(io) {
mutable2.add(1)
}
// ensure that objects referenced by this lambda were not frozen
assertFalse(mutable.isFrozen)
mutable.add(42) // just to be 100% sure
} @elizarov If I modify the test to the above, the test fails. Is this expected? |
@brendanw This test is expected to fail because Does it address your question? |
Ah, duh. Yup that makes sense. |
coroutines_version=1.3.3-native-mt
kotlin_version=1.3.61
expected result: parent coroutine should not be frozen on native.
actual result: parent coroutine is frozen on native; makes it impossible to dispatch
result
to any listeners without using atomics everywhere to prevent mutability exceptions.When I am running something akin to this in my project I am actually seeing someBackgroundSuspend() never complete when I have the ensureNeverFrozen call beforehand.
The text was updated successfully, but these errors were encountered: