diesieben07
01/30/2020, 9:09 PMJob
with a parent (parent is long-running), is it a memory/resource leak to not complete or cancel the Job? Will the parent hold on to it and prevent it from being garbage collected?octylFractal
01/30/2020, 9:10 PMdiesieben07
01/30/2020, 9:12 PMSupervisorJob
for a service implementation. When the service gets a request, I start a child job for that request. This handle is handed out to third party code, how do I ensure that I clean up my job if the third party code fails / throws an exception?streetsofboston
01/30/2020, 9:20 PMJob
of each request to the caller?
If so, why?diesieben07
01/30/2020, 9:24 PM// third party code
interface Callback {
fun onMessage(message: Any)
fun onComplete()
}
// my code
class MyService : ThirdPartyInterface {
val job = SupervisorJob()
override fun someOperation(responseListener: Callback): Callback {
val requestJob = Job(job)
return object : Callback {
// handle onMessage, etc. here. write responses to responseListener
// this spawns processing coroutines using requestJob
// as such I must ensure to properly dispose of it...
}
}
}
octylFractal
01/30/2020, 9:26 PMstreetsofboston
01/30/2020, 9:26 PMoctylFractal
01/30/2020, 9:28 PMdiesieben07
01/30/2020, 9:28 PMoctylFractal
01/30/2020, 9:29 PMCoroutineScope
always contains a Jobdiesieben07
01/30/2020, 9:29 PMoctylFractal
01/30/2020, 9:29 PMSupervisorJob
to have a parentstreetsofboston
01/30/2020, 9:30 PMdiesieben07
01/30/2020, 9:30 PMCoroutineScope
it doesn't solve my problem, because now I need to properly dispose of that.streetsofboston
01/30/2020, 9:30 PMdiesieben07
01/30/2020, 9:30 PMstreetsofboston
01/30/2020, 9:31 PMdiesieben07
01/30/2020, 9:33 PMThirdPartyInterface
is a callback-based API.someOperation
is an implemenation of a third-party interface.octylFractal
01/30/2020, 9:33 PMCasey Brooks
01/30/2020, 9:34 PMJob
is so that you can cancel it in response to application lifecycles. Otherwise, coroutines will tidy themselves up. To handle other code, you should find some way to “bind” it to the world of coroutines. You can wrap a callback-based API into a coroutine with `suspendCancellableCoroutine`:
suspendCancellableCoroutine<String> { cont ->
someJavaMethodWithACallback { cont.resume(it) }
}
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/suspend-cancellable-coroutine.htmldiesieben07
01/30/2020, 9:35 PMsuspendCancellableCoroutine
requires me to be in a suspending function already. And it is also just a one-off. There can be many messages going in and out of the callbacksoctylFractal
01/30/2020, 9:37 PMonCancel
/ onComplete
diesieben07
01/30/2020, 9:37 PMoctylFractal
01/30/2020, 9:38 PMonCancel
will be calledCasey Brooks
01/30/2020, 9:38 PMFlow
might be closer to what you’re wanting than a raw coroutinestreetsofboston
01/30/2020, 9:39 PMclass MyService : ThirdPartyInterface {
val job = SupervisorJob()
override fun someOperation(responseListener: Callback): Disposable {
val scope = CoroutineScope(job)
return object : Disposable {
override fun dispose() {
scope.cancel()
}
}
scope.launch {
// handle onMessage, etc. here. write responses to responseListener
// this spawns processing coroutines using requestJob
// as such I must ensure to properly dispose of it...
}
}
}
diesieben07
01/30/2020, 9:39 PMstreetsofboston
01/30/2020, 9:39 PMclass MyService : ThirdPartyInterface {
val scope = CoroutineScope(SupervisorJob())
override fun someOperation(responseListener: Callback): Callback {
scope.launch {
// handle onMessage, etc. here. write responses to responseListener
// this spawns processing coroutines using 'scope'
// as such I must ensure to properly dispose of it...
}
return responseListener
}
}
diesieben07
01/30/2020, 9:42 PMstreetsofboston
01/30/2020, 9:44 PMlaunch
throws an exception, or just finishes, any Jobs associated with it will be cancelled/clean-up and garbage collected. The CoroutineScope will take care of that
You may want to install a CoroutineExceptionHandler in the constructor of he CoroutineScope
as well, to handle any unhandled/un-caught exceptions….diesieben07
01/30/2020, 9:46 PMstreetsofboston
01/30/2020, 9:46 PMdiesieben07
01/30/2020, 9:48 PMstreetsofboston
01/30/2020, 9:50 PM