Youssef Shoaib [MOD]
05/04/2024, 3:51 PMContinuation
that I know I won't use anymore. It was previously created because a call to startCoroutine(myCont)
suspended. I'm sure that I want to get rid of that Continuation
and instead resume myCont
in a different way (with a different Coroutine). Is there a best practice to follow here? suspendCancellableCoroutine
just gives me an Already resumed
exception for some reason. What's the proper way to dispose of a Continuation
? Does the garbage collector handles this on its own? Should I be using a CancellableContinuation
and cancel it instead?Sam
05/04/2024, 3:56 PMtry
or use
) then resuming it with an exception (e.g. cancellation) is the only way to make sure the resources get releasedYoussef Shoaib [MOD]
05/04/2024, 4:01 PMCancellationException
that I then swallow? I don't want the cancellation to propagate to the myCont
because, again, I will resume it on my own later.Sam
05/04/2024, 4:09 PMmyCont
with some man-in-the-middle completion handler, it could decide whether to pass the completion on to the real continuation or not, depending on the outcomeYoussef Shoaib [MOD]
05/04/2024, 4:10 PMresume
calls that have my special exception, and thus it'd be able to resume multiple times until it gets an actual value. Is there a preferred way to do that? Would implementing a Continuation
be it?Sam
05/04/2024, 4:10 PMsequence
and iterator
functions do it, so it can't be that bad 😄🙈Sam
05/04/2024, 4:11 PMSam
05/04/2024, 4:12 PMSam
05/04/2024, 4:18 PMSam
05/04/2024, 4:19 PMYoussef Shoaib [MOD]
05/04/2024, 4:19 PMsequence
and iterator
are RestrictsSuspension
though so it makes some sense (although I didn't know they just throw it away).
I'll check out the AbortFlowException
code because that sounds like exactly what I need.Sam
05/04/2024, 4:25 PMsequence
issue a while ago -- not the most impressive thing I've ever written but it has some decent examples of the problem that needs to be avoided when making custom suspension points
https://betterprogramming.pub/breaking-try-catch-finally-in-kotlin-b95059a5673fYoussef Shoaib [MOD]
05/04/2024, 4:31 PMIterator
can't really close. Maybe we need a CloseableSequence
or similar, but it's likely too niche. I've made something that sort-of works now, but I'll check the flow code just in case.ephemient
05/04/2024, 8:15 PMStream
and Flow
are closeable sequencessimon.vergauwen
05/06/2024, 12:48 PMOr you could just throw away the continuation and accept the potential control flow violation -- theI think it's "not that bad" because it's RestrictSuspension, and this also single threaded but it probably breaksandsequence
functions do it, so it can't be that bad 😄🙈iterator
try/finally
. Right?
Afaik resuming with CancellationException
is the only sane thing to do @Youssef Shoaib [MOD], not ideal but necessary evil for these kind of things IMHO. I think sequence
and iterator
should do the same, since CancellationException
is now official part of the Kotlin Std.
Only alternative is if we get cancellation control in Kotlin Std, which has been an issue somewhere for ages. This is the perfect use-case IMHO, and Arrow could leverage the same if it wanted.