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.

Plain Job doesn't wait for children on join()

See original GitHub issue

A Job that isn’t backed by a coroutine doesn’t seem to wait for its children to complete when join() is called after cancelling. Given the following tests, I would expect them both to pass but only one does.

  private fun runTest(cancelAndJoinBlock: suspend (Job) -> Unit) {
    runBlocking {
      val parentJob = Job()

      val finishedLast = AtomicReference<String>()

      launch(parent = parentJob) {
        // Something "long-running" that doesn't suspend
        Thread.sleep(500)
        finishedLast.set("child")
      }

      // Give the child enough time to have started
      delay(200, TimeUnit.MILLISECONDS)

      cancelAndJoinBlock(parentJob)
      finishedLast.set("join")

      // Give the child enough time to have finished
      delay(1, TimeUnit.SECONDS)

      // The child should have finished before the join on the parent returned
      assertEquals("join", finishedLast.get())
    }
  }

  // This one fails
  @Test fun parentJobWithCancelAndJoin() {
    runTest { parentJob ->
      parentJob.cancel()
      parentJob.join()
    }
  }

  // This one passes
  @Test fun parentJobWithCancelAndJoinChildren() {
    runTest { parentJob ->
      parentJob.cancelChildren()
      parentJob.joinChildren()
    }
  }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Jan 12, 2018

After investigation I’ve found that this is a bug and it will be fixed in the next update. When Job is cancelled it should go into cancelling state while it has children and the join should wait until it becomes cancelled (that is, when all its children become cancelled).

0reactions
elizarovcommented, Jan 10, 2018

It does make sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between launch/join and async/await in ...
launch with join is used to wait until the job gets finished. It simply suspends the coroutine calling join() , leaving the current...
Read more >
Job and children awaiting in Kotlin Coroutines - Kt. Academy
The first important advantage of a job is that it can be used to wait until the coroutine is completed. For that, we...
Read more >
Waiting for Coroutines in Kotlin - DZone
Joining all jobs running inside a coroutine is not a requirement to ensure their completion is waited for. By using the coroutineScope builder...
Read more >
Why your multiprocessing Pool is stuck (it's full of sharks!)
Some code that ought to work, but doesn't. Unfortunately, while the Pool class is useful, it's also full of vicious sharks, just waiting...
Read more >
Adult Kids Living At Home | Managing Failure To Launch ...
Are you one of the many frustrated parents with their adult kids still living at home? Our experts provide insight on managing failure...
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