Skip to content

Use DirectExecutor for Task.addOnCompleteListener #2992

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

Merged
merged 1 commit into from
Oct 26, 2021

Conversation

alexvanyo
Copy link
Contributor

The current version of Task.addOnCompleteListener that is being used schedules the listener to run on the main thread. Because we are completing a Deferred or canceling or resuming a continuation, the listener can be run on any thread and therefore we don't need to introduce an unnecessary hop to the main thread if the rest of the work is happening on other threads.

Implementation notes:

  • MoreExecutors.directExecutor exists, but I implemented it directly since it is an extremely simple implementation and that avoids introducing any dependency.

  • Because we no longer touch the main thread in tests, I was able to remove the FakeAndroid file that was faking the Handler and Looper

This fixes #2990

@alexvanyo alexvanyo changed the title Use DirectExecutor for addOnCompleteListener Use DirectExecutor for Task.addOnCompleteListener Oct 19, 2021
@qwwdfsad qwwdfsad self-requested a review October 22, 2021 12:43
Copy link
Member

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Though a lot of tests fail with java.lang.NoClassDefFoundError: android/os/Handler as some parts of the stub library still use TaskExecutors.MAIN_THREAD, e.g. TaskCompletionSource(token) constructor, please address that (probably by returning FakeAndroid back)

@alexvanyo
Copy link
Contributor Author

Ah, yep I can reproduce locally after a clean build, not sure how those passed for me before.

I added FakeAndroid back, and rebased against develop to pull in the changes #2996.

@qwwdfsad qwwdfsad self-requested a review October 26, 2021 13:43
Copy link
Member

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@qwwdfsad qwwdfsad merged commit 1004f39 into Kotlin:develop Oct 26, 2021
@alexvanyo alexvanyo deleted the av/direct-executor branch October 26, 2021 16:32
@pmoons
Copy link

pmoons commented Oct 27, 2021

Curious when the next release will be cut that includes this change. I have a feeling .await() is currently blocking execution in a test I am trying to write because it is trying to execute on the main thread.

https://stackoverflow.com/questions/69693218/how-to-run-an-integration-test-for-the-repository-layer-of-an-android-app-with-f

yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this pull request Jan 28, 2022
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants