|
| 1 | +package pool |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// TestDoubleFreeTurnBug demonstrates the double freeTurn bug where: |
| 13 | +// 1. Dial goroutine creates a connection |
| 14 | +// 2. Original waiter times out |
| 15 | +// 3. putIdleConn delivers connection to another waiter |
| 16 | +// 4. Dial goroutine calls freeTurn() (FIRST FREE) |
| 17 | +// 5. Second waiter uses connection and calls Put() |
| 18 | +// 6. Put() calls freeTurn() (SECOND FREE - BUG!) |
| 19 | +// |
| 20 | +// This causes the semaphore to be released twice, allowing more concurrent |
| 21 | +// operations than PoolSize allows. |
| 22 | +func TestDoubleFreeTurnBug(t *testing.T) { |
| 23 | + var dialCount atomic.Int32 |
| 24 | + var putCount atomic.Int32 |
| 25 | + |
| 26 | + // Slow dialer - 150ms per dial |
| 27 | + slowDialer := func(ctx context.Context) (net.Conn, error) { |
| 28 | + dialCount.Add(1) |
| 29 | + select { |
| 30 | + case <-time.After(150 * time.Millisecond): |
| 31 | + server, client := net.Pipe() |
| 32 | + go func() { |
| 33 | + defer server.Close() |
| 34 | + buf := make([]byte, 1024) |
| 35 | + for { |
| 36 | + _, err := server.Read(buf) |
| 37 | + if err != nil { |
| 38 | + return |
| 39 | + } |
| 40 | + } |
| 41 | + }() |
| 42 | + return client, nil |
| 43 | + case <-ctx.Done(): |
| 44 | + return nil, ctx.Err() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + opt := &Options{ |
| 49 | + Dialer: slowDialer, |
| 50 | + PoolSize: 10, // Small pool to make bug easier to trigger |
| 51 | + MaxConcurrentDials: 10, |
| 52 | + MinIdleConns: 0, |
| 53 | + PoolTimeout: 100 * time.Millisecond, |
| 54 | + DialTimeout: 5 * time.Second, |
| 55 | + } |
| 56 | + |
| 57 | + connPool := NewConnPool(opt) |
| 58 | + defer connPool.Close() |
| 59 | + |
| 60 | + // Scenario: |
| 61 | + // 1. Request A starts dial (100ms timeout - will timeout before dial completes) |
| 62 | + // 2. Request B arrives (500ms timeout - will wait in queue) |
| 63 | + // 3. Request A times out at 100ms |
| 64 | + // 4. Dial completes at 150ms |
| 65 | + // 5. putIdleConn delivers connection to Request B |
| 66 | + // 6. Dial goroutine calls freeTurn() - FIRST FREE |
| 67 | + // 7. Request B uses connection and calls Put() |
| 68 | + // 8. Put() calls freeTurn() - SECOND FREE (BUG!) |
| 69 | + |
| 70 | + var wg sync.WaitGroup |
| 71 | + |
| 72 | + // Request A: Short timeout, will timeout before dial completes |
| 73 | + wg.Add(1) |
| 74 | + go func() { |
| 75 | + defer wg.Done() |
| 76 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 77 | + defer cancel() |
| 78 | + |
| 79 | + cn, err := connPool.Get(ctx) |
| 80 | + if err != nil { |
| 81 | + // Expected to timeout |
| 82 | + t.Logf("Request A timed out as expected: %v", err) |
| 83 | + } else { |
| 84 | + // Should not happen |
| 85 | + t.Errorf("Request A should have timed out but got connection") |
| 86 | + connPool.Put(ctx, cn) |
| 87 | + putCount.Add(1) |
| 88 | + } |
| 89 | + }() |
| 90 | + |
| 91 | + // Wait a bit for Request A to start dialing |
| 92 | + time.Sleep(10 * time.Millisecond) |
| 93 | + |
| 94 | + // Request B: Long timeout, will receive the connection from putIdleConn |
| 95 | + wg.Add(1) |
| 96 | + go func() { |
| 97 | + defer wg.Done() |
| 98 | + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + cn, err := connPool.Get(ctx) |
| 102 | + if err != nil { |
| 103 | + t.Errorf("Request B should have succeeded but got error: %v", err) |
| 104 | + } else { |
| 105 | + t.Logf("Request B got connection successfully") |
| 106 | + // Use the connection briefly |
| 107 | + time.Sleep(50 * time.Millisecond) |
| 108 | + connPool.Put(ctx, cn) |
| 109 | + putCount.Add(1) |
| 110 | + } |
| 111 | + }() |
| 112 | + |
| 113 | + wg.Wait() |
| 114 | + |
| 115 | + // Check results |
| 116 | + t.Logf("\n=== Results ===") |
| 117 | + t.Logf("Dials: %d", dialCount.Load()) |
| 118 | + t.Logf("Puts: %d", putCount.Load()) |
| 119 | + |
| 120 | + // The bug is hard to detect directly without instrumenting freeTurn, |
| 121 | + // but we can verify the scenario works correctly: |
| 122 | + // - Request A should timeout |
| 123 | + // - Request B should succeed and get the connection |
| 124 | + // - 1-2 dials may occur (Request A starts one, Request B may start another) |
| 125 | + // - 1 put should occur (Request B returning the connection) |
| 126 | + |
| 127 | + if putCount.Load() != 1 { |
| 128 | + t.Errorf("Expected 1 put, got %d", putCount.Load()) |
| 129 | + } |
| 130 | + |
| 131 | + t.Logf("✓ Scenario completed successfully") |
| 132 | + t.Logf("Note: The double freeTurn bug would cause semaphore to be released twice,") |
| 133 | + t.Logf("allowing more concurrent operations than PoolSize permits.") |
| 134 | + t.Logf("With the fix, putIdleConn returns true when delivering to a waiter,") |
| 135 | + t.Logf("preventing the dial goroutine from calling freeTurn (waiter will call it later).") |
| 136 | +} |
| 137 | + |
| 138 | +// TestDoubleFreeTurnHighConcurrency tests the bug under high concurrency |
| 139 | +func TestDoubleFreeTurnHighConcurrency(t *testing.T) { |
| 140 | + var dialCount atomic.Int32 |
| 141 | + var getSuccesses atomic.Int32 |
| 142 | + var getFailures atomic.Int32 |
| 143 | + |
| 144 | + slowDialer := func(ctx context.Context) (net.Conn, error) { |
| 145 | + dialCount.Add(1) |
| 146 | + select { |
| 147 | + case <-time.After(200 * time.Millisecond): |
| 148 | + server, client := net.Pipe() |
| 149 | + go func() { |
| 150 | + defer server.Close() |
| 151 | + buf := make([]byte, 1024) |
| 152 | + for { |
| 153 | + _, err := server.Read(buf) |
| 154 | + if err != nil { |
| 155 | + return |
| 156 | + } |
| 157 | + } |
| 158 | + }() |
| 159 | + return client, nil |
| 160 | + case <-ctx.Done(): |
| 161 | + return nil, ctx.Err() |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + opt := &Options{ |
| 166 | + Dialer: slowDialer, |
| 167 | + PoolSize: 20, |
| 168 | + MaxConcurrentDials: 20, |
| 169 | + MinIdleConns: 0, |
| 170 | + PoolTimeout: 100 * time.Millisecond, |
| 171 | + DialTimeout: 5 * time.Second, |
| 172 | + } |
| 173 | + |
| 174 | + connPool := NewConnPool(opt) |
| 175 | + defer connPool.Close() |
| 176 | + |
| 177 | + // Create many requests with varying timeouts |
| 178 | + // Some will timeout before dial completes, triggering the putIdleConn delivery path |
| 179 | + const numRequests = 100 |
| 180 | + var wg sync.WaitGroup |
| 181 | + |
| 182 | + for i := 0; i < numRequests; i++ { |
| 183 | + wg.Add(1) |
| 184 | + go func(id int) { |
| 185 | + defer wg.Done() |
| 186 | + |
| 187 | + // Vary timeout: some short (will timeout), some long (will succeed) |
| 188 | + timeout := 100 * time.Millisecond |
| 189 | + if id%3 == 0 { |
| 190 | + timeout = 500 * time.Millisecond |
| 191 | + } |
| 192 | + |
| 193 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 194 | + defer cancel() |
| 195 | + |
| 196 | + cn, err := connPool.Get(ctx) |
| 197 | + if err != nil { |
| 198 | + getFailures.Add(1) |
| 199 | + } else { |
| 200 | + getSuccesses.Add(1) |
| 201 | + time.Sleep(10 * time.Millisecond) |
| 202 | + connPool.Put(ctx, cn) |
| 203 | + } |
| 204 | + }(i) |
| 205 | + |
| 206 | + // Stagger requests |
| 207 | + if i%10 == 0 { |
| 208 | + time.Sleep(5 * time.Millisecond) |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + wg.Wait() |
| 213 | + |
| 214 | + t.Logf("\n=== High Concurrency Results ===") |
| 215 | + t.Logf("Requests: %d", numRequests) |
| 216 | + t.Logf("Successes: %d", getSuccesses.Load()) |
| 217 | + t.Logf("Failures: %d", getFailures.Load()) |
| 218 | + t.Logf("Dials: %d", dialCount.Load()) |
| 219 | + |
| 220 | + // Verify that some requests succeeded despite timeouts |
| 221 | + // This exercises the putIdleConn delivery path |
| 222 | + if getSuccesses.Load() == 0 { |
| 223 | + t.Errorf("Expected some successful requests, got 0") |
| 224 | + } |
| 225 | + |
| 226 | + t.Logf("✓ High concurrency test completed") |
| 227 | + t.Logf("Note: This test exercises the putIdleConn delivery path where the bug occurs") |
| 228 | +} |
| 229 | + |
0 commit comments