Commit 818cea0
committed
fix: eliminate data race in backgroundPing using mutex synchronization
Fixes a critical data race on the p.pingTimer field that occurs when
_background() goroutines are launched dynamically from ping callbacks.
Root Cause:
The pingTimer field is accessed concurrently without synchronization:
- Write: backgroundPing() initializes p.pingTimer
- Read: _background() accesses p.pingTimer during cleanup
- Write: Timer callbacks call p.pingTimer.Reset() to reschedule
The race occurs because _background() goroutines can be created dynamically
from inside the ping timer callback. When the callback invokes p.Do() to send
a PING command, Do() may call p.background() which launches a new _background()
goroutine that races with concurrent timer accesses.
Solution:
- Added pingTimerMu sync.Mutex to protect pingTimer access
- Mutex is held during timer initialization in backgroundPing()
- Mutex is held during each timer callback execution
- Reordered p.backgroundPing() before p.background() in _newPipe()
The mutex ensures:
1. Timer is fully initialized before any concurrent access
2. Timer callbacks execute sequentially (no concurrent callbacks)
3. Reset() calls are properly synchronized
4. No nil timer checks needed - guaranteed non-nil after init
No deadlock occurs because the mutex locks are sequential in time:
- Lock #1: Acquired during backgroundPing() initialization, released when
backgroundPing() returns (before callback can fire)
- Lock #2: Acquired when timer fires (after p.pinggap delay), released when
callback completes
- These locks are separated by the timer delay, so they never nest
Testing:
- Added 3 comprehensive regression tests to detect this race
- All tests pass with -race detector enabled
- Verified with full Docker test suite (Redis Cluster, Sentinel, etc.)
- 99.5% code coverage maintained
- Zero regressions, fully backward compatible1 parent 44083bc commit 818cea0
2 files changed
+112
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
336 | 337 | | |
337 | 338 | | |
338 | 339 | | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | 340 | | |
343 | 341 | | |
344 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
345 | 346 | | |
346 | 347 | | |
347 | 348 | | |
| |||
653 | 654 | | |
654 | 655 | | |
655 | 656 | | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
656 | 660 | | |
657 | 661 | | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
658 | 665 | | |
659 | 666 | | |
660 | 667 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
0 commit comments