-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support optional thread interrupt in newSingleThreadContext as its friends #57
Comments
@elizarov sorry to post here but I'm sure this could help a lot of other people. I really struggle to understand how to properly cancel blocking IO calls since normal usage does not interrupt thread. Goal would be convert some functions in a module to coroutines with blocking IO and some computation. I get the isActive/yield and ways to cancel the computations, but not how to push the cancel to the blocking IO call. If I understand correctly how to properly do the coroutines stuff it should be something like this
But from all that I read, cancelling the coroutine that call x() would still have to wait for the result to finish it's IO blocking call that only support thread interrupt) Is there any usable documentation / sample that fully cover this need (for Android so without Guava if possible) (Plan is not to use IO but an existing executor with asCoroutineDispatcher()) |
I meet the issue like in the references, so the following workaround was implemented:
It provides the similar behaviour like usual |
@khovanskiy This "works", but it is extremely dangerous code to use in a serious high-load production. Here's the implementation of that Ask you can see it does a classic "check & act" concurrent programming mistake: It interrupts a thread that was running this task at the time it had checked it, so under load it may, in fact, interrupt some other, unrelated piece of code. |
UPDATE: I stand corrected. False alarm. This particular usage of |
I think this ability is of great need on Android. According to
The practical usage on Android would be something like:
Line #1 may block for a relatively long time. If a lot of activities like this are entered and exited in a relatively short time, eg., someone is looking for interesting news and entering exiting news detail pages quickly without long stay, or someone is doing monkey test. The Dispatcher will be filled with canceled but blocked coroutines, so that new |
) See issue Kotlin#57 for details Signed-off-by: Trol <jiaoxiaodong@xiaomi.com>
) See issue Kotlin#57 for details Signed-off-by: Trol <jiaoxiaodong@xiaomi.com>
To implement this, I think, we could
|
I made a PR to implement this #1922
|
…ellation (Kotlin#57) This is implementation of issue Kotlin#57 and non-intrusive variant of Kotlin#1922 Signed-off-by: Trol <jiaoxiaodong@xiaomi.com>
PR (non-intrusive implementation of this feature): #1934 As mentioned above. This feature is of great use at least on Android as I could see. There is still another non-intrusive implementation of this feature: Add a function that makes a blocking code block cancellable (become a cancellation point of the coroutine) by interrupting the blocking code block and throwing CancellationException on coroutine cancellation. The added function is like:
|
👍 |
Is it possible for Kotlin Coroutines to handle thread interruption? |
@AndroidDeveloperLB yes, you could use |
@qwwdfsad This almost works. |
What do you mean by "forces you to cancel by interruption"? Can you elaborate, please? |
@elizarov Either you use On AsyncTask, you have a choice for every instance. |
See discussion here: https://discuss.kotlinlang.org/t/calling-blocking-code-in-coroutines/2368
The text was updated successfully, but these errors were encountered: