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 stops emitting values #2320

Closed
AndreyArapov opened this issue Oct 20, 2020 · 1 comment
Closed

SharedFlow stops emitting values #2320

AndreyArapov opened this issue Oct 20, 2020 · 1 comment
Assignees

Comments

@AndreyArapov
Copy link

If I use the replay parameter in the MutableSharedFlow function, then at some point in time the flow stops emitting values. The problem is reproducible if subscribers process values with different delays. But if the delays are equal, then it works correctly.

Сode:

private val state = MutableSharedFlow<Int>(1)

fun main() {
    val delay1: Long = 100
    val delay2: Long = 400
    state
        .onEach {
            delay(delay1)
            println("item1 = $it")
        }.launchIn(GlobalScope)

    state
        .onEach {
            delay(delay2)
            println("item2 = $it")
        }.launchIn(GlobalScope)


    val job = GlobalScope.launch(Dispatchers.IO) {
        delay(1.seconds)
        for (i in 0..5) {
            state.emit(i)
            println("emit = $i")
            delay(100)
        }
    }

    runBlocking {
        delay(10.seconds)
        println("Job = $job")
    }
}

Result:

emit = 0
emit = 1
value1 = 0
value1 = 1
value2 = 0
emit = 2
value2 = 1
value2 = 2
Job = "coroutine#3":StandaloneCoroutine{Active}
@elizarov
Copy link
Contributor

Thanks a lot for the report! Will be fixed.

elizarov added a commit that referenced this issue Oct 21, 2020
Problematic scenario:
* Emitter suspends because there is a slow subscriber
* Fast subscriber collects all the values and suspend
* Slow subscriber resumes, collects value, causes emitter to be resume
* Fast subscribers must be resumed in this case, too

Fixes #2320
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