https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
o

Oleksii Malovanyi

07/16/2021, 3:09 PM
Hey guys, I’ve found a nasty issue: the neverending flow collection from the
intent{}
– it’s never canceled on parent’s
scope.cancel
in case there is an
exceptionHandler
installed, e.g.: something like this would create a mem. leak:
) : ViewModel(),
    ContainerHost<ProfileState, ProfileEffect> {

    override val container: Container<ProfileState, ProfileEffect> = container(
        initialState = ProfileState(authorized = null, account = null),
        savedStateHandle = savedStateHandle,
        settings = Container.Settings(exceptionHandler = exceptionHandler.asCoroutineExceptionHandler()),
    ) {
        intent {
            subscribeToSessionUseCase().collect {
                reduce { state.copy(authorized = it != null) }
            }
        }
    }
Here is the sneak peak of the fix – I gonna create a test for that, but wonna let you know early https://github.com/orbit-mvi/orbit-mvi/pull/58
okay, PR is ready for review
a

appmattus

07/17/2021, 5:31 PM
Thanks, I’ll take a proper look on Monday :) As an FYI if you were to ever have an espresso test where there’s an infinite flow you should disable idle resource handling and handle them yourself. If you don’t then it will never go idle. Use:
intent(registerIdling = false) { …
m

Mikolaj Leszczynski

07/17/2021, 8:17 PM
Oleksy, thank you for the initiative. I had a look at your PR and while I’m happy with the fix, I feel a bit uneasy we’re doing non-standard logic around jobs to make the original solution work. My worry is that continuing down this path might cause other unexpected behaviour. I had another look myself and found an alternative solution that I’ve verified against your unit test (and the rest of our test suite). I’ve pushed it under
simple-exception-handler-test
The TL;DR • getting rid of supervisor scope • wrapping coroutine code inside the launched intent coroutine with
coroutineScope
- this will make any exceptions thrown by coroutines launched within catchable without the need to use an exception handler as explained in this article https://www.lukaslechner.com/why-exception-handling-with-kotlin-coroutines-is-so-hard-and-how-to-successfully-master-it/ • surrounding the new scope with
runCatching
to handle exceptions This is a very rough around the edges draft right now as I’m just forwarding exceptions to the handler defined in settings. My point is we wouldn’t even need one with this approach, we’re handling exceptions as defined by your test cases and we’re keeping the structured concurrency in place without having to recreate it. Happy to hear your thoughts @Oleksii Malovanyi @appmattus
o

Oleksii Malovanyi

07/19/2021, 9:15 AM
@Mikolaj Leszczynski thanks for the deep explanation. I feel a bit lost with the structured concurrency, and for now I can’t distinguish which solutions would be the best. Linking the newly created scope with the parent one via the job seems logical to me. Also, it has 1 serious benefit -> anyone inside the
intent
could use the underlying context implicitly to get the CoroutineExceptionHandler under the hood. E.g.:
private fun refreshAccount() = intent {
        executeUseCase { refreshAccountUseCase() }
    }.onSuccess {
            //
    }.onFailure {
            //
    }
}
suspend inline fun <R> executeUseCase(block: () -> R): Result<R> {
    val currentExecContext = coroutineScope { coroutineContext }
    val exceptionHandler = currentExecContext[CoroutineExceptionHandler]
    return runSuspendCatching(block)
        .onFailure {
            exceptionHandler?.handleException(currentExecContext, it)
        }
}
So, I’d go with the pure context based solution, as it doesn’t add new custom logic, but uses standart mechanisms of coroutines
Also, FYI we can’t use
runCatchnig
directly, as it breaks cancellation as well
m

Mikolaj Leszczynski

07/19/2021, 9:16 AM
we can rethrow cancellation exceptions
or do you mean something else
o

Oleksii Malovanyi

07/19/2021, 9:16 AM
yes, exactly
inline fun <R> runSuspendCatching(block: () -> R): Result<R> {
    return try {
        Result.success(block())
    } catch (cancellationException: CancellationException) {
        throw cancellationException
    } catch (e: Throwable) {
        Result.failure(e)
    }
}
but anyway, we can’t use underlying
CoroutineExceptionHandler
under the hood
even more, the class we pass to orbit’s container (CoroutineExceptionHandler) missleads -> as it would be never used as it is, it would be used more like a custom handler class which is not connected with coroutines infrastructure any more
m

Mikolaj Leszczynski

07/19/2021, 9:18 AM
I don’t know if forwarding exceptions to it manually is the right thing to do tbh
I wonder if a hybrid solution is possible i.e. using
coroutineScope
and passing it the exception handler
will have to test
o

