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

Help to understand behaviour in coroutine native-mt #2539

Closed
francismariano opened this issue Feb 16, 2021 · 1 comment
Closed

Help to understand behaviour in coroutine native-mt #2539

francismariano opened this issue Feb 16, 2021 · 1 comment

Comments

@francismariano
Copy link

francismariano commented Feb 16, 2021

Hello guys.

I have the following object

object SharedFlowRepository {

    init {
        println("SharedFlowRepository - init")
    }

    private val _devices = MutableSharedFlow<Int>()
    val devices = _devices.asSharedFlow()

    suspend fun startScanLeDevice() {
        println("SharedFlowRepository - startScanLeDevice")
        withContext(Dispatchers.Default) {
            println("SharedFlowRepository - startScanLeDevice:launch")
            testeFlow().collect {
                println("SharedFlowRepository - startScanLeDevice:collect = $it")
                _devices.emit(it)
            }
        }
    }

    fun stopScanLeDevice() {
        println("SharedFlowRepository - stopScanLeDevice")
    }

    private fun testeFlow() = flow {
        for (i in 1..5) {
            delay(1000)
            println("presenter - testeFlow:emitting = $i")
            emit(i)
        }
    }
}

And the following class

class SharedFlowPresenter {

    private val mainScope = MainScope()
    private var scan: Job? = null

    init {
        println("SharedFlowPresenter - SharedFlowPresenter:init")
        ensureNeverFrozen()
        deviceScanDevice()
        stopScanLeDevice()      // LINE 1
    }

    private val repository = SharedFlowRepository

    private fun stopScanLeDevice() {
        repository.stopScanLeDevice()
    }

    private fun deviceScanDevice() {
        println("SharedFlowPresenter - deviceScanDevice")
        scan = mainScope.launch {
            println("SharedFlowPresenter - deviceScanDevice:launch")
            try {
                println("SharedFlowPresenter - deviceScanDevice:try")
                withTimeout(20000) {
                    println("SharedFlowPresenter - deviceScanDevice:withTimeout:withTimeout")
                    repository.devices.collect {
                        println("SharedFlowPresenter - deviceScanDevice:withTimeout:collect INT ====== $it")
                    }
                }
            } catch (e: TimeoutCancellationException) {
                println("SharedFlowPresenter - deviceScanDevice:catch : ${e.message}")
            }
        }.apply {
            invokeOnCompletion {
                println("SharedFlowPresenter - deviceScanDevice:invokeOnCompletation")
//                stopScanLeDevice()            // LINE 2
            }
        }
        mainScope.launch {
            println("SharedFlowPresenter - deviceScanDevice:startScanLeDevice")
            repository.startScanLeDevice()
        }
    }
}

Main function

fun main() {
    println("START")
    SharedFlowPresenter()
    CFRunLoopRun() // run event loop
}

The code above works fine but if I comment the LINE 1 and uncomment the LINE 2 I get kotlin.native.concurrent.FreezingException when calls repository.devices.collect . Can anyone help me to understand that?

Thank you so much

@qwwdfsad
Copy link
Collaborator

Sorry for being that late.

Please use #coroutines slack channel for general questions. Also, it is possible to use new MM #462 (comment) that has FreezingException gone

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