MobileNativeFoundation / Store

A Kotlin Multiplatform library for building network-resilient applications
https://mobilenativefoundation.github.io/Store/
Apache License 2.0
3.16k stars 197 forks source link

[BUG] Calling 'fresh' under heavy load causes the upstream to close unexpectedly #636

Closed lukisk closed 3 months ago

lukisk commented 3 months ago

Describe the bug Occasionally, the 'main stream' closes unexpectedly.

To Reproduce Steps to reproduce the behavior:

val scope = CoroutineScope(Dispatchers.Default)

val store = StoreBuilder.from<Int, String>(Fetcher.of { key ->
    delay(10) //simulate long network request
    "$key :: " + Random.nextInt()
}).build()

scope.launch {
    store.stream(StoreReadRequest.cached(1, false)) //main stream
        .onCompletion {
            println("Unexpected !!!" + it)
        }
        .collect {
            println("result" + it)
    }
}

(1 .. 50).onEach  {
    scope.launch {
        while (isActive) {
            store.stream(StoreReadRequest.fresh(1))
                .filterNot { it is StoreReadResponse.Loading }
                .firstOrNull()
            delay(150)
        }
    }
}

Expected behavior Calling 'fresh' does not cause the 'main stream' to close

Smartphone (please complete the following information):