question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When creating a new job, it should be easier (default) to create child instead of a new root job

See original GitHub issue

When creating a new Job, the simplest way to do this, and the way most people do it by default is with Job() or SupervisorJob(). However, in most cases what is actually intended is to create a job that is a child of some context or scope. The current state of things makes it really easy to accidentally create a job that breaks out of the hierarchy, which can lead to leaks of coroutines and exceptions, and broken cancellation.

A simple extension function could improve things:

fun CoroutineScope.createJob(): Job = Job(parent = coroutineContext[Job])
fun CoroutineScope.createSupervisorJob(): SupervisorJob = SupervisorJob(parent = coroutineContext[Job])

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
zach-klippensteincommented, Sep 9, 2019

That binding API looks really nice for (1).

My main use case is (2) in your list however. I’m working on a library that uses Jobs under the hood to scope and cancel concurrent work (effectively a hierarchical worker pool). The work is configured in a tree, and so we create a parallel tree of Jobs. Multiple coroutines may be started later using that job as the parent. We’re creating Jobs explicitly, and not as part of an immediate launch or other new coroutine, so I don’t think this binding approach, as described above, wouldn’t solve my use case.

1reaction
qwwdfsadcommented, Sep 9, 2019

There was an attempt to add such helpers in #688. We decided to postpone it because creating a child out of thin air is a bit dangerous. For example, the following code

launch {
    childJob() // leftover after refactoring
}.join()

never completes because childJob will never be completed and without a proper debugger it may be really hard to pin down the problem.

Though writing something like SupervisorJob(coroutineContext[Job]) is not the best experience as well. We want to prototype #1406 to address this issue, for example, instead of

scope.launch(SupervisorJob(coroutineContext[Job])) {}

we will provide a flexible “BindJob” (name TBD, just to show the idea):

scope.launch(Bind(mode = Supervising)) {}
// or, TBD
scope.launch(Bind.Supervising)

Bind(mode = Supervising) knows how to bind itself as a supervising job between a parent scope and child job (launch).

It would be really helpful if you could elaborate on why you need such extensions because they have multiple use-cases:

  1. Launching a child with a specific cancellation mode (launch(createSupervisorJob))
  2. Creating an intermediate child in the hierarchy with multiple children:
fun createSubFragment() {
   val fragment = Fragment(createChildJob()) // Will be used as a field in the fragment
}
  1. …probably something else 😃

Understanding the most common patterns will help us a lot in designing the best possible solution in #1406

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring and Running a Job - Spring
Inheriting from a Parent Job​​ Similar to class inheritance in Java, a “child” Job combines its elements and attributes with the parent's. In...
Read more >
Choose when to run jobs - GitLab Docs
To configure a job to be executed only when the pipeline has been scheduled, use the rules keyword. In this example, make world...
Read more >
Pipeline as Code - Jenkins
Pipeline as Code describes a set of features that allow Jenkins users to define pipelined job processes with code, stored and versioned in...
Read more >
Create and Configure Jobs and Pipelines Using YAML
All YAML files must reside in the .ci-build directory in the root directory of any hosted Git ... if no errors are detected,...
Read more >
Constructs - AWS Cloud Development Kit (AWS CDK) v2
New features will be developed for CDK v2 exclusively. ... They also provide convenience methods that make it simpler to work with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found