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 @@ -1402,6 +1402,7 @@ def __init__(
1402
1402
self .health_check_response = ["pong" , self .HEALTH_CHECK_MESSAGE ]
1403
1403
else :
1404
1404
self .health_check_response = [b"pong" , self .health_check_response_b ]
1405
+ self ._connection_lock = threading .Lock ()
1405
1406
self .reset ()
1406
1407
1407
1408
def __enter__ (self ):
@@ -1466,12 +1467,14 @@ def execute_command(self, *args):
1466
1467
# subscribed to one or more channels
1467
1468
1468
1469
if self .connection is None :
1469
- self .connection = self .connection_pool .get_connection (
1470
- "pubsub" , self .shard_hint
1471
- )
1472
- # register a callback that re-subscribes to any channels we
1473
- # were listening to when we were disconnected
1474
- self .connection .register_connect_callback (self .on_connect )
1470
+ with self ._connection_lock :
1471
+ if self .connection is None :
1472
+ self .connection = self .connection_pool .get_connection (
1473
+ "pubsub" , self .shard_hint
1474
+ )
1475
+ # register a callback that re-subscribes to any channels we
1476
+ # were listening to when we were disconnected
1477
+ self .connection .register_connect_callback (self .on_connect )
1475
1478
connection = self .connection
1476
1479
kwargs = {"check_health" : not self .subscribed }
1477
1480
if not self .subscribed :
Original file line number Diff line number Diff line change @@ -777,3 +777,22 @@ def get_msg():
777
777
778
778
# the timeout on the read should not cause disconnect
779
779
assert is_connected ()
780
+
781
+
782
+ @pytest .mark .onlynoncluster
783
+ class TestConnectionLeak :
784
+ def test_connection_leak (self , r : redis .Redis ):
785
+ pubsub = r .pubsub ()
786
+
787
+ def test ():
788
+ tid = threading .get_ident ()
789
+ pubsub .subscribe (f"foo{ tid } " )
790
+
791
+ threads = [threading .Thread (target = test ) for _ in range (10 )]
792
+ for thread in threads :
793
+ thread .start ()
794
+
795
+ for thread in threads :
796
+ thread .join ()
797
+
798
+ assert r .connection_pool ._created_connections == 2
You can’t perform that action at this time.
0 commit comments