From 5fd6ab564c79fce7c5694f78dcec5ec2f9796608 Mon Sep 17 00:00:00 2001 From: Dayna Blackwell Date: Thu, 30 Apr 2026 16:29:22 -0700 Subject: [PATCH 1/2] fix(pgxpool): pass base context to BeforeConnect from background healthcheck checkMinConns creates new connections with context.Background(), which means BeforeConnect hooks receive a bare context with no values. This breaks any hook that depends on context values from the original NewWithConfig call (loggers, AWS IAM credential caches, tracing spans). Store the context passed to NewWithConfig on the Pool struct and use it in checkMinConns instead of context.Background(). This matches the behavior of the initial idle resource creation at startup (line 337), which already uses the constructor context. Fixes #2545 Signed-off-by: Dayna Blackwell --- pgxpool/pool.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pgxpool/pool.go b/pgxpool/pool.go index dac8058c3..3f4c2c52f 100644 --- a/pgxpool/pool.go +++ b/pgxpool/pool.go @@ -103,6 +103,7 @@ type Pool struct { healthCheckTimer *time.Timer healthCheckChan chan struct{} + baseCtx context.Context acquireTracer AcquireTracer releaseTracer ReleaseTracer @@ -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, } if t, ok := config.ConnConfig.Tracer.(AcquireTracer); ok { @@ -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 } From 9262a9af94d05ed09a1d395a07532dc8c25ac96a Mon Sep 17 00:00:00 2001 From: Dayna Blackwell Date: Sat, 2 May 2026 13:21:57 -0700 Subject: [PATCH 2/2] fix: use context.WithoutCancel to prevent stale cancellation on pool refill --- pgxpool/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgxpool/pool.go b/pgxpool/pool.go index 3f4c2c52f..cc39b3891 100644 --- a/pgxpool/pool.go +++ b/pgxpool/pool.go @@ -251,7 +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, + baseCtx: context.WithoutCancel(ctx), } if t, ok := config.ConnConfig.Tracer.(AcquireTracer); ok {