Skip to content

Commit 3cfdfee

Browse files
committed
fix logic
1 parent 61077d9 commit 3cfdfee

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,10 +2777,10 @@ async def run(self) -> T:
27772777
try:
27782778
res = await self._read() if self._is_read else await self._write()
27792779
await self._retry_policy.record_success(self._attempt_number > 0)
2780-
# Track whether the transaction has completed command.
2780+
# Track whether the transaction has completed a command.
27812781
# If we need to apply backpressure to the first command,
27822782
# we will need to revert back to starting state.
2783-
if self._session.in_transaction:
2783+
if self._session is not None and self._session.in_transaction:
27842784
self._session._transaction.has_completed_command = True
27852785
return res
27862786
except ServerSelectionTimeoutError:
@@ -2796,7 +2796,6 @@ async def run(self) -> T:
27962796
always_retryable = False
27972797
overloaded = False
27982798
exc_to_check = exc
2799-
28002799
# Execute specialized catch on read
28012800
if self._is_read:
28022801
if isinstance(exc, (ConnectionFailure, OperationFailure)):
@@ -2820,11 +2819,11 @@ async def run(self) -> T:
28202819

28212820
# Revert back to starting state if we're in a transaction but haven't completed the first
28222821
# command.
2823-
if (
2824-
self._session.in_transaction
2825-
and not self._session._transaction.has_completed_command
2826-
):
2827-
self._session._transaction.set_starting()
2822+
if self._session is not None and self._session.in_transaction:
2823+
if not self._session._transaction.has_completed_command:
2824+
self._session._transaction.set_starting()
2825+
else:
2826+
raise
28282827

28292828
# Specialized catch on write operation
28302829
if not self._is_read:
@@ -2860,11 +2859,9 @@ async def run(self) -> T:
28602859
self._last_error = exc
28612860
# Revert back to starting state if we're in a transaction but haven't completed the first
28622861
# command.
2863-
if (
2864-
self._session.in_transaction
2865-
and not self._session._transaction.has_completed_command
2866-
):
2867-
self._session._transaction.set_starting()
2862+
if self._session is not None and self._session.in_transaction:
2863+
if not self._session._transaction.has_completed_command:
2864+
self._session._transaction.set_starting()
28682865

28692866
if self._client.topology_description.topology_type == TOPOLOGY_TYPE.Sharded:
28702867
self._deprioritized_servers.append(self._server)

pymongo/synchronous/mongo_client.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,10 +2767,10 @@ def run(self) -> T:
27672767
try:
27682768
res = self._read() if self._is_read else self._write()
27692769
self._retry_policy.record_success(self._attempt_number > 0)
2770-
# Track whether the transaction has completed command.
2770+
# Track whether the transaction has completed a command.
27712771
# If we need to apply backpressure to the first command,
27722772
# we will need to revert back to starting state.
2773-
if self._session.in_transaction:
2773+
if self._session is not None and self._session.in_transaction:
27742774
self._session._transaction.has_completed_command = True
27752775
return res
27762776
except ServerSelectionTimeoutError:
@@ -2786,7 +2786,6 @@ def run(self) -> T:
27862786
always_retryable = False
27872787
overloaded = False
27882788
exc_to_check = exc
2789-
27902789
# Execute specialized catch on read
27912790
if self._is_read:
27922791
if isinstance(exc, (ConnectionFailure, OperationFailure)):
@@ -2810,11 +2809,11 @@ def run(self) -> T:
28102809

28112810
# Revert back to starting state if we're in a transaction but haven't completed the first
28122811
# command.
2813-
if (
2814-
self._session.in_transaction
2815-
and not self._session._transaction.has_completed_command
2816-
):
2817-
self._session._transaction.set_starting()
2812+
if self._session is not None and self._session.in_transaction:
2813+
if not self._session._transaction.has_completed_command:
2814+
self._session._transaction.set_starting()
2815+
else:
2816+
raise
28182817

28192818
# Specialized catch on write operation
28202819
if not self._is_read:
@@ -2850,11 +2849,9 @@ def run(self) -> T:
28502849
self._last_error = exc
28512850
# Revert back to starting state if we're in a transaction but haven't completed the first
28522851
# command.
2853-
if (
2854-
self._session.in_transaction
2855-
and not self._session._transaction.has_completed_command
2856-
):
2857-
self._session._transaction.set_starting()
2852+
if self._session is not None and self._session.in_transaction:
2853+
if not self._session._transaction.has_completed_command:
2854+
self._session._transaction.set_starting()
28582855

28592856
if self._client.topology_description.topology_type == TOPOLOGY_TYPE.Sharded:
28602857
self._deprioritized_servers.append(self._server)

0 commit comments

Comments
 (0)