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

rx Flowable that use Unconfined dispatcher result in a deadlock when using a runblocking() that execute a suspend function with (Semaphore, mutex or ReentrantLock) #4222

Open
cchaouachi opened this issue Sep 9, 2024 · 1 comment

Comments

@cchaouachi
Copy link

cchaouachi commented Sep 9, 2024

Hello,

This code result in a deadlock :

sharedFlow.asFlowable(Unconfined)
            .flatMap {
 runBlocking {
            suspend function with (Mutex , Semaphore or  ReentrantLock) --> This code deadlocks here
        }
}

when we change the Unconfined dispatcher to the Default the problem disappear?
is it the intended behavior ?
I found a similar issue but with blockingGet

@cchaouachi cchaouachi added the bug label Sep 9, 2024
@dkhalanskyjb dkhalanskyjb added reactive and removed bug labels Sep 9, 2024
@dkhalanskyjb
Copy link
Collaborator

Could you provide a complete reproducer? When I write the following, it works the same for Unconfined and Default:

        val sharedFlow = MutableSharedFlow<Int>()
        val mutex = Mutex()
        GlobalScope.launch {
            delay(100)
            repeat(5) {
                sharedFlow.emit(it)
            }
        }
        sharedFlow.asFlowable(Dispatchers.Unconfined)
            .flatMap<Int> {
                runBlocking {
                    mutex.withLock {
                        flow {
                            emit(it)
                        }.asFlowable()
                    }
                }
            }
            .blockingForEach {
                println("Got $it")
            }

It also works if I move withLock inside the inner flow.

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

2 participants