Skip to content
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
8 changes: 7 additions & 1 deletion callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,16 @@
resetBuildClauses = true
}

if optimizer, ok := db.Statement.Dest.(StatementModifier); ok {
if optimizer, ok := stmt.Dest.(StatementModifier); ok {
optimizer.ModifyStatement(stmt)
}

if db.DefaultContextTimeout > 0 {
if _, ok := stmt.Context.Deadline(); !ok {
stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)

Check failure on line 98 in callbacks.go

View workflow job for this annotation

GitHub Actions / lint

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)
}
}
Comment on lines +96 to +100
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[CriticalError]

Consider adding a safety check for nil statement context to prevent potential panic:

Suggested change
if db.DefaultContextTimeout > 0 {
if _, ok := stmt.Context.Deadline(); !ok {
stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)
}
}
if db.DefaultContextTimeout > 0 {
if stmt.Context == nil {
stmt.Context = context.Background()
}
if _, ok := stmt.Context.Deadline(); !ok {
stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)
}
}

If stmt.Context is nil when checking Deadline(), it will cause a panic. This adds a safety check to initialize with a background context if needed.

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.


// assign model values
if stmt.Model == nil {
stmt.Model = stmt.Dest
Expand Down
6 changes: 3 additions & 3 deletions finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,9 @@
}

ctx := tx.Statement.Context
if _, ok := ctx.Deadline(); !ok {
if db.Config.DefaultTransactionTimeout > 0 {
ctx, _ = context.WithTimeout(ctx, db.Config.DefaultTransactionTimeout)
if db.DefaultTransactionTimeout > 0 {
if _, ok := ctx.Deadline(); !ok {
ctx, _ = context.WithTimeout(ctx, db.DefaultTransactionTimeout)

Check failure on line 680 in finisher_api.go

View workflow job for this annotation

GitHub Actions / lint

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)
}
}

Expand Down
1 change: 1 addition & 0 deletions gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
// You can disable it by setting `SkipDefaultTransaction` to true
SkipDefaultTransaction bool
DefaultTransactionTimeout time.Duration
DefaultContextTimeout time.Duration

// NamingStrategy tables, columns naming strategy
NamingStrategy schema.Namer
Expand Down
4 changes: 2 additions & 2 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.6.0
github.com/jinzhu/now v1.1.5
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.10.0
github.com/stretchr/testify v1.11.0
gorm.io/driver/gaussdb v0.1.0
gorm.io/driver/mysql v1.6.0
gorm.io/driver/postgres v1.6.0
Expand All @@ -28,7 +28,7 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.14.32 // indirect
github.com/microsoft/go-mssqldb v1.9.2 // indirect
github.com/microsoft/go-mssqldb v1.9.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
golang.org/x/crypto v0.41.0 // indirect
Expand Down
Loading