Skip to content
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

Fail-fast when trying to do runBlocking from Android UI thread #227

Closed
elizarov opened this issue Feb 6, 2018 · 11 comments
Closed

Fail-fast when trying to do runBlocking from Android UI thread #227

elizarov opened this issue Feb 6, 2018 · 11 comments
Assignees

Comments

@elizarov
Copy link
Contributor

elizarov commented Feb 6, 2018

Android UI thread does not generally allow blocking operations and vaiours things like doing network IO from UI thread throws an exception on Android. Similarly, using runBlocking from Android UI thread shall throw an exception to fail-fast and prevent hard-to-diagnose bugs when the code just "happens to run fast" during testing but can in fact hang the UI in production.

@Groostav
Copy link
Contributor

Can I request this apply to JavaFX also?

@elizarov
Copy link
Contributor Author

It indeed makes sense for all UI dispatchers.

@deva666
Copy link
Contributor

deva666 commented Apr 10, 2018

@elizarov is this up for grabs? I'd be interested in working on this

@elizarov
Copy link
Contributor Author

@deva666 You can try to work out solution. I have not though too deep about possible implementation strategies, so I don't know if it will turn out to be simple or not.

@jcornaz
Copy link
Contributor

jcornaz commented Jul 10, 2018

I have to say I like the Idea. But why only forbid runBlocking on UI threads? Blocking a thread of the fork-join-pool is also bad practice.

And actually, even if I like very much the Idea I have some fears about our usage. In our codebase we are slowly adding coroutines and improving our code base to block as less often as possible. But we have to live with an existing code base which has legacy code blocking the UI (without using coroutine). And problems arise when we convert some code or add new code using coroutines. Even if we try as much as possible to make the code not blocking the UI anymore, it is sometime to much work and we simply put the suspending code in runBlocking and create a refactoring issue for later. After all, the code under modification was already blocking the UI, and we cannot completely rewrite the application each time we add a small feature. And at the end, it is still nice to use runBlocking, because it makes it obvious that the code is blocking and that further refactoring is needed.

But if kotlinx.coroutines sudently start to throw exceptions when we use runBlocking in UI thread what should we do? Our code base is too massive to make it completely non-ui-blocking in a reasonable amount of time.

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Jul 25, 2018

@jcornaz You raised an important issue, thank you

In general, blocking UI thread is a no go, so we prefer to have this check enabled by default.
But nevertheless, your problem is clear. Will system property which disables this feature be a good solution for you?

@jcornaz
Copy link
Contributor

jcornaz commented Jul 25, 2018

Will system property which disables this feature be a good solution for you?

Yes I thought about that too. It would be great.

@elizarov
Copy link
Contributor Author

Shall we open a separate issue for a property, since this issue already sits on the release train and will be closed soon?

@elizarov
Copy link
Contributor Author

@jcornaz See #458

@svenjacobs
Copy link

I have a bit of coroutine code that needs to be run in a blocking fashion when my Android application starts. For instance in my Application class I need to fetch a value from a suspending function. Based on the result of that value certain application options are configured. This should actually block because the configuration needs to be finished before/while the application starts. This worked well before version 0.24 and was not noticeable.

How do I solve this use case now?

@elizarov
Copy link
Contributor Author

elizarov commented Aug 3, 2018

Let's discuss a solution in #464

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants