Skip to content

Commit 428a888

Browse files
committed
PYTHON-4993 - Reevaluate handling of asyncio.CancelledError
1 parent 7a4150a commit 428a888

File tree

9 files changed

+3
-41
lines changed

9 files changed

+3
-41
lines changed

pymongo/_client_bulk_shared.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Constants, types, and classes shared across Client Bulk Write API implementations."""
1717
from __future__ import annotations
1818

19+
import asyncio
1920
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping, NoReturn
2021

2122
from pymongo.errors import ClientBulkWriteException, OperationFailure
@@ -75,5 +76,7 @@ def _throw_client_bulk_write_exception(
7576
)
7677
raise OperationFailure(errmsg, code, full_result)
7778
if isinstance(full_result["error"], BaseException):
79+
if isinstance(full_result["error"], asyncio.CancelledError):
80+
raise
7881
raise ClientBulkWriteException(full_result, verbose_results) from full_result["error"]
7982
raise ClientBulkWriteException(full_result, verbose_results)

pymongo/asynchronous/encryption.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ def _wrap_encryption_errors() -> Iterator[None]:
127127
# BSON encoding/decoding errors are unrelated to encryption so
128128
# we should propagate them unchanged.
129129
raise
130-
except asyncio.CancelledError:
131-
raise
132130
except Exception as exc:
133131
raise EncryptionError(exc) from exc
134132

@@ -766,8 +764,6 @@ async def create_encrypted_collection(
766764
await database.create_collection(name=name, **kwargs),
767765
encrypted_fields,
768766
)
769-
except asyncio.CancelledError:
770-
raise
771767
except Exception as exc:
772768
raise EncryptedCollectionError(exc, encrypted_fields) from exc
773769

pymongo/asynchronous/mongo_client.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"""
3333
from __future__ import annotations
3434

35-
import asyncio
3635
import contextlib
3736
import os
3837
import warnings
@@ -2038,8 +2037,6 @@ async def _process_kill_cursors(self) -> None:
20382037
for address, cursor_id, conn_mgr in pinned_cursors:
20392038
try:
20402039
await self._cleanup_cursor_lock(cursor_id, address, conn_mgr, None, False)
2041-
except asyncio.CancelledError:
2042-
raise
20432040
except Exception as exc:
20442041
if isinstance(exc, InvalidOperation) and self._topology._closed:
20452042
# Raise the exception when client is closed so that it
@@ -2054,8 +2051,6 @@ async def _process_kill_cursors(self) -> None:
20542051
for address, cursor_ids in address_to_cursor_ids.items():
20552052
try:
20562053
await self._kill_cursors(cursor_ids, address, topology, session=None)
2057-
except asyncio.CancelledError:
2058-
raise
20592054
except Exception as exc:
20602055
if isinstance(exc, InvalidOperation) and self._topology._closed:
20612056
raise
@@ -2070,8 +2065,6 @@ async def _process_periodic_tasks(self) -> None:
20702065
try:
20712066
await self._process_kill_cursors()
20722067
await self._topology.update_pool()
2073-
except asyncio.CancelledError:
2074-
raise
20752068
except Exception as exc:
20762069
if isinstance(exc, InvalidOperation) and self._topology._closed:
20772070
return

pymongo/asynchronous/monitor.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
import asyncio
2019
import atexit
2120
import logging
2221
import time
@@ -257,8 +256,6 @@ async def _check_server(self) -> ServerDescription:
257256
details = cast(Mapping[str, Any], exc.details)
258257
await self._topology.receive_cluster_time(details.get("$clusterTime"))
259258
raise
260-
except asyncio.CancelledError:
261-
raise
262259
except ReferenceError:
263260
raise
264261
except Exception as error:
@@ -424,8 +421,6 @@ def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
424421
if len(seedlist) == 0:
425422
# As per the spec: this should be treated as a failure.
426423
raise Exception
427-
except asyncio.CancelledError:
428-
raise
429424
except Exception:
430425
# As per the spec, upon encountering an error:
431426
# - An error must not be raised
@@ -489,8 +484,6 @@ async def _run(self) -> None:
489484
except ReferenceError:
490485
# Topology was garbage-collected.
491486
await self.close()
492-
except asyncio.CancelledError:
493-
raise
494487
except Exception:
495488
await self._pool.reset()
496489

