arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Saga with raise inside SagaActionStep don't rollback

Open outlndrr opened this issue 1 year ago • 3 comments

Hi, there is a problem if raise happened inside SagaActionStep then saga don't rollback action, but return Either.Left. Example code:

suspend fun main() {
    // perform the transaction
    val result = either {
        saga {
            saga({
                raise("failed")
                println("Action A")
            }) { println("Rollback A") }
        }.transact()
    }
    println(result)
}

Arrow version: 1.2.4

outlndrr avatar Jun 18 '24 14:06 outlndrr

What printout are you getting, and what's your expected output?

kyay10 avatar Jun 18 '24 16:06 kyay10

What printout are you getting, and what's your expected output?

It printed Either.Left and that was all. I expected it to run a rollback and print "Rollback A" and return Either.Left.

outlndrr avatar Jun 19 '24 05:06 outlndrr

@outlndrr that should indeed result in rolling back the action. Thank you for reporting this issue, I think this is fixed on 2.0.0 but might be missing a test case. So leaving this issue open!

nomisRev avatar Jun 19 '24 06:06 nomisRev

@outlndrr the behavior you see is actually correct. A saga block will undo every action which has succeeded, and the block where you have raise is not considered successful yet, since it hasn't fully executed. Think of the case of a database, if some part of the transaction has failed, you don't have to roll that one back, since it's already failed.

I've added a few tests as part of 103848035bf382126b10857136e8fc2fc1657286, and you can see there that if you have other saga blocks before the one that raises, those are rolled back as expected.

serras avatar Jan 26 '25 07:01 serras