-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Document and fix dispatchYield codepath #4255
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you're proposing is better than what we have now, but it's still not ideal. We are just one step farther from seeing the same effect.
LoggingDispatcher(Dispatchers.IO)
will exhibit this behavior, as the author ofLoggingDispatcher
can't overridedispatchYield
. That's a problem we're also facing withDelay
(people can't wrapDelay
implementations and expect the same behavior).I see the following options of dealing with this problem for good:
YieldContext
marker used byDispatchers.Unconfined
available to all dispatchers (and not just those not needing a dispatch) and check its presence whendispatch
'ing to the coroutine scheduler; removedispatchYield
completely. Probably too slow to check that on every dispatch?dispatchYield
.dispatchYield
a valid optimization in a wide range of scenarios (including things like "skip suspensions onDispatchers.Main
if the looper is empty anyway") by recognizing the coroutine not being yet dispatched to the correct execution context as a separate state. EDIT: ... and makedispatchYield
public, as it will no longer be deeply tied to our implementation details.Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, and it seems like the proper solution includes a solution for
Delay
as well.In general, it's not really sustainable to include
@Internal*
methods or superinterfaces into the class that we expect people to extend.Yes, this option we'd rather avoid -- quite a non-deterministic check for the sake of code path that is really rare.
I would merge this as is and, as a next step, would consider sticking to the third option.
Probably, making
dispatchYield
public should be enough, not sure about theyield
optimization (as it's quite niche)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea actually wasn't to introduce the optimizations (they are even more niche than even
yield()
, which itself is niche) (though introducing the optimizations actually shouldn't be difficult) but to ensure that if someone does overridedispatchYield
in favor of a non-trivial optimized implementation, that implementation wouldn't break the way ours did in #4248. If we introducedispatchYield
for public implementation without fixing the state machine, the most obvious usage ofdispatchYield
becomes a footgun.