Skip to content

Commit 4ceab0f

Browse files
committed
Ensure Watch closes the transaction on panic
1 parent a8e8bf1 commit 4ceab0f

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

tx.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,13 @@ func (c *Tx) Process(ctx context.Context, cmd Cmder) error {
6565
// The transaction is automatically closed when fn exits.
6666
func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
6767
tx := c.newTx(ctx)
68+
defer tx.Close(ctx)
6869
if len(keys) > 0 {
6970
if err := tx.Watch(ctx, keys...).Err(); err != nil {
70-
_ = tx.Close(ctx)
7171
return err
7272
}
7373
}
74-
75-
err := fn(tx)
76-
_ = tx.Close(ctx)
77-
return err
74+
return fn(tx)
7875
}
7976

8077
// Close closes the transaction, releasing any open resources.

0 commit comments

Comments
 (0)