From 933607bd5248c2a2c86da101d09f2199074b28ab Mon Sep 17 00:00:00 2001 From: Youssef Shoaib Date: Fri, 1 Nov 2024 17:21:49 +0000 Subject: [PATCH] Call `finalizer` in one place in `finalizeCase` --- .../commonMain/kotlin/arrow/fx/coroutines/Bracket.kt | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt index fe296f85744..c8209e9ffcd 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Bracket.kt @@ -224,17 +224,14 @@ internal suspend inline fun runReleaseAndRethrow(original: Throwable?, crossinli @PublishedApi internal inline fun finalizeCase(block: () -> R, finalizer: (ExitCase) -> Unit): R { - var finished = false + var exitCase: ExitCase = ExitCase.Completed return try { block() } catch (e: Throwable) { - finished = true - if (e !is CancellationException) e.nonFatalOrThrow() - finalizer(ExitCase.ExitCase(e)) + exitCase = ExitCase.ExitCase(e) throw e } finally { - if (!finished) { - finalizer(ExitCase.Completed) - } + if (exitCase is ExitCase.Failure) exitCase.failure.nonFatalOrThrow() + finalizer(exitCase) } }