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

Crashes on shift when using withContext and handleErrorWith #2779

Closed
e1ywka opened this issue Jul 29, 2022 · 4 comments
Closed

Crashes on shift when using withContext and handleErrorWith #2779

e1ywka opened this issue Jul 29, 2022 · 4 comments

Comments

@e1ywka
Copy link

e1ywka commented Jul 29, 2022

Hello,

I have reproduced similar issue #2760 which was not fixed with #2765

arrow version 1.1.3-alpha.37

import arrow.core.continuations.Effect
import arrow.core.continuations.effect
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

fun main() {
    val failed: Effect<String, Int> =
        effect {
              withContext(Dispatchers.Default) {}
              shift("failed")
        }

    val newError: Effect<List<Char>, Int> =
        failed.handleErrorWith { str ->
            effect { shift(str.reversed().toList()) }
        }

    runBlocking {
        newError.toEither()
    }
}

The crash looks like that

Exception in thread "main" kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[BlockingEventLoop@77846d2c, Continuation at MainKt$main$failed$1$1.invokeSuspend(Main.kt:13)@530612ba]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
	at kotlinx.coroutines.DispatchedTask.handleFatalException(DispatchedTask.kt:144)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:115)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at MainKt.main(Main.kt:22)
	at MainKt.main(Main.kt)
Caused by: ShiftCancellationException(Shifted Continuation)
	at arrow.core.continuations.DefaultEffect$fold$2$effectScope$1.shift(Effect.kt:785)
	at MainKt$main$newError$1$1.invokeSuspend(Main.kt:19)
	at MainKt$main$newError$1$1.invoke(Main.kt)
	at MainKt$main$newError$1$1.invoke(Main.kt)
	at arrow.core.continuations.EffectScope$DefaultImpls.bind(EffectScope.kt:61)
	at arrow.core.continuations.DefaultEffect$fold$2$effectScope$1.bind(Effect.kt:772)
	at arrow.core.continuations.Effect$handleErrorWith$1$1.invokeSuspend(Effect.kt:662)
	at arrow.core.continuations.Effect$handleErrorWith$1$1.invoke(Effect.kt)
	at arrow.core.continuations.Effect$handleErrorWith$1$1.invoke(Effect.kt)
	at arrow.core.continuations.FoldContinuation$resumeWith$2$f$1.invokeSuspend(Effect.kt:719)
	at arrow.core.continuations.FoldContinuation$resumeWith$2$f$1.invoke(Effect.kt)
	at arrow.core.continuations.FoldContinuation$resumeWith$2$f$1.invoke(Effect.kt)
	at arrow.core.continuations.FoldContinuation.resumeWith(Effect.kt:720)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	... 8 more

@nomisRev nomisRev mentioned this issue Aug 1, 2022
@nomisRev
Copy link
Member

nomisRev commented Aug 1, 2022

Thank you for reporting this fix with a reproducible scenario @e1ywka! 🙏
That was really useful for writing the test, and investigating the issue!

I'll report back here if an alpha with the fix is released, the PR I made verifies the test fails with your reported issue and a later commit adds the fix to proof it was fixed but verifying in your local project would be great.

@nomisRev nomisRev mentioned this issue Aug 1, 2022
@nomisRev
Copy link
Member

nomisRev commented Aug 3, 2022

@e1ywka the fix is available in 1.1.3-alpha.38
If you have some time could you check it, and report back please? 🙏

@jrgonzalezg
Copy link
Member

Thanks for this!. I was still having those crashes in other "related" code after resolving the ones on #2760 but I had not been able to reduce them to a minimal reproducible test, nice you found a way! :)

@e1ywka
Copy link
Author

e1ywka commented Aug 3, 2022

Yes, it works for me. I'm glad it was helpful!

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

Successfully merging a pull request may close this issue.

3 participants