Skip to content

Commit 9d2ba82

Browse files
authored
fix(bigtable): fix a nil pointer deference in remove connections when… (#13535)
… one conn is being drained
1 parent fe4ae91 commit 9d2ba82

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

bigtable/internal/transport/connpool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,11 @@ func (p *BigtableChannelPool) removeConnections(decreaseDelta, minConns, maxRemo
830830
return false
831831
}
832832

833-
entries := make([]entryWithAge, numSnapshot)
834-
for i, entry := range snapshotConns {
833+
entries := make([]entryWithAge, 0, numSnapshot)
834+
for _, entry := range snapshotConns {
835835
// Only consider connections not *already* draining for removal via this logic.
836836
if !entry.isDraining() {
837-
entries[i] = entryWithAge{entry: entry, createdAt: entry.conn.creationTime()}
837+
entries = append(entries, entryWithAge{entry: entry, createdAt: entry.conn.creationTime()})
838838
}
839839
}
840840

bigtable/internal/transport/connpool_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,29 @@ func TestRemoveConnections(t *testing.T) {
14171417
}
14181418
}
14191419
})
1420+
1421+
t.Run("MixedDrainingState", func(t *testing.T) {
1422+
poolSize := 5
1423+
pool, err := NewBigtableChannelPool(ctx, poolSize, btopt.RoundRobin, dialFunc, poolOpts()...)
1424+
if err != nil {
1425+
t.Fatalf("Failed to create pool: %v", err)
1426+
}
1427+
defer pool.Close()
1428+
1429+
conns := pool.getConns()
1430+
conns[2].markAsDraining()
1431+
1432+
changed := pool.removeConnections(1, 1, 5)
1433+
1434+
if !changed {
1435+
t.Errorf("Expected pool change, got false")
1436+
}
1437+
finalConns := pool.getConns()
1438+
1439+
if len(finalConns) != 3 {
1440+
t.Errorf("Expected final size 3, got %d", len(finalConns))
1441+
}
1442+
})
14201443
}
14211444

14221445
func TestConnPoolStatisticsVisitor(t *testing.T) {

0 commit comments

Comments
 (0)