CLOVIS
04/14/2024, 11:34 AMDaniel Lukic
04/15/2024, 7:36 AMCLOVIS
04/15/2024, 7:44 AMlouiscad
04/15/2024, 8:26 AMDmitry Khalanskiy [JB]
04/15/2024, 8:53 AMAre there any known plans to improve the debugger experience?Yes, certainly, there's active work in this direction: https://youtrack.jetbrains.com/issue/IDEA-338878/Coroutine-debugger-improvements
CLOVIS
04/15/2024, 9:55 AMCLOVIS
04/15/2024, 9:57 AMDmitry Khalanskiy [JB]
04/15/2024, 10:03 AMval result = AtomicInteger()
thread {
if (!inputIsValid(input)) throw IllegalStateException()
result.set(10)
}
while (result.get() != 10) { Thread.yield() }
CLOVIS
04/15/2024, 10:03 AMDmitry Khalanskiy [JB]
04/15/2024, 10:05 AMCLOVIS
04/15/2024, 10:05 AMCLOVIS
04/15/2024, 10:06 AMDmitry Khalanskiy [JB]
04/15/2024, 10:09 AMCLOVIS
04/15/2024, 10:11 AMIf you have some counterexamples, please share them.I don't have any on hand, but just in case—do I send them on YouTrack or in the KotlinX.Coroutines github?
Dmitry Khalanskiy [JB]
04/15/2024, 10:14 AMlouiscad
04/15/2024, 10:15 AMVsevolod Tolstopyatov [JB]
04/15/2024, 10:16 AMlouiscad
04/15/2024, 10:16 AMlouiscad
04/15/2024, 10:16 AMlouiscad
04/15/2024, 10:17 AMlouiscad
04/15/2024, 10:18 AMCancellationException
is already changing the semantics of catch (e: Exception)
, and for the worst anyway (zombie coroutines, infinite loops, ANRs…)…Dmitry Khalanskiy [JB]
04/15/2024, 10:19 AMIf it's only a semantic problem, and not a performance concernIf you mean enabling stacktrace recovery by default, then I'd think that subtly breaking production code in thousands of projects is quite a bit more problematic than making it slightly slower, actually.
louiscad
04/15/2024, 10:19 AMCancellationException
louiscad
04/15/2024, 10:20 AMDmitry Khalanskiy [JB]
04/15/2024, 10:22 AMCancellationException
doesn't break any codebases that just upgrade to the new version of the coroutines library.louiscad
04/15/2024, 10:23 AMlouiscad
04/15/2024, 10:26 AMCancellationException
being an Exception
is really my top one complaint, and stacktrace recovery being more known and available on Android in production is my top 2 wish.louiscad
04/15/2024, 10:27 AMDmitry Khalanskiy [JB]
04/15/2024, 10:28 AMCancellationException
should be instead?CLOVIS
04/15/2024, 10:29 AMlouiscad
04/15/2024, 10:30 AMCancellationSignal
which would be a direct Throwable
subclass after a migration phase before kotlinx.coroutines 2.0 to make it no longer an Exception
subclass.louiscad
04/15/2024, 10:31 AMThrowable
, stacktrace-free CancellationSignal
.louiscad
04/15/2024, 10:34 AMCancellationException
or from CancellationSignal : Throwable
would be about the same anyway, so I think going with CancellationSignal : Throwable
first is a good step, one that can fix many bugs by just upgrading kotlinx.coroutines to 2.xlouiscad
04/15/2024, 10:35 AMlouiscad
04/15/2024, 10:35 AMCLOVIS
04/15/2024, 10:52 AMlouiscad
04/15/2024, 10:54 AMCancellationException
first every time I need to catch for other possible things, but it's bad…kevin.cianfarini
04/15/2024, 6:13 PMkevin.cianfarini
04/15/2024, 6:16 PMkevin.cianfarini
04/15/2024, 6:19 PMkevin.cianfarini
04/15/2024, 6:21 PMlouiscad
04/15/2024, 6:22 PMCancellationException
was silently added to the stdlib at some point, and I don't understand why it was.kevin.cianfarini
04/15/2024, 6:23 PMlouiscad
04/15/2024, 6:24 PMIllegalStateException
.kevin.cianfarini
04/15/2024, 6:24 PMkevin.cianfarini
04/15/2024, 6:24 PMlouiscad
04/15/2024, 6:25 PMlouiscad
04/15/2024, 6:25 PMkevin.cianfarini
04/15/2024, 6:26 PMkevin.cianfarini
04/15/2024, 6:26 PMkevin.cianfarini
04/15/2024, 6:27 PMCLOVIS
04/15/2024, 7:50 PMGenerally you probably shouldn't catch ISE since it's supposed to represent illegal program state and should tear down everything, but in large production codebases with many engineers of varying levels that's a pretty unrealistic expectation to maintain over time.
Well, yeah, but it's also one of the most common exceptions we throw (
require
, check
, error
) so it's one of the most likely you'd want to target in event-loop-level exception handlers (as in, UI threads, web server endpoints...).CLOVIS
04/15/2024, 7:51 PMJust our of curiosity, are there other places in the Kotlin ecosystem that make heavy use of CancellationException that isn't the coroutines library?
I believe Arrow does, but it's mostly in order to interop with KotlinX.Coroutines.
CLOVIS
04/15/2024, 7:58 PMDaniel Lukic
04/18/2024, 10:25 AMobject Cx {
var Default: CoroutineDispatcher = Dispatchers.Default
var IO: CoroutineDispatcher = <http://Dispatchers.IO|Dispatchers.IO>
var Main: CoroutineDispatcher = Dispatchers.Main
var MainImmediate: CoroutineDispatcher = Dispatchers.Main.immediate
var Unconfined: CoroutineDispatcher = Dispatchers.Unconfined
}
(with all the globals in android it never really bothered me much to a few more.. 😄 )
and then simply set what i need in tests. i always felt it is clearer than passing around dispatchers. but i guess it comes down to opinions.. as it often does.. 🙃
and i didn't actually know (or care) about what CancellationException
extends.. i created - a long time ago, too - tryCatching
to mimic runCatching
, but for coroutines.. and it handles cancellation in one place.. and provides a CxResult
(instead of Result
because iirc Result
wasn't initially available to pass around or something?).. anyway, main point: maybe it's possible to manage cancellation in one place and consistently use something like tryCatching
? and forget about CancellationException
mostly? but i'm pretty sure i'm missing something here.. skimmed over all the comments here only.. ☯️
bottom line: can the issues you mention be avoided mostly with some helper code? 🤔 again, i'm probably missing something...CLOVIS
04/18/2024, 12:03 PMmaybe it's possible to manage cancellation in one place and consistently use something likeI recommend? and forget abouttryCatching
mostly?CancellationException
Either.catch
from Arrow which does this perfectly well (and also does it for other types of expressions that you shouldn't catch).
Personnally, all my classes that have a lifecycle of their own accept a CoroutineContext
constructor argument. But in the end, it's fairly rare that I need to switch dispatchers, so there are very few of those.