Fix Bug In ZStream#flatMapPar #5325
Merged
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.
Currently it is possible for
flatMapParto signal the end of the stream even though a failure has occurred.This is possible because the following line assumes that if acquiring the permits wins the race against awaiting the inner failure then all streams have completed successfully and we can signal stream completion.
However, this is not necessarily the case because even if the left side is already complete the right side could still win the race.
To address this, we change the logic of the inner stream acquiring the permit so that it only releases the permit in the event of success. The main purpose of the permits is to control parallelism. If one inner stream has failed then all inner streams are in the process of being shut down so there is no point in releasing a permit to allow a new stream to be created. In the event of failure the inner stream completes the inner failure promise to signal to the outer stream, ensuring that it is always the case that either the permit is released or failure is signaled.