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

Crash in 3.0.0 when CapturableController is reused #224

Open
MiSikora opened this issue Oct 24, 2024 · 0 comments
Open

Crash in 3.0.0 when CapturableController is reused #224

MiSikora opened this issue Oct 24, 2024 · 0 comments

Comments

@MiSikora
Copy link

MiSikora commented Oct 24, 2024

I have a setup where controllers are kept in a higher-level node and then passed down to children that are inside a pager. With the 3.0.0 version, the code crashes when a controller is used again in the capturable() modifier due to attempting to consume a channel as a flow multiple times.

I believe it is due to this change: 931937c. It can be fixed by making the flow hot with .receiveAsFlow() instead as the channel wouldn't be subscribed to multiple times. And #202 is still fixed. Though, .receiveAsFlow() uses a fan-out algorithm so simultaneous .captureable() modifiers with the same controller would not receive requests. But at this point it is a misuse of APIs.

I have a a working fix (MiSikora@af82176) with a solution using .receiveAsFlow(). Let me know If I should make a PR.

Here's a simple reproducer. Open the app with this component added, swipe to the 2nd page, and go back to the 1st one.

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import dev.shreyaspatil.capturable.capturable
import dev.shreyaspatil.capturable.controller.rememberCaptureController

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun Screen() {
    val capturable0 = rememberCaptureController()
    val capturable1 = rememberCaptureController()
    HorizontalPager(
        state = rememberPagerState { 2 },
        modifier = Modifier.fillMaxSize(),
    ) { index ->
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .capturable(if (index == 0) capturable0 else capturable1)
                .fillMaxSize()
        ) {
            Text("$index")
        }
    }
}
java.lang.IllegalStateException: ReceiveChannel.consumeAsFlow can be collected just once
	at kotlinx.coroutines.flow.ChannelAsFlow.markConsumed(Channels.kt:101)
	at kotlinx.coroutines.flow.ChannelAsFlow.collect(Channels.kt:124)
	at kotlinx.coroutines.flow.FlowKt__CollectKt.emitAll(Collect.kt:105)
	at kotlinx.coroutines.flow.FlowKt.emitAll(Unknown Source:1)
	at dev.shreyaspatil.capturable.CapturableModifierNode$observeCaptureRequestsAndServe$$inlined$flatMapLatest$1.invokeSuspend(Merge.kt:189)
	at dev.shreyaspatil.capturable.CapturableModifierNode$observeCaptureRequestsAndServe$$inlined$flatMapLatest$1.invoke(Unknown Source:11)
	at dev.shreyaspatil.capturable.CapturableModifierNode$observeCaptureRequestsAndServe$$inlined$flatMapLatest$1.invoke(Unknown Source:4)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$2.invokeSuspend(Merge.kt:30)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$2.invoke(Unknown Source:8)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1$2.invoke(Unknown Source:4)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:27)
	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:90)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3$1.emit(Merge.kt:29)
	at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:396)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invokeSuspend(Merge.kt:23)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invoke(Unknown Source:8)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest$flowCollect$3.invoke(Unknown Source:4)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:61)
	at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:261)
	at kotlinx.coroutines.flow.internal.ChannelFlowTransformLatest.flowCollect(Merge.kt:21)
	at kotlinx.coroutines.flow.internal.ChannelFlowOperator.collectTo$suspendImpl(ChannelFlow.kt:153)
	at kotlinx.coroutines.flow.internal.ChannelFlowOperator.collectTo(Unknown Source:0)
	at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:56)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
	at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
	at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:68)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1404)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1415)
	at android.view.Choreographer.doCallbacks(Choreographer.java:1015)
	at android.view.Choreographer.doFrame(Choreographer.java:941)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1389)
	at android.os.Handler.handleCallback(Handler.java:959)
	at android.os.Handler.dispatchMessage(Handler.java:100)
	at android.os.Looper.loopOnce(Looper.java:232)
	at android.os.Looper.loop(Looper.java:317)
	at android.app.ActivityThread.main(ActivityThread.java:8592)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant