-
Notifications
You must be signed in to change notification settings - Fork 453
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 effect shift when using Arrow with ktor client #2760
Comments
Hey @jrgonzalezg, This seems very weird 🤔 I am not sure this is being caused by Ktor, I cannot see any clear indication in the stacktrace at least. Those Android packages got me worried more :D It looks here was able to catch the Do you have a reproducible example of this code I could look at? Is there any surrounding code that could be capturing the |
@nomisRev The Android code is only reached after the exception bubbles all the way up to the activity, so I don't think it plays a role on it. Shouldn't the |
Hey @jrgonzalezg,
That is definitely the case, but something is going wrong here. I just noticed you have Could you try 2 things?
|
The reason I suspect this is because I don't see anything unexpected in the stack-trace. Potentially |
@nomisRev Neither removing suspend nor inline / reified seemed to work. But I have also tried to reproduce it by adding some minimal code on the console application template made by IntelliJ and it does not seem to have the issue on that one. I'm using Kotlin 1.7.0 in all cases, since I can only use that exact version on the app code due to being blocked by Jetpack Compose restrictions. But I don't understand how the Android side of things may be causing this though. I'm simply calling the effect on a |
I have spent a bit of time trying to figure out the differences between the variants that work and the ones that doesn't and it seems it can be minimized to this fairly minimal code:
That fails both on console application and Android application. The inline / reified and so on don't seem to play a role after all so I removed it. That code on Android side crashes with the exception while on console application it just hangs forever but does not crash. If instead I replace the:
with
it does work. So somehow it seems that the issue is related to trying to combine effects inside an enclosing |
Another way that does work is this:
so maybe the issue is the |
Oh... I've seen this before, but I thought it was related to context receivers at the time. If Using I am going to try these examples for latest versions of Arrow, Kotlin and KotlinX since we should try to pin-point the exact issue so we can fix it in Arrow, or create a YouTrack issue for the compiler. |
Yes, you are right, using |
@jrgonzalezg another similar bug has been reported, and I shared some findings here. It's indeed going wrong inside of the compiler, since You can use |
Thanks @nomisRev! But note that in my examples only |
That is very strange 🤔 I've just changed the implementation of |
I have reduced the issue further down to the following, so it's not related to Ktor. suspend fun main() {
effect {
failure().bind()
}.fold(
recover = { println("Recovered $it") },
transform = { println("Result $it") },
)
}
fun failure(): Effect<Unit, HttpResponse> = effect {
// Different dispatcher then where the code is running
withContext(Dispatchers.IO) {}
shift(Unit)
} |
Nice simplification!, that one behaves just like the code I had albeit being much simpler. It also does not work with the |
I found a fix, you can check out the PR here. #2765 |
Nice!. I will try it once a new alpha is out 🎉 |
@jrgonzalezg if you have some time to test the fix in version 1.1.3-alpha.31 that would be great! 🙏 |
@nomisRev Yes, it seems to be working now 🎉 . Closing this one. |
@jrgonzalezg the actual issue was not fixed before, only the symptom... The actual fix is available in 1.1.3-alpha.38 |
Thanks!, I knew there were still issues as I mentioned on some comments I made on #2769 but I had not yet been able to reduce it to a minimal failing test like #2779. I will try alpha 38 (maybe better 39?) and see if it works in more cases. For instance I had crashes on a |
Awesome, thanks @jrgonzalezg! Any version after |
@nomisRev Both the original code where I was using Arrow with the Ktor client and my initial |
Awesome, thank you so much for taking the time to test this and reporting back @jrgonzalezg! |
Hi!. I'm trying to migrate an Android app's network layer to the new Effect / effect APIs in Arrow but there seems to be some issue or incompatibility with ktor's HttpClient. The code works fine on the happy path, but as soon as it gets to a
shift
the app crashes and it does not even stop on catch blocks or fold error handling so it seems to be bypassing the error handling mechanism.The function I'm trying to implement looks like this:
but same issue also happened with more minimal code without the try-catch and simplified error type. This happens both with arrow-stack 1.1.2 and 1.1.3-alpha.29. The stacktraces of the crashes look like this:
So it seems like some kind of incompatibility maybe because both Arrow and ktor try to tap on the coroutine mechanisms. Apparently the
ShiftCancellationException
is somehow handled by ktor internal code and that in turn produces a crash before the exception is handled by Arrow code.The text was updated successfully, but these errors were encountered: