Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processing of panics in retriers #1609

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions retry/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func DoTx(ctx context.Context, db *sql.DB, op func(context.Context, *sql.Tx) err
// DoTxWithResult is a retryer of database/sql transactions with fallbacks on errors
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func DoTxWithResult[T any](ctx context.Context, db *sql.DB, //nolint:funlen
func DoTxWithResult[T any](ctx context.Context, db *sql.DB,
op func(context.Context, *sql.Tx) (T, error),
opts ...doTxOption,
) (T, error) {
Expand Down Expand Up @@ -211,17 +211,7 @@ func DoTxWithResult[T any](ctx context.Context, db *sql.DB, //nolint:funlen
return zeroValue, unwrapErrBadConn(xerrors.WithStackTrace(err))
}
defer func() {
if finalErr == nil {
return
}
errRollback := tx.Rollback()
if errRollback == nil {
return
}
finalErr = xerrors.NewWithIssues("",
xerrors.WithStackTrace(finalErr),
xerrors.WithStackTrace(fmt.Errorf("rollback failed: %w", errRollback)),
)
_ = tx.Rollback()
}()
v, err := op(xcontext.MarkRetryCall(ctx), tx)
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions retry/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -233,3 +234,61 @@ func TestDoTx(t *testing.T) {
})
}
}

func TestCleanUpResourcesOnPanicInRetryOperation(t *testing.T) {
panicErr := errors.New("test")
t.Run("Do", func(t *testing.T) {
m := &mockConnector{
t: t,
}
db := sql.OpenDB(m)
defer func() {
require.NoError(t, db.Close())
}()
require.Panics(t, func() {
require.Equal(t, 0, db.Stats().OpenConnections)
require.Equal(t, 0, db.Stats().Idle)
require.Equal(t, 0, db.Stats().InUse)
defer func() {
require.Equal(t, 1, db.Stats().OpenConnections)
require.Equal(t, 1, db.Stats().Idle)
require.Equal(t, 0, db.Stats().InUse)
}()
_ = Do(context.Background(), db,
func(ctx context.Context, cc *sql.Conn) error {
require.Equal(t, 1, db.Stats().OpenConnections)
require.Equal(t, 0, db.Stats().Idle)
require.Equal(t, 1, db.Stats().InUse)
panic(panicErr)
},
)
})
})
t.Run("DoTx", func(t *testing.T) {
m := &mockConnector{
t: t,
}
db := sql.OpenDB(m)
defer func() {
require.NoError(t, db.Close())
}()
require.Panics(t, func() {
require.Equal(t, 0, db.Stats().OpenConnections)
require.Equal(t, 0, db.Stats().Idle)
require.Equal(t, 0, db.Stats().InUse)
defer func() {
require.Equal(t, 1, db.Stats().OpenConnections)
require.Equal(t, 1, db.Stats().Idle)
require.Equal(t, 0, db.Stats().InUse)
}()
_ = DoTx(context.Background(), db,
func(ctx context.Context, tx *sql.Tx) error {
require.Equal(t, 1, db.Stats().OpenConnections)
require.Equal(t, 0, db.Stats().Idle)
require.Equal(t, 1, db.Stats().InUse)
panic(panicErr)
},
)
})
})
}
Loading