Hello all, I have a question about `structured con...
# coroutines
a
Hello all, I have a question about
structured concurrency
. If I understand, the thing is to ensure that we reuse a proper
CoroutineScope
instead of launching coroutines in the `Global`f scope. Then here is my question. I have 2 components:
A
&
B
. My component
A
initiate a coroutine scope and I can run functions against that. How do I share/propagate it to my component
B
on which I want to call functions.
Copy code
class A {
	val coroutineContext: CoroutineContext ...
}

class B {
	fun myFunction(){
		// run with A.coroutineContext?
	}
}
Do we have to pass the
coroutineContext
as an argument to any call of
B
?
b
“components A and B” is extremely vague in what the relationship between them is. It seems like you would make
A
implement
CoroutineScope
and then inject an instance of
A
into
B
Copy code
class A : CoroutineScope {
    override val coroutineContext: CoroutineContext ...
}

class B(private val a: A) {
    fun myFunction() {
        a.launch { ... }
    }
}
CoroutineScope is just that, a scope for coroutines
g
Also it's not clear why do you need scope or context in myFunction, but this is the key thing to answer on your question
a
To give some precision
I start some work from A, in coroutine scope
this work can call a function on class B
and I have to keep my coroutine scope then
typically, A is an Android ViewModel that has it’s coroutine context
g
What kind function on class B? Suspend, regular function, regular function that want to start new coroutine in scope A
a
I don’t know well
I written suspend function until now
g
And this is most important question
a
B contains suspending functions
g
Suspend function just runs, has nothing to do with scope, just call it from any coroutine
a
yeah, but then the suspending functions just run in the current coroutine scope?
The thing is: from A to B I have mostly
suspending functions
g
in will run in the ViewModel's scope (A). Isn't that what you want? And for dispatcher switch you have
withContext
. No need to pass CoroutineScope.
g
Depends on what you mean by "run in the current scope", suspend function doesn't start new background job in scope, it just suspend and return when function return, so you don't need scope in general for suspend function, but suspend function will have context of coroutine from which it called, and most probably this coroutine created using scope context
Scope is required only for coroutine builders that run background tasks, like launch or async, to avoid asyncronous work leaking
a
The is the thing
my work is mostly background
g
But suspend function is not background
a
The general case, I launch something from
A
and subcomponents called from
A
are suspending functions
g
so far so good
a
if
A
start a coroutines in background
suspending function should follow in this context if I understand
g
Yes, suspend function has context of parent (call site) suspend function
a
I have a corner case, when I need to create a channel to send data in an async way
g
But context and suspend function is not about structured concurrency
👌 1
This is completely different case from suspend function
Depending how you create and use this channel
g
For suspended functions: In A:
Copy code
override val coroutineContext: CoroutineContext = job + Dispatchers.Main
In B:
Copy code
suspend fun foo(){
   withContext(<http://Dispatchers.IO|Dispatchers.IO>){ some work }
}
For channels you'd need to pass the scope.
a
currently in one suspending function, called from a coroutines
g
If you use produce, which is coroutine + channel you should provide scope
a
but then you are out of the initial scope?
g
Why?
It's hard to recommend something without example of code
a
yeah
let me try to gather a smll snippet
g
If you use produce inside a scope it's fine, no need for additional scopes
👍 1
a
I can’t use
produce
directly from a suspending function
I have to create it from a coroutineScope?
Copy code
class A(val b : B) {
	
	val rootJob  = Job() 
    val coroutineContext = rootJob + <http://Dispatchers.dispatcherConfiguration.io|Dispatchers.dispatcherConfiguration.io>()

	fun runMe() = coroutineContext.launch {
        b.runMyChannel().consumeEach { myBooleanValue ->
        	// have incoming data
        }
    }
}

class B {
	
	suspend fun runMyChannel() = Channel<Boolean>{
		val channel = Channel<Boolean>()
		Global.async {
			// send a boolean value in a async way
			val myBooleanValue = true
			channel.send(myBooleanValue)
		}
		return channel
	}
}
How can I avoid using
Global.async
to send data in a async way?
g
I can’t use
produce
directly from a suspending function
Yes, this is what I mentioned above, you need some scope
a
Ok
g
I don't see any reason why runMyChannel is suspend function
a
yes here, not needed
g
Just pass scope to this function argument and use it to launch produce coroutine
a
Either, make
B
have its own scope?
perhaps not ... best to propagate scope to
B
to ensure that if we cancel
A
we cancel cal to
B
g
Yes, this is one of the options, depends on case, but you have to manage lifecycle of B scope (cancel it on destroy)
a
To give the complete story:
r
Let me throw in a more specific example. If I want to run some coroutine builders, for example, from
onBindViewHolder
of
RecyclerView.Adapter
, I have to pass `Fragment`s scope to the adapter explicitly, right?
a
yeah
To complete my story about B and why I need channels
in
B
I have old async API with callbacks
aka Firebase stuffs
r
So something like
class MyAdapter(coroutineScole: CoroutineScope): RecyclerView.Adapter<...>(), CoroutineScope by coroutineScope {
should do?
g
Also, side note, I'm not sure that understand code of your A class, it looks incorrect, it probably should implement CoroutineScope and you should just call launch as extension function in this scope, not on CoroutineContext, this wouldn't be available
a
👍
r
It's not incorrect, it's just explicit about it's context. IIRC, @louiscad has the same explicit scope in his Splitties. https://github.com/LouisCAD/Splitties/tree/master/modules/lifecycle-coroutines#example
g
Yes, I also prefer style from Andrew's message with passing scope to constructor of a class that shares it with some other classes
a
My sample is complex
because I want to catch data from an old async style API
into a channel
then into my old async callback, I have to recreate/reuse a coroutine scope to allow me to send data with my channel
g
It's not incorrect, it's just explicit about it's context. IIRC
I'm not sure that it is the same tho, maybe, but it's not enough context to be sure
a
Copy code
user.getIdToken()
        .addOnCompleteListener { task ->
            // send my data here, but in other thread
        }
g
Why not just write coroutines adapter for your call back based API, it would be easier to use and would not require additional complexity
a
a coroutines adapter?
g
Yes
Is it one time event or stream of events?
a
yes
r
Yeah, I agree. You just add a thin layer which wraps your callbacks into coroutines stuff and work with normal kotlinx.coroutines code in all the other app.
a
that’s what I want ^^
r
g
And this (wrapping callbacks to coroutines) shouldn't involve any kind Structured concurrency machinery, scopes or contexts
a
great guys 👍
yeah I understand, it’s a problem to continue the execution of a coroutine after a callback
and
kotlinx-coroutines-play-services
will clearly help 👍
👍 1
thanks
and the
Wrapping callbacks
also 👍
r
Glad we could help, good luck with jumping on board 🙂
g
Yes, play-services adapter does exactly this: wrapping callbacks, but a bit more complicated, also supports cancellation, converting multiple events to channel etc
r
I'm not entirely sure there are channels specifically in play-services, but you can find an example of wrapping something channel-like here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt You basically provide observer thing for the library you wrap, which either is a
Channel
itself or provides a channel where it emits events observed in upstream (and vice versa).
g
Ahh, you right, for some reason I thought that Task supports multiple callbacks, maybe I thought about some other API
But careful with Rx adapter it's quite complicated
a
mixing Rx/Coroutines + Firebase API ... yummy 😛
d
Copy code
class B {
    fun CoroutineScope.runMyChannel() = Channel<Boolean>().also { launch { it.send(true) } }
g
This code can be replaced with produce,
b
@gildor except
produce
closes the channel when the
produce
coroutine returns
g
Good point! But for example above it is probably good and more correct behaviour
d
I assumed that the OP wanted to send more stuff to the channel @gildor
g
Maybe, really hard to follow without actual use case
d
True, given the example that was my assumption, didn't think it through very much.
I almost suggested just using
offer
😂 no CoroutineScope needed
a
@arnaud.giuliani, I've also struggled about understanding of scopes and jobs and relations between calls: Here is nice read about https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055 And my thread on github: https://github.com/Kotlin/kotlinx.coroutines/issues/1001 Short summary: there are two mechanisms in play: a) all coroutine builders (async, launch) will create new Job (coroutine) and attaches it into parent's job (parent is your current context where you've called async/launch unless you've explicitly passed new context with "context" parameter (applies only if passed context contains Job())) And these parent-child relationships plays a role on cancellation and join/waiting of coroutines. b) and there is "withContext" suspending behaviour, example it's just a suspend function. It suspends and waits on current coroutine where it have been invoked. withContext do also maintain parent-child the same as async/launch. So if "withContext" is used with explicit new context which contains Job() it gets more difficult to track because it suspends current coroutine and code block is launched on new coroutine which becomes a child of given Job(), it won't become a child of a coroutine where you've invoked from (they are tangled by suspend behaviour only)
👍 1
To answering directly to your question, if you have an object of class A in that object you can call launch/async and just call object's B any suspend functions and they are all be run on A's scope by default
unless these suspend function will contain some kind of switched e.g. withContext or launch(context =..)
Also take as a warning that default kotlin's implementation of withContext and launch do not maintains parent-child relations of callee so if you are explicitly switching context on new Job it won't be linked to your call graph, so if A's object scope will be cancelled all invocations deep inside call graph of withContext launch won't be cancelled (they are non-cancellable by default) (this applies only if you passing context with Job())
but if you are designing system there many objects maintains it's own scopes and want to forward executions of methods on these scopes then with current withContext / launch implementations you need explicitly implement cancellation tracking.
a
@Antanas A. thanks 👍
a
np
g
if A’s object scope will be cancelled all invocations deep inside call graph of withContext launch won’t be cancelled (they are non-cancellable by default
This is incorrect
withContext and any other suspend function is cancellable by default, if Job is cancelled any suspend point will interrupt execution of any suspend function
a
in theory only
g
No
On practice
if you don’t have any blocking calls
only blocking calls are non-cancellable
a
sorry, maybe I've expressed wrongly
suspend function is cancelled, but a Job which was started by withContext is not cancelled by that, because it will be attached on some Job which you've passed with parameter in withContext(context = someNewContext) { codeblock() }
even if withContext, technically, it's suspension is cancelled
codeblock() will be run independently of that cancellation
g
a Job which was started by withContext is not cancelled by that
withContext doesn’t start any Jobs, I don’t understand what you mean
a
wait, I'll try to explain by example
If I'm having a class A with CoroutineScope and it has coroutineContext = Dispatcher + job
and I'm doing withContext(context=coroutineContext) { }
Im passing a job
g
yes, correct
if coroutineContext has a Job it will cancel withContext
I see what you mean, that if you pass another Job to withContext it will be non-cancellable
a
@test fun `withContext test`() = runBlocking { class A : CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Default + job suspend fun test() = withContext(coroutineContext) { while (isActive) { delay(1000) } println("work completed") } } val a = A() val job = launch { a.test() } delay(200) println("my jobs: " + job.children.toList()) println("A jobs: " + a.job.children.toList()) launch { delay(2000); job.cancel() // never cancels } job.join() // <- join is waiting also for test() to complete? But how? it's on different Job() println("done") // <- never reaching here }
here is example
my jobs: [] A jobs: ["coroutine#2":DispatchedCoroutine{Active}@1324409e]
it prints this
so withContext will start new coroutine (which has interface Job, thats why I'm calling sometimes job instead of coroutine)
So actually, I'm just passing my own context
g
Yes, you can mess-up it, but why would you do this?
a
and it gets non cancellable by default
g
Just because you replaced job
It’s nasty behaviour
a
I'm just thinking more like as Erlang objects
g
but I still not sure about your point
why do you need to pass Job to withContext?
a
it contains it's own scope/context, and if I'm running some method on it I want that this object's scope will handle a load
I dont passing Job into with context I just passing my own context
as A class has CoroutineScope
so I just pass withContext(this.coroutineContext) { }
implementing coroutineContext of CoroutineScope without explicit job is unknown behaviour
g
withContext(this.coroutineContext) what is use case for this?
a
all launches will be run on global scope then
g
implementing coroutineContext of CoroutineScope without explicit job is unknown behaviour
What do you mean?
If you create CoroutineScope without job Job will be created automatically
a
if I'm implementing CoroutineScope on some class
then I'll definitely need some Job object to allow maintain a jobs launched on that scope
g
Yes
and it has Job
a
without an explicit Job is not possible to track and cancel all launches
g
It’s not true, you can get job from context
a
If I'm not explicitly pass a Job the Job object will be created each time new, then I "call Launch"
g
or just cancel scope itself
a
Ok, one more demo
please wait a bit 🙂
by time, example is object DataDownloader : CoroutineScope, which has suspend fun download(url) = withContext(this.coroutineScope) { downloading } and method destroy() or cancelAll() that downloader can be used from may places in an application
and application must have a behaviour to track an cancel all downloads initiated in application
but also we need a way to cancel a specific download
g
I really don’t understand why do you need this idiom
withContext(this.coroutineScope)
a
if a coroutine is cancelled which invoked a "download"
it has its own dispatchers
and these operations must be offloaded from original dispatchers
I mean all callees can be on any dispatchers
and DataDownloader should not be affected by that
g
Yes, you switch to particular dispatcher using
withContext(MyDispatcher)
To cancel this downloading you just cancel job where this downloading started
a
If we've rewrite that code by this there will be no way to cancel all activities
from one place
yes, but it wont be possible to cancel everything at once
as I understood examples of android activities
the CoroutineScope was designed for that (to control lifecycle of coroutines of particular scope)
g
I don’t understand your use case. suspend function that download something shouldn’t be bounded on any kind global state, it returns result or cancel
a
not a global state, but to a scope of downloader
why I cannot have a scope of downloader?
g
I believe it’s just wrong implementation for this use case
a
looking as Erlang actors
g
You may have, but than it shouldn’t be suspend function
a
but why not if it has only download operation
g
rather Defered that runs coroutine on downloader
Or actor yes
Suspend function means that caller side manages lifecycle of this function
a
Maybe it's an edge case and really rare needs
but following logic I see no point why it not logical
g
you of couse may add some trick to also control it from downloader, but you shouldn’t do that using Jobs, otherwise you get this nasty behaviour like in your first example
I think it’s abuse of cancellation that causes real problems with cancellation
a
but the pratice is if you are implementing CoroutineScope interface you should define job object otherwise it won't be possible to control scope
g
but the pratice is if you are implementing CoroutineScope interface you should define job object otherwise it won’t be possible to control scope
Depending on how you do this
If you use
CoroutineScope()
or
MainScope
builders job will be created for your, no need to create it explictly
a
but Job will be created
SupervisorJob
g
if you implementing interface than yes, you have to create Job yourself
a
and withContext(this.coroutineContext) { }
g
Yes
a
will be non-cancellable immediately
g
withContext(this.coroutineContext)
just wrong by definition IMO
I mean such idiom doesn’t have sense, I don’t see any valid usage
a
but why not
g
I do not argue about Job, of course you need one implictly or explicitly created
a
or withContext is bad naming
withDispatcher
should be called instead
g
No
a
but why illogical is to run on class's context
g
see:
Copy code
withContext(this.coroutineContext) {
   someSuspendCall()
}
and
Copy code
someSuspendCall()
Is just the same
a
if I want to delegate execution on that
if called inside the same scope (class)
if someSuspendCall() is called from outer scopes eg Dispatchers.Main
g
If you just want to create new scope use:
coroutineScope{}
instead of
withContext(this.coroutineContext
a
then its completely different semantics
g
Yes, I see what you mean, you right
a
that scope will be bound to callee scope
g
Sorry, now I got your case
Or not %)
a
😄
ok, imagine android app there everything is run on Main thread
g
Yes, I see
a
some activities want to do downloading
g
this.coroutineContext is actually CoroutineScope.coroutineContext
a
yes
g
now I see
a
not the callees scope
I want to run away of callees scope actually
but maintain cancellation
because if activity is destroyed
g
I still not sure that this is correct approach, because you mess up Job
a
I want to cancel that download
g
Using dispatcher if fine
like
this.coroutineContext[CoroutineDispather]
a
but also I want somehow to cancel all suspended download() calls in system with another background operation which destroys and cancels downloader
g
but using context together with job seems wrong for me
a
so all suspended functions will be cancelled
g
but also I want somehow to cancel all suspended download() calls in system with another background operation which destroys and cancels downloader
I belive this is use case for Actor
a
yes, by doing this.coroutineContext[CoroutineDispather] it would work because new execution will be attached to callees parent
but I've no way to cancel by outside
from some background service downloader.cancelAllDownloadsImmediately()
maybe explicitly by adding to some list of jobs all created jobs
g
Yes, and it’s good thing imo, but even if you need it, there are other ways to cancel, on level of your function itself, for example it easy to cancel all downloading tasks on Http client level
a
withContext(this.coroutineContext[CoroutineDispather]) { jobs += this.coroutineContext[Job] do stuff }
😄
g
Yes, something like that
but It’s very nasty behaviour
It may cause terrible bugs
a
but why to do that if CoroutineScope is designed for that
scope is to group coroutines
g
yes, but you try to group coroutines of one scope and coroutines of another scope
a
so suspend fun download() = withContext(this.coroutineContext) { }
even launch(this.coroutineContext) { } will corrupt parent-child
yes, but shouldn't better these also be tracked
and be cancelled
here and tell me if it's ok
g
If I would need some global cancellation of all running tasks I would use async on my downloader or actor or other pattern
a
It's home made patch to maintain cancellation somehow
but I think it's better to have suspend behaviour instead of async
or are you suggesting
suspend fun download() = async(this@A.coroutineContext) { hob }.join()
?
g
I will check discussion later
I see that Roman suggesting the same solution with launch/async
a
but async is also wont cancel children job (downloading)
g
Why?
a
because one way or another it will be attached to coroutineScope's job
not a callees
g
Yes, but this is caller responsibility to manage Defered if you need cancellation
a
but I'm not sure, I will test this solution
yes, but I'm talking about Downloader's responsibility to be allow cancel everything on downloader
all download tasks which is currently run
g
But I agree, this is much more error prone
I would do this on Downloader implementation level rather than mess-up client cancellation flow
a
I just logically tried to implemented that downloader as thinking of objects which has a dispatcher and it's own scope
and by deferring all works with withContext(this@A.coroutineContext) { } looked as a way to go
but maybe it's just my logical thinking path which will be not followed by others
😄
just imagination that objects who has CoroutineScope is like a pools
where jobs can be run on
and be grouped together (and controlled)
independently of callers implementations and usages of these objects
I've suggestion that by default kotlin could maintain parent-child in any cases
unless NonCancellable object is passed into context
because now if anyone will pass context with a Job object it gets instant non-cancel behavior
Don't know exactly which is more appropriate and what behaviour is more frequent expected
d
I suggested having multiple parents once but I didn't make a great case for it
Because I think it would make things really complex unnecessarily
And I suggested it alongside
withScope
function
a
I think multiple parents would be really overkill and more edge cases would appear.. I'd prefer solution to track a callee graph instead, but actually it would copy job child relations..
d
Yeah, special extra parent
a
multiple parents is hard to understand, how about cancellation and supervision how to define rules which is which
d
Exactly. That's my point
a
logically "extra parent" = "callee", and this property cannot be used for cancellation
because parent must cancel childrens
g
But suspend function is not children imo
a
and if child knowns from what parent it originates
the parent itself couldn't find a children
yes, suspend fun is not children
everything is ok with suspend functions and logic behind
children is a new coroutine launched from some suspend function from another coroutine
now it's seems partly done, all works if coroutineContext[Job] is not changed
It feels that there are some missing concept - as CoroutineCreationGraph
on which cancellation could be implemented