2.x: Fix Flowable.publish(-|Function) subscriber swap possible data loss #5893
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.
This PR fixes an avoidable dataloss in the following subscriber-swap scenario with the
publish()
andpublish(Function<Flowable, Publisher>)
.When an
onNext
changes the current array of subscribers (an existing consumer cancelled or a new one arrived), the change detection is delayed and items may get dropped even though a fresh consumer could take those values.The algorithms were updated to fix this case as well as the emission tracking in
FlowableMulticastProcessor
: a globalemitted
value is not good here after all (subscribers joining late would indicate an incorrect aggregate demand and get overflown).One of the advanced uses of
publish(Function)
is to implement consumption mode changes by having a mode cancel the subscription to the sharedFlowable
and synchronously subscribe a new consumer with a different behavior.The discovery of this shortcoming was due to a special transformation pattern: apply a transformer if the source is not empty, and in this case, deliver all items of it.