Skip to content

Ensure awaitFrame() only awaits a single frame when used from the main looper #3437

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 4 commits into from
Sep 16, 2022

Conversation

qwwdfsad
Copy link
Collaborator

@qwwdfsad qwwdfsad commented Sep 5, 2022

See #3431 for the additional context

@qwwdfsad qwwdfsad removed the request for review from dkhalanskyjb September 5, 2022 19:41
@qwwdfsad qwwdfsad marked this pull request as draft September 5, 2022 19:41
@qwwdfsad qwwdfsad marked this pull request as ready for review September 6, 2022 09:41
@qwwdfsad qwwdfsad changed the title Improve handling of undispatched resume Ensure awaitFrame() only awaits a single frame when used from the main looper Sep 8, 2022
Comment on lines 118 to 121
mainLooper.scheduler.advanceBy(50, TimeUnit.MILLISECONDS)
expect(5)
mainLooper.scheduler.advanceBy(51, TimeUnit.MILLISECONDS)
finish(6)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do these lines test anything?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does not seem so


private suspend fun awaitFrameSlowPath(): Long = suspendCancellableCoroutine { cont ->
if (Looper.myLooper() === Looper.getMainLooper()) { // Check if we are already in the main looper thread
updateChoreographerAndPostFrameCallback(cont)
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb Sep 15, 2022

Choose a reason for hiding this comment

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

I saw you mention Dispatchers.Main.immediate not being good enough for this and something about intending to fix it. Could you elaborate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, I was hoping that adding Dispatchers.main.immediate would solve the problem, but it won't due to how we do structure our code: CancellableContinuation already incapsulates the dispatcher (for this case -- Dispatchers.Main) and we cannot "just" resume it in place.

Neither can we do Dispatchers.Main.immediate.resumeUndispatched(cont) due to how it is implemented -- that's a grey area in "how should 'resumeUndispatched' behave if extension receiver and actual dispatcher do not match". And they do not match because Dispatchers.Main.immediate and Dispatchers.Main are different dispatchers, non-transparent to each other

expect(5)
mainLooper.scheduler.advanceBy(51, TimeUnit.MILLISECONDS)
finish(6)
finish(5)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Then it's finish(4), as the action after expect(4) also does nothing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@qwwdfsad qwwdfsad merged commit 5322c8d into develop Sep 16, 2022
@qwwdfsad qwwdfsad deleted the better-await-frame branch September 16, 2022 13:50
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