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.

Redundant 'async' call may be reduced to 'kotlinx.coroutines.withContext'

See original GitHub issue

I have the following code:

DATABASE_CONTEXT.async(block = block).await()

IntelliJ gives me a warning and wants to simplify it to use withContext instead of async.

These two things aren’t exactly equivalent though, right? i.e. if I cancel the coroutine, it will also stop the coroutine code from executing if I user withContext, but if I use DATABASE_CONTEXT.async(block = block).await(), that has a new coroutine hierarchy, and that will ensure that the code inside the block runs to completion, right?

Those are two very important distinctions, and I like to differentiate between the use-cases. The fact that the IDE is suggesting these to me as equivalent seems like an error to me.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
elizarovcommented, Sep 6, 2019

Thanks a lot! It is also good to know that this architecture works well with withContext. As for “wrapping plugin” – we indeed periodically run into cases where this or that kind of wrapping might be needed and some day this kind of “auto-wrapping” might become a separate language feature.

0reactions
qwwdfsadcommented, Aug 15, 2020

When I apply the fix IntelliJ suggests, merely saying withContext { jmsconsumer.receive() }, it works, but if the job hierarchy gets cancelled, this one is stuck forever.

It gets stuck anyway, but in case of async you just don’t wait for it on the outer scope. So this behaviour is rather uncovered a potential leak in your code. If JMS supports interruption, I would recommend using runInterruptible to support cooperative cancellation of JMS

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin: withContext() vs Async-await - Stack Overflow
async (context) : Starts a new coroutine in the given context and if we call .await() on the returned Deferred task, it will...
Read more >
Kotlin withContext() vs. async-await - Baeldung
Coroutines are strong tools for writing asynchronous code with a fluent API in a sequential style without the headache of reactive style coding....
Read more >
Settling the Async-Await v withContext debate in Kotlin ...
One such debate is the Async-Await v withContext to perform background work. ... Redundant 'async' call may be reduced to 'kotlinx.coroutines.withContext'.
Read more >
Consider removing "Redundant 'async' call may be reduced ..."
This inspection and further quick-fix produce non-equivalent code, we should consider removing it completely. suspend fun test(ctx: CoroutineContext, ...
Read more >
withContext - Kotlin
suspend fun <T> withContext(context: CoroutineContext, block: suspend ... Calls the specified suspending block with a given coroutine context, ...
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