Skip to content

Commit 90f2963

Browse files
domdomeggclaude
andcommitted
Make rollback resilient to context cancellation
Use independent context for transaction rollbacks to ensure they complete even if the parent request is cancelled. Also log rollback errors for debugging. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 5721266 commit 90f2963

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal/database/postgres.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"log"
89
"strings"
910
"time"
1011

@@ -426,7 +427,11 @@ func (db *PostgreSQL) InTransaction(ctx context.Context, fn func(ctx context.Con
426427
return fmt.Errorf("failed to begin transaction: %w", err)
427428
}
428429
defer func() {
429-
_ = tx.Rollback(ctx)
430+
rollbackCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
431+
defer cancel()
432+
if rbErr := tx.Rollback(rollbackCtx); rbErr != nil && !errors.Is(rbErr, pgx.ErrTxClosed) {
433+
log.Printf("failed to rollback transaction: %v", rbErr)
434+
}
430435
}()
431436

432437
if err := fn(ctx, tx); err != nil {

0 commit comments

Comments
 (0)