@@ -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 )
0 commit comments