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

SharedFlow random lose message when emit in cancelled coroutine #3774

Open
wxbsocial opened this issue Jun 7, 2023 · 0 comments
Open

SharedFlow random lose message when emit in cancelled coroutine #3774

wxbsocial opened this issue Jun 7, 2023 · 0 comments

Comments

@wxbsocial
Copy link

wxbsocial commented Jun 7, 2023

Hello everyone, I encountered this problem in the project, in order to highlight the problem, the following is the simplified code

Kotlin: 1.8.21
excepted: recv:100,emit:100
but: recv:76,emit:100

@Test
fun shared_flow_random_lose_message_when_emit_in_cancelled_coroutine(): Unit = runBlocking {
    val signalBus = MutableSharedFlow<Int>()

    val scope = CoroutineScope(Executors.newSingleThreadExecutor().asCoroutineDispatcher())

    var oneTimes = 0
    var emitOneTimes = 0

    signalBus.onEach {
        when (it) {
            1 -> oneTimes++
            else -> {}
        }

    }.stateIn(scope, SharingStarted.Eagerly, 0)

    delay(2_000) // wait stateIn inited


    repeat(100) {
        val job = foo(scope, signalBus)
        delay(100) // wait a moment
        job.cancelAndJoin()
        signalBus.emit(1)
        emitOneTimes++
    }

    delay(1_000)
    Log.d(TAG, "recv:$oneTimes,emit:$emitOneTimes")

    assertEquals(100, oneTimes) // failed < 100
    assertEquals(100, emitOneTimes)
}


private suspend fun foo(scope: CoroutineScope, shared: MutableSharedFlow<Int>): Job {

    return scope.launch {
        try {
            delay(10_000)

        } catch (_: CancellationException) {
            shared.emit(0) // throw ex, (0) not emit,and cause random loss of messages (1) that emit by signalBus.emit(1) after called foo()
        }
    }
}
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