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.

Shouldn't join resume after onCompletion?

See original GitHub issue

Hello,

please consider the following code:

fun foo() {
  Thread.sleep(1000)
  println("alright this have been executed")
}

fun main(args: Array<String>) {
  val job = Job()

  job.cancel()

  GlobalScope.launch(job) {
    try {
      println("do stuff")
    } finally {
      foo()
    }
  }

  runBlocking {
    println("before join")
    job.cancelAndJoin()
    println("afterJoin")
  }
}

foo() is not invoked at all, because the job is cancelled before being scheduled.

So what are my options, if I want to make sure foo() is always executed?

One may try to use onCompletion like this:

fun foo() {
  Thread.sleep(1000)
  println("alright this have been executed")
}

fun main(args: Array<String>) {
  val job = Job()

  job.cancel()

  GlobalScope.launch(job, onCompletion = { foo() }) { println("do stuff") }

  runBlocking {
    println("before join")
    job.cancelAndJoin()
    println("afterJoin")
  }
}

But it doesn’t help, because join doesn’t seem to care if onCompletion have been executed or not.

And here comes my question: Shouldn’t join resume only after the execution onCompletion?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
elizarovcommented, Sep 28, 2018

Ops. We need to promote CoroutineStart.ATOMIC from internal to experimental.

1reaction
elizarovcommented, Sep 27, 2018

For now, start = ATOMIC with try / finally { ... } is the only guaranteed way to make sure that something is done before other coroutines that .join resume. In the future we might add back support or something similar to onCompletion, but it has to be composable (don’t want to hack every coroutine builder again).

Btw, if you are only interested in exceptions, you can install CorouinteExceptionHandler. It is guaranteed to be invoked before all joining coroutines can resume.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stop Wasting Time on Your Resume: A Lesson From The ...
While phenomenal resume tips (and debates) abound, there's one point everyone seems to miss: Out of all the job search activities, ...
Read more >
invokeOnCompletion - Kotlin
Registers handler that is synchronously invoked once on completion of this job. ... There is no need to dispose the handler after completion...
Read more >
Waiting until the task finishes - swift - Stack Overflow
It will wait until the loop finishes but in this case your main thread will block. You can also do the same thing...
Read more >
MediaPlayer - Android Developers
A MediaPlayer object must first enter the Prepared state before playback can be started. ... MediaPlayer is resuming playback after filling buffers.
Read more >
15 Things You Should Not Include in a Resume - TopResume
What you decide to not include in a resume is just as important as what you choose to include. Here's a list to...
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