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
Member

@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 Oct 1, 2021
@qwwdfsad qwwdfsad force-pushed the cancellable-flat-map-operators branch from 7dcefb9 to f3ac536 Compare Oct 8, 2021
@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 Oct 8, 2021
@qwwdfsad qwwdfsad requested a review from dkhalanskyjb Oct 8, 2021
@qwwdfsad qwwdfsad marked this pull request as ready for review Oct 8, 2021
@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
Member

@dkhalanskyjb dkhalanskyjb left a comment

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
Member Author

@qwwdfsad qwwdfsad commented Oct 20, 2021

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 Oct 20, 2021
Copy link
Member

@dkhalanskyjb dkhalanskyjb left a comment

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
1 check was pending
@qwwdfsad qwwdfsad deleted the cancellable-flat-map-operators branch Oct 20, 2021
hoc081098 added a commit to hoc081098/FlowExt that referenced this issue Nov 23, 2021
yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this issue Jan 28, 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
Development

Successfully merging this pull request may close these issues.

None yet

2 participants