2.x: A.flatMapB to eagerly check for cancellations before subscribing #4992
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.
The
flatMap
implementations always subscribed to the generated inner source even if there was an asynchronous cancel while the function was running. With typical functions, there is only a really tiny window inside function but some users tend to block/sleep in the function and when that returns, theflatMap
operator is already cancelled.If the generated inner source emitted an error disregarding its own cancellation signal (the
error()
operators do this), those errors end up in theRxJavaPlugins.onError
and crash the app (on Android).This PR adjusts the
flatMap
implementations to check for the disposed/cancelled state before subscribing to the inner source. ForObservable
andFlowable
, this has practically no extra overhead as the add/remove already checks for the terminal state and can return a boolean for it. The rest require an explicitisDisposed()
check.