Skip to content

Commit fcaf8f9

Browse files
committed
Add comments to catching BaseException, speed up tests
1 parent cbc1160 commit fcaf8f9

File tree

10 files changed

+40
-24
lines changed

10 files changed

+40
-24
lines changed

pymongo/asynchronous/change_stream.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ async def try_next(self) -> Optional[_DocumentType]:
391391
if not _resumable(exc) and not exc.timeout:
392392
await self.close()
393393
raise
394+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
394395
except BaseException:
395396
await self.close()
396397
raise

pymongo/asynchronous/client_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ async def callback(session, custom_arg, custom_kwarg=None):
697697
)
698698
try:
699699
ret = await callback(self)
700+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
700701
except BaseException as exc:
701702
if self.in_transaction:
702703
await self.abort_transaction()

pymongo/asynchronous/cursor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ async def _send_message(self, operation: Union[_Query, _GetMore]) -> None:
11261126
self._killed = True
11271127
await self.close()
11281128
raise
1129+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
11291130
except BaseException:
11301131
await self.close()
11311132
raise

pymongo/asynchronous/pool.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ async def command(
559559
)
560560
except (OperationFailure, NotPrimaryError):
561561
raise
562-
# Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
562+
# Catch socket.error, KeyboardInterrupt, CancelledError, etc. and close ourselves.
563563
except BaseException as error:
564564
self._raise_connection_failure(error)
565565

@@ -576,6 +576,7 @@ async def send_message(self, message: bytes, max_doc_size: int) -> None:
576576

577577
try:
578578
await async_sendall(self.conn, message)
579+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
579580
except BaseException as error:
580581
self._raise_connection_failure(error)
581582

@@ -586,6 +587,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
586587
"""
587588
try:
588589
return await receive_message(self, request_id, self.max_message_size)
590+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
589591
except BaseException as error:
590592
self._raise_connection_failure(error)
591593

@@ -1269,6 +1271,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
12691271

12701272
try:
12711273
sock = await _configured_socket(self.address, self.opts)
1274+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
12721275
except BaseException as error:
12731276
async with self.lock:
12741277
self.active_contexts.discard(tmp_context)
@@ -1308,6 +1311,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13081311
handler.contribute_socket(conn, completed_handshake=False)
13091312

13101313
await conn.authenticate()
1314+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13111315
except BaseException:
13121316
async with self.lock:
13131317
self.active_contexts.discard(conn.cancel_context)
@@ -1369,6 +1373,7 @@ async def checkout(
13691373
async with self.lock:
13701374
self.active_contexts.add(conn.cancel_context)
13711375
yield conn
1376+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13721377
except BaseException:
13731378
# Exception in caller. Ensure the connection gets returned.
13741379
# Note that when pinned is True, the session owns the
@@ -1515,6 +1520,7 @@ async def _get_conn(
15151520
async with self._max_connecting_cond:
15161521
self._pending -= 1
15171522
self._max_connecting_cond.notify()
1523+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
15181524
except BaseException:
15191525
if conn:
15201526
# We checked out a socket but authentication failed.

pymongo/periodic_executor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ async def _run(self) -> None:
9898
if not await self._target():
9999
self._stopped = True
100100
break
101+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
101102
except BaseException:
102103
self._stopped = True
103104
raise
@@ -230,6 +231,7 @@ def _run(self) -> None:
230231
if not self._target():
231232
self._stopped = True
232233
break
234+
# Catch KeyboardInterrupt, etc. and cleanup.
233235
except BaseException:
234236
with self._lock:
235237
self._stopped = True

pymongo/synchronous/change_stream.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ def try_next(self) -> Optional[_DocumentType]:
389389
if not _resumable(exc) and not exc.timeout:
390390
self.close()
391391
raise
392+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
392393
except BaseException:
393394
self.close()
394395
raise

pymongo/synchronous/client_session.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ def callback(session, custom_arg, custom_kwarg=None):
694694
self.start_transaction(read_concern, write_concern, read_preference, max_commit_time_ms)
695695
try:
696696
ret = callback(self)
697+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
697698
except BaseException as exc:
698699
if self.in_transaction:
699700
self.abort_transaction()

pymongo/synchronous/cursor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ def _send_message(self, operation: Union[_Query, _GetMore]) -> None:
11241124
self._killed = True
11251125
self.close()
11261126
raise
1127+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
11271128
except BaseException:
11281129
self.close()
11291130
raise

pymongo/synchronous/pool.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def command(
559559
)
560560
except (OperationFailure, NotPrimaryError):
561561
raise
562-
# Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
562+
# Catch socket.error, KeyboardInterrupt, CancelledError, etc. and close ourselves.
563563
except BaseException as error:
564564
self._raise_connection_failure(error)
565565

@@ -576,6 +576,7 @@ def send_message(self, message: bytes, max_doc_size: int) -> None:
576576

577577
try:
578578
sendall(self.conn, message)
579+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
579580
except BaseException as error:
580581
self._raise_connection_failure(error)
581582

@@ -586,6 +587,7 @@ def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _OpMsg]:
586587
"""
587588
try:
588589
return receive_message(self, request_id, self.max_message_size)
590+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
589591
except BaseException as error:
590592
self._raise_connection_failure(error)
591593

