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.

Async is not cancelled

See original GitHub issue

I have a function which load the image by click on button. If button is clicked twice (second click before async completion) it’s needed to cancel old async and start new. Code example:

private var deferred: Deferred<Bitmap>? = null
override fun onCreate(inState: Bundle?) {
        super.onCreate(inState)
        findViewById<View>(R.id.btn_action).setOnClickListener {
            if (deferred != null && deferred?.isActive == true) {
                Log.d("debug", "cancel")
                deferred?.cancel()
            }
            loadImage()
        }
    }

    private fun loadImage() = launch(UI) {
        Log.d("debug", "start")
        deferred = async {
           val conn = URL("http://elitefon.ru/pic/201503/2560x1600/elitefon.ru-38503.jpg").openConnection() as HttpURLConnection
            val bitmap = BitmapFactory.decodeStream(conn.inputStream)
            Log.d("debug", "end of async")
            return@async bitmap
        }
        deferred?.await()
        Log.d("debug", "loaded")
    }

In this case log will be something like this:

start
cancel
start
end of async
end of async
loaded

As we can see, in spite of cancel, “end of async” log is called twice. Also, try/catch logging shown that CancelationException is throws in the launch(UI) block (not the async block). Can I make it so that if I cancel async, the exception was thrown in the async block?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
SamKotlinFishercommented, Apr 11, 2018

@fvasco, thanks for your answer. I developed the similar solution:

fun <T> async (block: () -> T): Deferred <T?> {
     var thread: Thread? = null
     val deferred = async (CachedPool) {
         thread = Thread.currentThread ()
         return @async block.invoke ()
     }
     deferred.invokeOnCompletion (true, true) {
         if (deferred.isCancelled && thread! = null && thread? .isAlive == true) {
             thread? .interrupt ()
             thread = null
         }
     }
     return deferred
}

What do you think about this?

0reactions
SamKotlinFishercommented, Apr 12, 2018

@fvasco, thank you so much for your answer. I hope that this functionality will be able in kotlin-coroutine in a future as are described in #57.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swift async let cancellation doesn't work - Stack Overflow
Your "sleep" is not "cancellable", meaning it does not check any "cancellation point", thus it will run till completion. Please read Task ...
Read more >
Perspectives on Async Cancellation - Eric Holk
Lately there has been a lot of discussion about async cancellation in Rust. One thing I've noticed in these discussions is that cancellation...
Read more >
Async Cancellation I — 2021-11-10 - Yoshua Wuyts
This means the first cancellation point of any future is immediately after instantiation before the async functions's body has run.
Read more >
Async let cancellation - Bug? (confused) - Using Swift
The child task is marked as cancelled, but it is running in the "background" and the parent task is not notified of its...
Read more >
Cancel async tasks after a period of time (C#) - Microsoft Learn
You can cancel an asynchronous operation after a period of time by using the CancellationTokenSource.CancelAfter method if you don't want 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