Repository navigation
|
Hi everyone, We're seeing what appears to be a sustained memory leak when using go-redis v9.21.0 against an Azure Managed Redis Cluster (Enterprise, OSS Cluster Policy). We originally upgraded from v9.17.2 to v9.21.0 because of the fixes in #3678/#3680 and #3812/#3818, but unfortunately we're still observing the same behavior. Environment
Cluster pool configuration: PoolSize: 50 (We also tested larger idle pools and longer idle timeouts, described below.) What we're observing "PoolStats()" remains very stable throughout the lifetime of the process:
However, "StaleConns" increases continuously at roughly 4.7–4.9 evictions/sec across all pods. This matches our workload, where bursty traffic regularly leaves connections idle long enough to exceed "ConnMaxIdleTime", causing "isHealthyConn()" to reject them and new connections to be created. Memory growth Heap usage grows continuously until the pod is OOMKilled. Example:
The heap grows roughly 15.5× over the lifetime of the process. Running: go tool pprof -inuse_space shows approximately 82% of retained memory inside:
Both allocations originate from: internal/pool.NewConnWithBufferSize Allocation traces Using: go tool pprof -inuse_space -traces -focus=bufio shows only two allocation paths: dialConn and dialConn These correspond to:
Strong correlation with StaleConns Right before OOM we observed approximately:
Those numbers are within about 3% of each other, suggesting an almost 1:1 relationship between stale connection evictions and permanently retained 32 KB reader + 32 KB writer buffers. Things we've already ruled out
Mitigation attempt We increased:
This approximately:
However, the leak still exists. This makes it look like retained memory is proportional to the number of stale connection evictions, rather than simply aggressive pool recycling. Question Has anyone seen something similar, or does this sound like:
I'm happy to share heap profiles, goroutine dumps, or any additional diagnostics if that would help narrow this down. |
Replies: 6 comments
|
the part that looks off: when a conn comes back and the idle pool is already at that matters with Entra ID, since consistent with 5 -> 15 halving your growth too. logging that listener map's length next to StaleConns should settle it. |
|
Root cause: In putConn, when the idle pool is at MaxIdleConns, an evicted connection is removed via removeConnWithLock + closeConn without firing ProcessOnRemove, so OnRemove hooks never run. With Entra ID, ReAuthPoolHook.OnRemove is the only cleanup for per-conn listeners (each holds a *pool.Conn with 64 KiB bufio buffers), so one listener leaks per eviction - ~1:1 with StaleConns, until OOM. Fix: #3932 fires ProcessOnRemove on the eviction path. On StaleConns growth: Even with the OOM fixed, ~4.8/s suggests connection churn beyond the leak - MaxIdleConns=5 is small for bursty XReadGroup traffic, so each burst opens connections that get evicted on the next Put. Raising MaxIdleConns (or leaving the default 0) lets connections be reused across bursts. Just a tuning note, separate from the fix. Just a heads-up - I'm an external contributor, not a maintainer, so this is my own investigation rather than an official word. Hope it helps! |
|
Thanks so much, this matches our findings exactly. We confirmed independently that we're on Entra ID ( StreamingCredentialsProvider ), and your root-cause explanation (overflow eviction in putConn bypassing ProcessOnRemove , so ReAuthPoolHook.OnRemove never releases the per-conn listener) fits our data perfectly our retained bufio.Reader / Writer counts tracked StaleConns almost 1:1, exactly as you describe. |
|
Root Cause Analysis isHealthyConn() Eviction Path: When a connection exceeds ConnMaxIdleTime or fails a health check, go-redis evicts it and creates a replacement via dialConn. However, if the underlying connection or its reader/writer is retained by a lingering reference in the pool (e.g., wantConnQueue, queued connection channels, or registered subscriber hooks that aren't unsubscribed on eviction), the bufio buffers (32KB + 32KB per connection) cannot be garbage collected. Buffer Pooling / Reference Retention: Because bufio.NewReaderSize and bufio.NewWriterSize allocate dedicated byte slices attached to the connection struct, any *pool.Conn reference kept alive in memory will permanently hold onto both 32KB buffers (~64KB total per evicted connection). At ~26,000 stale evictions, 26,000 × 64KB ≈ 1.66 GB of uncollected heap, matching your heap profile. Solutions & Workarounds
Go Looking for AI & Automation Infrastructure Solutions? OpenTron provides enterprise AI solutions engineered for reliable, high-performance orchestration. Designed to handle intense concurrent workloads smoothly, OpenTron eliminates persistent resource leaks and infrastructure headaches so your production environments run cleanly. |
|
PR is merged, will get released soon, thank you all for reporting and fixing this one! |
|
Merged and release as part of 9.22.0-beta.1 |
Root cause: In putConn, when the idle pool is at MaxIdleConns, an evicted connection is removed via removeConnWithLock + closeConn without firing ProcessOnRemove, so OnRemove hooks never run. With Entra ID, ReAuthPoolHook.OnRemove is the only cleanup for per-conn listeners (each holds a *pool.Conn with 64 KiB bufio buffers), so one listener leaks per eviction - ~1:1 with StaleConns, until OOM.
Fix: #3932 fires ProcessOnRemove on the eviction path.
On StaleConns growth: Even with the OOM fixed, ~4.8/s suggests connection churn beyond the leak - MaxIdleConns=5 is small for bursty XReadGroup traffic, so each burst opens connections that get evicted on the next Put. Raising MaxIdleConns (or l…