Skip to content

Commit

Permalink
Merge pull request #1096 from functional-streams-for-scala/bugfix/dou…
Browse files Browse the repository at this point in the history
…ble-close-scope

Do no throw exception when scope is not anymore in hierarchy.
  • Loading branch information
mpilquist authored Feb 26, 2018
2 parents 3eb6e97 + a9f8a1d commit d3e83a9
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions core/shared/src/main/scala/fs2/internal/Algebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,23 @@ private[fs2] object Algebra {
case close: Algebra.CloseScope[F, O] =>
if (close.interruptFallback) onInterrupt(Right(close.scopeId))
else {
scope.findSelfOrAncestor(close.scopeId) match {
case Some(toClose) =>
F.flatMap(toClose.close) { r =>
F.flatMap(toClose.openAncestor) { ancestor =>
compileLoop(ancestor, f(r))
}
def closeAndGo(toClose: CompileScope[F, O]) =
F.flatMap(toClose.close) { r =>
F.flatMap(toClose.openAncestor) { ancestor =>
compileLoop(ancestor, f(r))
}
}

scope.findSelfOrAncestor(close.scopeId) match {
case Some(toClose) => closeAndGo(toClose)
case None =>
val rsn = new IllegalStateException(
"Failed to close scope that is not active scope or ancestor")
compileLoop(scope, f(Left(rsn)))
scope.findSelfOrChild(close.scopeId).flatMap {
case Some(toClose) => closeAndGo(toClose)
case None =>
// indicates the scope is already closed
compileLoop(scope, f(Right(())))
}

}
}

Expand Down

0 comments on commit d3e83a9

Please sign in to comment.