Skip to content
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

[native-mt] runBlocking in Unit test with 1.3.9-native-mt or 1.3.9-native-mt-2 never exits #2335

Closed
werner77 opened this issue Oct 25, 2020 · 2 comments
Assignees

Comments

@werner77
Copy link

Consider the very simple unit test:

@Test
    fun coroutines() {
        runBlocking {
            withContext(Dispatchers.Main) {
                println("1")
                withContext(Dispatchers.Main) {
                    println("2")
                }
                println("3")
            }
            println("4")
        }
        println("5")
    }

This test runs and finishes fine on JVM or native with the main (non-mt) version, but with the mt version it hangs indefinitely.

@ntherning
Copy link

I have seen this deadlock too on iOS. I believe the problem with runBlocking() is that it is blocking the main thread without letting iOS process any blocks (coroutines) enqueued on the native GDC main queue. On iOS Dispatchers.Main uses this queue to asynchronously run coroutines.

I have experimented with making my own version of runBlocking() for my tests which uses NSRunLoop.mainRunLoop.runUntilDate(...) to give GDC a chance to process the queued blocks. It works, coroutines are run, but my understanding of coroutine internals is not deep enough to make it reliable (e.g. waiting for all descendant jobs to complete).

I see that there's a concept of an EventLoop in the coroutines internals which I don't fully understand. Perhaps a custom EventLoop impl for the iOS main thread could make runBlocking() pump the main thread run loop as needed and prevent the deadlocking?

@qwwdfsad
Copy link
Member

qwwdfsad commented Aug 8, 2022

native-mt is no longer supported and it is recommended to use regular releases of coroutines along with new memory model

@qwwdfsad qwwdfsad closed this as completed Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants