Code monitors: fix transaction rollbacks
Created by: camdencheek
This is a fun one.
I was seeing an issue where, when I create a code monitor, even if it failed, the monitor would still be created. We supposedly have transactional semantics in the creation code, so the transaction should be rolled back if there is a failure, but apparently that wasn't happening. Scary
Turns out the issue was that err
was being declared in the function body, and the defer func() { err = tx.Done(err) }
was referring to that declared err
variable. This would be fine, except we make a new scope on line 150, which makes the declaration on line 151 create a new variable. This means when we assign to it, we aren't assigning to the variable that is being read by tx.Done()
.
I fixed this by declaring the variable in the method signature so that the error referenced by tx.Done()
will always be whatever error is returned with return
.
Test plan
Added a test to make sure that a failed create actually rolls back the transaction.