Skip to content
Open
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion pgxpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Pool struct {
healthCheckTimer *time.Timer

healthCheckChan chan struct{}
baseCtx context.Context

acquireTracer AcquireTracer
releaseTracer ReleaseTracer
Expand Down Expand Up @@ -250,6 +251,7 @@ func NewWithConfig(ctx context.Context, config *Config) (*Pool, error) {
healthCheckPeriod: config.HealthCheckPeriod,
healthCheckChan: make(chan struct{}, 1),
closeChan: make(chan struct{}),
baseCtx: ctx,

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.

Storing a context for reuse doesn't feel right. One concern is if it could be cancelled or carries a deadline, as all future refills would break. Removing those prior to storing would be safer, IMO:

Suggested change
baseCtx: ctx,
baseCtx: context.WithoutCancel(ctx),

https://pkg.go.dev/context#WithoutCancel

That said, I wonder if the underlying issue is an incorrect use of context, ie., request-scoped vs. application-scoped. BeforeConnect seems like the right place for per-connection context enrichment, since it runs on every connection creation, whether for warmup or refill of the pool.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, updated to context.WithoutCancel(ctx). That prevents a request-scoped context from breaking future refills while still preserving the values that BeforeConnect callbacks depend on (e.g., AWS IAM auth tokens).

On the context scope point: agreed, BeforeConnect is the right place for per-connection enrichment. The tricky part is that background healthchecks pass context.Background(), which drops all values from the original context. So users who store auth credentials in the context passed to NewWithConfig see it work on first connect but silently break on pool refill. WithoutCancel preserves the values without carrying over cancellation or deadlines.

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.

So users who store auth credentials in the context passed to NewWithConfig see it work on first connect but silently break on pool refill.

Credentials passed through the context seems like anti-pattern. I'd argue it points to the wrong injection point entirely. If credentials are fetched in BeforeConnect, they're available on every connection attempt regardless of what triggered it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fair point. Fetching credentials directly in BeforeConnect is cleaner and avoids the context-propagation question entirely. That said, the current behavior (silently dropping to context.Background() on refill) is surprising regardless of how credentials are injected. Happy to adjust the approach if jackc has a preference on the design direction.

}

if t, ok := config.ConnConfig.Tracer.(AcquireTracer); ok {
Expand Down Expand Up @@ -561,7 +563,7 @@ func (p *Pool) checkMinConns() error {
stat := p.Stat()
toCreate := max(p.minConns-stat.TotalConns(), p.minIdleConns-stat.IdleConns())
if toCreate > 0 {
return p.createIdleResources(context.Background(), int(toCreate))
return p.createIdleResources(p.baseCtx, int(toCreate))
}
return nil
}
Expand Down