@@ -1263,6 +1265,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12631265

12641266
try:
12651267
sock = _configured_socket(self.address, self.opts)
1268+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
12661269
except BaseException as error:
12671270
with self.lock:
12681271
self.active_contexts.discard(tmp_context)
@@ -1302,6 +1305,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
13021305
handler.contribute_socket(conn, completed_handshake=False)
13031306

13041307
conn.authenticate()
1308+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13051309
except BaseException:
13061310
with self.lock:
13071311
self.active_contexts.discard(conn.cancel_context)
@@ -1363,6 +1367,7 @@ def checkout(
13631367
with self.lock:
13641368
self.active_contexts.add(conn.cancel_context)
13651369
yield conn
1370+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13661371
except BaseException:
13671372
# Exception in caller. Ensure the connection gets returned.
13681373
# Note that when pinned is True, the session owns the
@@ -1509,6 +1514,7 @@ def _get_conn(
15091514
with self._max_connecting_cond:
15101515
self._pending -= 1
15111516
self._max_connecting_cond.notify()
1517+
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
15121518
except BaseException:
15131519
if conn:
15141520
# We checked out a socket but authentication failed.

test/asynchronous/test_async_cancellation.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626

2727
class TestAsyncCancellation(AsyncIntegrationTest):
2828
async def test_async_cancellation_closes_connection(self):
29-
client = await self.async_rs_or_single_client(maxPoolSize=1)
30-
pool = await async_get_pool(client)
31-
await connected(client)
29+
pool = await async_get_pool(self.client)
30+
await connected(self.client)
3231
conn = one(pool.conns)
33-
await client.db.test.insert_one({"x": 1})
34-
self.addAsyncCleanup(client.db.test.drop)
32+
await self.client.db.test.insert_one({"x": 1})
33+
self.addAsyncCleanup(self.client.db.test.drop)
3534

3635
async def task():
37-
await client.db.test.find_one({"$where": delay(0.2)})
36+
await self.client.db.test.find_one({"$where": delay(0.2)})
3837

3938
task = asyncio.create_task(task())
4039

@@ -48,15 +47,14 @@ async def task():
4847

4948
@async_client_context.require_transactions
5049
async def test_async_cancellation_aborts_transaction(self):
51-
client = await self.async_rs_or_single_client()
52-
await connected(client)
53-
await client.db.test.insert_one({"x": 1})
54-
self.addAsyncCleanup(client.db.test.drop)
50+
await connected(self.client)
51+
await self.client.db.test.insert_one({"x": 1})
52+
self.addAsyncCleanup(self.client.db.test.drop)
5553

56-
session = client.start_session()
54+
session = self.client.start_session()
5755

5856
async def callback(session):
59-
await client.db.test.find_one({"$where": delay(0.2)}, session=session)
57+
await self.client.db.test.find_one({"$where": delay(0.2)}, session=session)
6058

6159
async def task():
6260
await session.with_transaction(callback)
@@ -73,13 +71,12 @@ async def task():
7371

7472
@async_client_context.require_failCommand_blockConnection
7573
async def test_async_cancellation_closes_cursor(self):
76-
client = await self.async_rs_or_single_client()
77-
await connected(client)
74+
await connected(self.client)
7875
for _ in range(2):
79-
await client.db.test.insert_one({"x": 1})
80-
self.addAsyncCleanup(client.db.test.drop)
76+
await self.client.db.test.insert_one({"x": 1})
77+
self.addAsyncCleanup(self.client.db.test.drop)
8178

82-
cursor = client.db.test.find({}, batch_size=1)
79+
cursor = self.client.db.test.find({}, batch_size=1)
8380
await cursor.next()
8481

8582
# Make sure getMore commands block
@@ -106,11 +103,10 @@ async def task():
106103
@async_client_context.require_change_streams
107104
@async_client_context.require_failCommand_blockConnection
108105
async def test_async_cancellation_closes_change_stream(self):
109-
client = await self.async_rs_or_single_client()
110-
await connected(client)
111-
self.addAsyncCleanup(client.db.test.drop)
106+
await connected(self.client)
107+
self.addAsyncCleanup(self.client.db.test.drop)
112108

113-
change_stream = await client.db.test.watch(batch_size=2)
109+
change_stream = await self.client.db.test.watch(batch_size=2)
114110

115111
# Make sure getMore commands block
116112
fail_command = {
@@ -122,7 +118,7 @@ async def test_async_cancellation_closes_change_stream(self):
122118
async def task():
123119
async with self.fail_point(fail_command):
124120
for _ in range(2):
125-
await client.db.test.insert_one({"x": 1})
121+
await self.client.db.test.insert_one({"x": 1})
126122
await change_stream.next()
127123

128124
task = asyncio.create_task(task())

0 commit comments

Comments
 (0)