Skip to content

Commit a7c1090

Browse files
authored
PYTHON-4414 interruptInUseConnections should cancel pending connections too (mongodb#2010)
1 parent a3bdc13 commit a7c1090

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

pymongo/asynchronous/pool.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,9 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
12491249
async with self.lock:
12501250
conn_id = self.next_connection_id
12511251
self.next_connection_id += 1
1252+
# Use a temporary context so that interrupt_connections can cancel creating the socket.
1253+
tmp_context = _CancellationContext()
1254+
self.active_contexts.add(tmp_context)
12521255

12531256
listeners = self.opts._event_listeners
12541257
if self.enabled_for_cmap:
@@ -1267,6 +1270,8 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
12671270
try:
12681271
sock = await _configured_socket(self.address, self.opts)
12691272
except BaseException as error:
1273+
async with self.lock:
1274+
self.active_contexts.discard(tmp_context)
12701275
if self.enabled_for_cmap:
12711276
assert listeners is not None
12721277
listeners.publish_connection_closed(
@@ -1292,6 +1297,9 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
12921297
conn = AsyncConnection(sock, self, self.address, conn_id) # type: ignore[arg-type]
12931298
async with self.lock:
12941299
self.active_contexts.add(conn.cancel_context)
1300+
self.active_contexts.discard(tmp_context)
1301+
if tmp_context.cancelled:
1302+
conn.cancel_context.cancel()
12951303
try:
12961304
if self.handshake:
12971305
await conn.hello()
@@ -1301,6 +1309,8 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13011309

13021310
await conn.authenticate()
13031311
except BaseException:
1312+
async with self.lock:
1313+
self.active_contexts.discard(conn.cancel_context)
13041314
conn.close_conn(ConnectionClosedReason.ERROR)
13051315
raise
13061316

pymongo/synchronous/pool.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,9 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12431243
with self.lock:
12441244
conn_id = self.next_connection_id
12451245
self.next_connection_id += 1
1246+
# Use a temporary context so that interrupt_connections can cancel creating the socket.
1247+
tmp_context = _CancellationContext()
1248+
self.active_contexts.add(tmp_context)
12461249

12471250
listeners = self.opts._event_listeners
12481251
if self.enabled_for_cmap:
@@ -1261,6 +1264,8 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12611264
try:
12621265
sock = _configured_socket(self.address, self.opts)
12631266
except BaseException as error:
1267+
with self.lock:
1268+
self.active_contexts.discard(tmp_context)
12641269
if self.enabled_for_cmap:
12651270
assert listeners is not None
12661271
listeners.publish_connection_closed(
@@ -1286,6 +1291,9 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12861291
conn = Connection(sock, self, self.address, conn_id) # type: ignore[arg-type]
12871292
with self.lock:
12881293
self.active_contexts.add(conn.cancel_context)
1294+
self.active_contexts.discard(tmp_context)
1295+
if tmp_context.cancelled:
1296+
conn.cancel_context.cancel()
12891297
try:
12901298
if self.handshake:
12911299
conn.hello()
@@ -1295,6 +1303,8 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12951303

12961304
conn.authenticate()
12971305
except BaseException:
1306+
with self.lock:
1307+
self.active_contexts.discard(conn.cancel_context)
12981308
conn.close_conn(ConnectionClosedReason.ERROR)
12991309
raise
13001310

test/test_connection_monitoring.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ def set_fail_point(self, command_args):
216216

217217
def run_scenario(self, scenario_def, test):
218218
"""Run a CMAP spec test."""
219-
if (
220-
scenario_def["description"]
221-
== "clear with interruptInUseConnections = true closes pending connections"
222-
):
223-
self.skipTest("Skip pending PYTHON-4414")
224219
self.logs: list = []
225220
self.assertEqual(scenario_def["version"], 1)
226221
self.assertIn(scenario_def["style"], ["unit", "integration"])

0 commit comments

Comments
 (0)