From 4ceab0fee3dc62e888fb2fecac48cbf3f5191fbb Mon Sep 17 00:00:00 2001 From: Adrian-Stefan Mares Date: Thu, 21 Jan 2021 19:29:28 +0200 Subject: [PATCH] Ensure Watch closes the transaction on panic --- tx.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tx.go b/tx.go index ad825c6..08d381a 100644 --- a/tx.go +++ b/tx.go @@ -65,16 +65,13 @@ func (c *Tx) Process(ctx context.Context, cmd Cmder) error { // The transaction is automatically closed when fn exits. func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error { tx := c.newTx(ctx) + defer tx.Close(ctx) if len(keys) > 0 { if err := tx.Watch(ctx, keys...).Err(); err != nil { - _ = tx.Close(ctx) return err } } - - err := fn(tx) - _ = tx.Close(ctx) - return err + return fn(tx) } // Close closes the transaction, releasing any open resources.