File tree Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -1392,6 +1392,7 @@ def __init__(
1392
1392
self .health_check_response = ["pong" , self .HEALTH_CHECK_MESSAGE ]
1393
1393
else :
1394
1394
self .health_check_response = [b"pong" , self .health_check_response_b ]
1395
+ self ._connection_lock = threading .Lock ()
1395
1396
self .reset ()
1396
1397
1397
1398
def __enter__ (self ):
@@ -1465,12 +1466,14 @@ def execute_command(self, *args):
1465
1466
# subscribed to one or more channels
1466
1467
1467
1468
if self .connection is None :
1468
- self .connection = self .connection_pool .get_connection (
1469
- "pubsub" , self .shard_hint
1470
- )
1471
- # register a callback that re-subscribes to any channels we
1472
- # were listening to when we were disconnected
1473
- self .connection .register_connect_callback (self .on_connect )
1469
+ with self ._connection_lock :
1470
+ if self .connection is None :
1471
+ self .connection = self .connection_pool .get_connection (
1472
+ "pubsub" , self .shard_hint
1473
+ )
1474
+ # register a callback that re-subscribes to any channels we
1475
+ # were listening to when we were disconnected
1476
+ self .connection .register_connect_callback (self .on_connect )
1474
1477
connection = self .connection
1475
1478
kwargs = {"check_health" : not self .subscribed }
1476
1479
if not self .subscribed :
Original file line number Diff line number Diff line change @@ -1118,3 +1118,22 @@ def get_msg():
1118
1118
1119
1119
# the timeout on the read should not cause disconnect
1120
1120
assert is_connected ()
1121
+
1122
+
1123
+ @pytest .mark .onlynoncluster
1124
+ class TestConnectionLeak :
1125
+ def test_connection_leak (self , r : redis .Redis ):
1126
+ pubsub = r .pubsub ()
1127
+
1128
+ def test ():
1129
+ tid = threading .get_ident ()
1130
+ pubsub .subscribe (f"foo{ tid } " )
1131
+
1132
+ threads = [threading .Thread (target = test ) for _ in range (10 )]
1133
+ for thread in threads :
1134
+ thread .start ()
1135
+
1136
+ for thread in threads :
1137
+ thread .join ()
1138
+
1139
+ assert r .connection_pool ._created_connections == 2
You can’t perform that action at this time.
0 commit comments