question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash when collecting a combined MutableStateFlow<Duration>

See original GitHub issue

Using the latest version of Kotlin (1.4.10) and coroutines (1.4.0), I get a confusing crash when attempting to combine a MutableStateFlow<Duration>.

See the code producing the issue in a Kotlin Playground.

Sample code:

val durationFlow = MutableStateFlow(Duration.ZERO)
val incrementFlow = MutableStateFlow(Duration.ZERO)

launch {
    combine(durationFlow, incrementFlow) { duration, increment -> duration + increment }
        .collect {
            Log.e("Flow", "Collected $it")
        }
}

incrementFlow.value = 5.seconds

Stacktrace:

    java.lang.ClassCastException: kotlin.time.Duration cannot be cast to java.lang.Number
        at MainActivity$onCreate$1$1.invoke(Unknown Source:0)
        at kotlinx.coroutines.flow.FlowKt__ZipKt$combine$$inlined$unsafeFlow$1$lambda$1.invokeSuspend(Zip.kt:33)
        at kotlinx.coroutines.flow.FlowKt__ZipKt$combine$$inlined$unsafeFlow$1$lambda$1.invoke(Unknown Source:14)
        at kotlinx.coroutines.flow.internal.CombineKt$combineInternal$2.invokeSuspend(Combine.kt:79)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

A similar code used to work earlier for me, but I couldn’t pinpoint in which versions.

I wonder if this is because Duration is an inline class, because using String instead works perfectly:

val stringFlow = MutableStateFlow("")
val incrementFlow = MutableStateFlow("")

launch {
    combine(stringFlow, incrementFlow) { string, increment -> string + increment }
        .collect {
            Log.e("Flow", "Collected $it")
        }
}

incrementFlow.value = "a"

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ilmiruscommented, Nov 9, 2020

@mickael-menu Thanks, reproduced: https://youtrack.jetbrains.com/issue/KT-43249. This is clearly compiler bug(s). And yes, this is because Duration is an inline class.

0reactions
Shusekcommented, May 19, 2021

After update to kotlin 1.5.0 and coroutines 1.5.0 the error happen again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Outputs to collect in the event of crash or unexpected reload
This document is a guide on important checks & useful outputs which should be collected in the event of crash or unexpected reboot....
Read more >
Collecting User-Mode Dumps - Win32 apps | Microsoft Learn
The options are a bitwise combination of the MINIDUMP_TYPE ... A crash dump is not collected when you set automatic debugging for ...
Read more >
Java SE 6 with HotSpot VM Troubleshooting System Crashes
This chapter provides information and guidance on some specific procedures for troubleshooting system crashes. A crash, or fatal error, causes a process to ......
Read more >
How to troubleshoot kernel crashes, hangs, or reboots with ...
At the moment of a kernel panic the secondary kernel will boot up, collect, compress and dump the first kernel's memory based on...
Read more >
Debug Android Crashes Faster With Datadog
Collect crash data from your users' Android devices. Understanding how users interact with your application can help you troubleshoot issues ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found