3.x: Fix delay-cancellation race producing random subsequent emissions #7855
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.
When a sequence using
delay
is cancelled asynchronously, there is a race between cancelling each individualFuture
inside the executor and them running, causing skips in theonNext
emissions.For example, let's say there are 3 futures waiting to be executed. The Future-1 triggers the cancellation asynchronously and emits
1
. This asynchronous routine will start cancelling Future-2 and Future-3 one by one. Concurrently, the executor will look at Future-2, see it cancelled and moves onto Future-3, which is still executable as the asynchronous routine hasn't gotten to cancel Future-3. Now Future-3 runs and emits3
. This will appear to produce a sequence of[1, 3]
where2
is missing.The fix is to hard-check for a disposed worker and prevent the
onNext
emissions in the operator.Resolves #7851