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

Do not propagate cancellation to the upstream in Flow flat* operators #2964

Merged
merged 2 commits into from
Oct 20, 2021

Conversation

qwwdfsad
Copy link
Contributor

@qwwdfsad qwwdfsad commented Oct 1, 2021

A new mental model is now perfectly aligned with the rest of the coroutines:

  • Where one [semantically] expects a new child to be launched (e.g. in merge or in flatMap*), the cancellation works in one direction as expected -- cancellation of the child is not propagated to the parent. It's especially important when the "virtual child" (flow returned by flat*) can have a different cancellation scope (e.g. it can be a separate UI fragment)
  • Where everything happens in-place, e.g. flattenConcat, the behaviour stays the same

Fixes #2942

@qwwdfsad qwwdfsad requested review from elizarov and removed request for elizarov October 1, 2021 14:31
@qwwdfsad qwwdfsad force-pushed the cancellable-flat-map-operators branch from 7dcefb9 to f3ac536 Compare October 8, 2021 12:46
@qwwdfsad qwwdfsad changed the title Non-propagating cancellation of @FlowPreview operators @qwwdfsad Do not propagate cancellation to the upstream in Flow flat* operators Oct 8, 2021
@qwwdfsad qwwdfsad force-pushed the cancellable-flat-map-operators branch from f3ac536 to 0c2d4e0 Compare October 8, 2021 13:37
@qwwdfsad qwwdfsad requested a review from dkhalanskyjb October 8, 2021 13:37
@qwwdfsad qwwdfsad marked this pull request as ready for review October 8, 2021 13:37
@qwwdfsad qwwdfsad changed the title @qwwdfsad Do not propagate cancellation to the upstream in Flow flat* operators Do not propagate cancellation to the upstream in Flow flat* operators Oct 8, 2021
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

The docs for flattenConcat still state that it's just an optimized flattenMerge(concurrency = 1), but they now have different downstream cancellation behavior. I didn't check the other operators to see if their docs also need to be updated.

@qwwdfsad
Copy link
Contributor Author

I've updated the doc.

flatMapMerge and flattenMerge indeed now have an observable difference in cancellation behaviour for concurrency equal 1 and > 1. This is a deliberate design decision as the fact that concurrency == 1 is optimized into sequential transformation. Sequential -> no child coroutines -> in place invocations -> in-place cancellation is propagated

@qwwdfsad qwwdfsad requested a review from dkhalanskyjb October 20, 2021 11:23
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

Yeah, I didn't have issues with the change here, only with the fact that the documentation would now be misleading.

@qwwdfsad qwwdfsad merged commit 80af499 into develop Oct 20, 2021
@qwwdfsad qwwdfsad deleted the cancellable-flat-map-operators branch October 20, 2021 11:31
hoc081098 added a commit to hoc081098/FlowExt that referenced this pull request Nov 23, 2021
yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this pull request Jan 28, 2022
…Kotlin#2964)

* Do not propagate cancellation to the upstream in Flow flat* operators

Fixes Kotlin#2964
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
…Kotlin#2964)

* Do not propagate cancellation to the upstream in Flow flat* operators

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

Successfully merging this pull request may close these issues.

2 participants