Skip to content

Add DefaultContextTimeout option#7567

Merged
jinzhu merged 1 commit intomasterfrom
add_context_timeout
Aug 26, 2025
Merged

Add DefaultContextTimeout option#7567
jinzhu merged 1 commit intomasterfrom
add_context_timeout

Conversation

@jinzhu
Copy link
Copy Markdown
Member

@jinzhu jinzhu commented Aug 26, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

User Case Description

@propel-code-bot
Copy link
Copy Markdown
Contributor

Introduce DefaultContextTimeout Configuration for DB Contexts

This pull request adds a new DefaultContextTimeout field to the GORM Config struct. The change enables users to configure a default timeout for DB contexts, applying a context.WithTimeout to DB operations when no deadline is set and DefaultContextTimeout is greater than zero. Several modifications were made to ensure this timeout is respected in the main query execution and transaction initiation paths. Additionally, test dependencies in go.mod were updated.

Key Changes

• Added DefaultContextTimeout (time.Duration) to gorm.Config to allow setting a global default context timeout.
• Modified callbacks.go to wrap statement contexts with context.WithTimeout using DefaultContextTimeout when appropriate.
• Adjusted transaction context initialization in finisher_api.go to apply DefaultTransactionTimeout only if set and not already present.
• Changed references from db.Config.DefaultTransactionTimeout to db.DefaultTransactionTimeout for internal consistency.
• Updated test dependencies: github.com/stretchr/testify upgraded from v1.10.0 to v1.11.0; github.com/microsoft/go-mssqldb upgraded from v1.9.2 to v1.9.3.

Affected Areas

• gorm.go (Config struct, initialization logic)
• callbacks.go (processor logic for context timeout)
finisher_api.go (transaction context timeout and handling)
• tests/go.mod (test dependencies)

This summary was automatically generated by @propel-code-bot

Comment thread callbacks.go
Comment on lines +96 to +100
if db.DefaultContextTimeout > 0 {
if _, ok := stmt.Context.Deadline(); !ok {
stmt.Context, _ = context.WithTimeout(stmt.Context, db.DefaultContextTimeout)
}
}
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.

@jinzhu jinzhu merged commit 7ceb0d9 into master Aug 26, 2025
49 of 50 checks passed
@jinzhu jinzhu deleted the add_context_timeout branch August 26, 2025 06:24
phroggyy pushed a commit to incident-io/gorm that referenced this pull request Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant