Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Currently it is possible for flatMapPar to 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.

innerFailure.await.interruptible.raceWith(permits.withPermits(n.toLong)(ZIO.unit).interruptible)(???)

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.

@jdegoes jdegoes merged commit ec493b2 into zio:master Jul 27, 2021
@adamgfraser adamgfraser deleted the mapmparunordered branch July 27, 2021 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behavior of mapMParUnordered with failures

2 participants