1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-03 18:31:14 +03:00
Files
go-redis/redis.go
cyningsun 665d0bfbb7 fix(pool): correct turn management in putIdleConn to prevent connection leaks (#3626)
* fix(pool): prevent double freeTurn in queuedNewConn

This commit fixes a critical race condition where freeTurn() could be
called twice in the connection pool's queuedNewConn flow, causing turn
counter inconsistency.

Problem:
- When a new connection creation failed in queuedNewConn, both the
  defer handler and the dialing goroutine could call freeTurn()
- This led to turn counter underflow and queue length inconsistency

Solution:
- Modified putIdleConn to return a boolean indicating whether the
  caller needs to call freeTurn()
  - Returns true: connection was put back to pool, caller must free turn
  - Returns false: connection was delivered to a waiting request,
    turn will be freed by the receiving goroutine
- Updated queuedNewConn to only call freeTurn() when putIdleConn
  returns true
- Improved error handling flow in the dialing goroutine

Changes:
- putIdleConn now returns bool instead of void
- Added comprehensive documentation for putIdleConn behavior
- Refactored error handling in queuedNewConn goroutine
- Updated test cases to reflect correct turn state expectations

This ensures each turn is freed exactly once, preventing resource
leaks and maintaining correct queue state.

* fix: sync double freeturn bug fix and context calculation from upstream

Synced from https://github.com/redis/go-redis/tree/ndyakov/freeturn-fix

Changes include:
- Add comprehensive tests for double freeTurn bug detection
- Improve context timeout calculation using min(remaining time, DialTimeout)
- Prevent goroutines from waiting longer than necessary

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Nedyalko Dyakov <nedyalko.dyakov@gmail.com>
Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-01 14:05:01 +02:00

46 KiB