pymongo/asynchronous/pool.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ def _close_conn(self) -> None:
704704
# shutdown.
705705
try:
706706
self.conn.close()
707-
except asyncio.CancelledError:
708-
raise
709707
except Exception: # noqa: S110
710708
pass
711709

pymongo/synchronous/encryption.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Support for explicit client-side field level encryption."""
1616
from __future__ import annotations
1717

18-
import asyncio
1918
import contextlib
2019
import enum
2120
import socket
@@ -127,8 +126,6 @@ def _wrap_encryption_errors() -> Iterator[None]:
127126
# BSON encoding/decoding errors are unrelated to encryption so
128127
# we should propagate them unchanged.
129128
raise
130-
except asyncio.CancelledError:
131-
raise
132129
except Exception as exc:
133130
raise EncryptionError(exc) from exc
134131

@@ -760,8 +757,6 @@ def create_encrypted_collection(
760757
database.create_collection(name=name, **kwargs),
761758
encrypted_fields,
762759
)
763-
except asyncio.CancelledError:
764-
raise
765760
except Exception as exc:
766761
raise EncryptedCollectionError(exc, encrypted_fields) from exc
767762

pymongo/synchronous/mongo_client.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"""
3333
from __future__ import annotations
3434

35-
import asyncio
3635
import contextlib
3736
import os
3837
import warnings
@@ -2032,8 +2031,6 @@ def _process_kill_cursors(self) -> None:
20322031
for address, cursor_id, conn_mgr in pinned_cursors:
20332032
try:
20342033
self._cleanup_cursor_lock(cursor_id, address, conn_mgr, None, False)
2035-
except asyncio.CancelledError:
2036-
raise
20372034
except Exception as exc:
20382035
if isinstance(exc, InvalidOperation) and self._topology._closed:
20392036
# Raise the exception when client is closed so that it
@@ -2048,8 +2045,6 @@ def _process_kill_cursors(self) -> None:
20482045
for address, cursor_ids in address_to_cursor_ids.items():
20492046
try:
20502047
self._kill_cursors(cursor_ids, address, topology, session=None)
2051-
except asyncio.CancelledError:
2052-
raise
20532048
except Exception as exc:
20542049
if isinstance(exc, InvalidOperation) and self._topology._closed:
20552050
raise
@@ -2064,8 +2059,6 @@ def _process_periodic_tasks(self) -> None:
20642059
try:
20652060
self._process_kill_cursors()
20662061
self._topology.update_pool()
2067-
except asyncio.CancelledError:
2068-
raise
20692062
except Exception as exc:
20702063
if isinstance(exc, InvalidOperation) and self._topology._closed:
20712064
return

pymongo/synchronous/monitor.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
import asyncio
2019
import atexit
2120
import logging
2221
import time
@@ -257,8 +256,6 @@ def _check_server(self) -> ServerDescription:
257256
details = cast(Mapping[str, Any], exc.details)
258257
self._topology.receive_cluster_time(details.get("$clusterTime"))
259258
raise
260-
except asyncio.CancelledError:
261-
raise
262259
except ReferenceError:
263260
raise
264261
except Exception as error:
@@ -424,8 +421,6 @@ def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
424421
if len(seedlist) == 0:
425422
# As per the spec: this should be treated as a failure.
426423
raise Exception
427-
except asyncio.CancelledError:
428-
raise
429424
except Exception:
430425
# As per the spec, upon encountering an error:
431426
# - An error must not be raised
@@ -489,8 +484,6 @@ def _run(self) -> None:
489484
except ReferenceError:
490485
# Topology was garbage-collected.
491486
self.close()
492-
except asyncio.CancelledError:
493-
raise
494487
except Exception:
495488
self._pool.reset()
496489

pymongo/synchronous/pool.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,6 @@ def _close_conn(self) -> None:
702702
# shutdown.
703703
try:
704704
self.conn.close()
705-
except asyncio.CancelledError:
706-
raise
707705
except Exception: # noqa: S110
708706
pass
709707

0 commit comments

Comments
 (0)