Oleksii Malovanyi

07/19/2021, 9:20 AM
I need to fix the test from my PR first, sorry 😞
m

Mikolaj Leszczynski

07/19/2021, 9:21 AM
even more, the class we pass to orbit’s container (CoroutineExceptionHandler) missleads -> as it would be never used as it is, it would be used more like a custom handler class which is not connected with coroutines infrastructure any more
As I mentioned before, I’m not suggesting we keep it as a CoroutineExceptionHandler 🙂
we could do with a simple function
o

Oleksii Malovanyi

07/19/2021, 9:21 AM
yes, but what’s the benefit?
m

Mikolaj Leszczynski

07/19/2021, 9:21 AM
not messing around with jobs
I’ll investigate if we can keep the standard mechanism but simplify it
the only reason you need this fix is cause we separate the coroutine out from the parent’s context through supervisor job - if there’s a way we can avoid it that’s what we should do
I’m sure there’s an alternative that will keep both of us happy 🙂
will investigate in the evening
o

Oleksii Malovanyi

07/19/2021, 9:24 AM
Sure, if you feel like so, need to try definitely. But I don’t see what’s wrong with the current solution and fix: • we’re creating a child job for each intent as we don’t want it to break all the tree up (supervisor) • we still want it be dependant on parent scope, so we link it to the parent job
m

Mikolaj Leszczynski

07/19/2021, 9:26 AM
Nothing wrong with it - I just see that we’re starting to complicate things and am worried about hidden consequences. If we can simplify this we should, if we can’t we’ll go with your approach.
o

Oleksii Malovanyi

07/19/2021, 9:28 AM
sorry for being a bit aggressive, Monday my bad 😞
m

Mikolaj Leszczynski

07/19/2021, 9:28 AM
Relax, we’ll get it fixed 😉
o

Oleksii Malovanyi

07/19/2021, 10:36 AM
FYI, I’ve pushed the fix for the cancellation test
👍 1
a

appmattus

07/19/2021, 11:04 AM
My concern with the use of coroutineexceptionhandlers is to me it’s feels like if you need one something isn’t being done right. Obviously I don’t know enough about the context of what you’re trying to achieve Oleksii so it’s not to say you should never need one. The fact is if an exception has occurred and not been handled properly then what does this mean for your current state? For example: fun doSomething() = intent { reduce { Loading } val data = someFunctionThatErrors() reduce { Ready(data) } } If someFunctionThatErrors() throws an exception then your existing state is left mangled with a value that shouldn’t be there. In some sense you need to handle the exception appropriately in every intent block. So what does your exception handler do? While it could “fix” the state, is that right? Probably not. The best you can do is log the exception.
o

Oleksii Malovanyi

07/19/2021, 11:11 AM
In my experience, the exceptionHandler is best for 2 things: • prevents production from crashing; • centralises logging of the non-expected warnings/fatals. For the case you’ve wrote in our current project we do always wrap the potential failing execution site with the wrapper (see
executeUseCase
from above in this thread) the same as we do for other coroutine-related stuff -> via the
coroutineContext[CoroutineExceptionHandler]
, which is quite good application of dependency inversion principle in my taste. Now the talk about a small fix which actually causes a mem. leak. Maybe, if you want to discuss the existence of the handler itself, I’d recommend to start another discussion to keep this focused
@Mikolaj Leszczynski I have another small observation regarding the mem. leak to think about -> I’ve noticed the
dispatchChannel
from the
RealContainer
is never cancelled -> so there could be a situation, probably, where the channel would have a buffered message but the receiver coroutine is already cancelled. I guess we should cancel/close the channel when scope completes as well. Smth like this could work (but requires some caution in terms of send invocation then):
👀 1
m

Mikolaj Leszczynski

07/19/2021, 7:13 PM
Hey I probably won't be able to look at it today, I had one hell of a Monday... Will pick it up as soon as I have some spare time.
🙏 1
o

Oleksii Malovanyi

07/19/2021, 7:23 PM
what I wanted to say how I feel: this is purely a hotfix and I’d appreciate if we treat it as so and maybe postponed some long-term refactoring or discussions for a better times
m

Mikolaj Leszczynski

07/19/2021, 7:32 PM
Fair enough
I’ve merged and tagged
4.1.3
- now we need to wait for the build
o

Oleksii Malovanyi

07/19/2021, 7:42 PM
thanks @Mikolaj Leszczynski I owe you 🍻