Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Stream#parJoin for short-circuiting monad transformers #2895

Merged
merged 7 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4049,16 +4049,22 @@ object Stream extends StreamLowPriority {
.compile
.drain
.guaranteeCase(oc =>
lease.cancel
.flatMap(onOutcome(oc, _)) >> available.release >> decrementRunning
lease.cancel.rethrow.guaranteeCase {
case Outcome.Succeeded(fu) =>
onOutcome(oc <* Outcome.succeeded(fu), Either.unit)

case Outcome.Errored(e) =>
onOutcome(oc, Either.left(e))

case _ =>
F.unit
}
)
.handleError(_ => ())
.forceR(available.release >> decrementRunning)
}.void
}
}

val RightUnit = Right(())

def runOuter: F[Unit] =
F.uncancelable { _ =>
outer
Expand All @@ -4071,7 +4077,7 @@ object Stream extends StreamLowPriority {
.interruptWhen(done.map(_.nonEmpty))
.compile
.drain
.guaranteeCase(onOutcome(_, RightUnit) >> decrementRunning)
.guaranteeCase(onOutcome(_, Either.unit) >> decrementRunning)
.handleError(_ => ())
}

Expand Down Expand Up @@ -4107,7 +4113,7 @@ object Stream extends StreamLowPriority {
signalResult(fiber)
}
.flatMap { _ =>
output.stream.flatMap(Stream.chunk(_).covary[F])
output.stream.flatMap(Stream.chunk)
}
}

Expand Down
24 changes: 24 additions & 0 deletions core/shared/src/test/scala/fs2/StreamParJoinSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@ class StreamParJoinSuite extends Fs2Suite {
}
}

test(
"do not block while evaluating a stream in EitherT[IO, Throwable, *] with multiple parJoins"
) {
case object TestException extends Throwable with NoStackTrace
type F[A] = EitherT[IO, Throwable, A]

Stream
.range(1, 64)
.covary[F]
.map(i =>
Stream.eval[F, Unit](
if (i == 7) EitherT.leftT(TestException)
else EitherT.right(IO.unit)
)
)
.parJoin(4)
.map(_ => Stream.eval[F, Unit](EitherT.right(IO.unit)))
.parJoin(4)
.compile
.drain
.value
.assertEquals(Left(TestException))
}

test("do not block while evaluating an EitherT.left outer stream") {
case object TestException extends Throwable with NoStackTrace

Expand Down