@@ -565,8 +565,7 @@ def __init__(
565565 self ._implicit = implicit
566566 self ._transaction = _Transaction (None , client )
567567 # The one "transaction" span shared across every retry of a single
568- # with_transaction() call, or None outside of it, where
569- # start/commit/abort_transaction each manage their own span.
568+ # with_transaction() call; the direct API manages its own span instead.
570569 self ._with_transaction_span : Optional [Any ] = None
571570 # Is this session attached to a cursor?
572571 self ._attached_to_cursor = False
@@ -776,17 +775,14 @@ async def callback(session, custom_arg, custom_kwarg=None):
776775 https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback
777776 """
778777 if self ._with_transaction_span is not None :
779- # Raise before any span bookkeeping, so a nested call cannot
780- # clobber and leak the outer call's span.
778+ # Before any span bookkeeping, so a nested call cannot leak the outer span.
781779 raise InvalidOperation (
782780 "Cannot call with_transaction() while a previous with_transaction() "
783781 "call on this session has not returned; sessions do not support "
784782 "nested or concurrent with_transaction() calls"
785783 )
786- # One span for the whole call: start_transaction reuses it and
787- # commit/abort leave it open, so a retried with_transaction() yields a
788- # single span. Skipped when a direct-API transaction is already active,
789- # since start_transaction() raises below and the span would be empty.
784+ # Skipped when a direct-API transaction is already active, since
785+ # start_transaction() raises below and the span would be empty.
790786 tracing_options = self ._client .options .tracing
791787 if _otel ._is_tracing_enabled (tracing_options ) and not self .in_transaction :
792788 self ._with_transaction_span = _otel .start_transaction_span (tracing_options )
@@ -797,8 +793,7 @@ async def callback(session, custom_arg, custom_kwarg=None):
797793 finally :
798794 if self ._with_transaction_span is not None :
799795 _otel .end_transaction_span (self ._with_transaction_span )
800- # Only clear the span this call owns; a concurrent direct-API
801- # transaction's span belongs to that transaction.
796+ # A direct-API transaction's span belongs to that transaction.
802797 if self ._transaction .span is self ._with_transaction_span :
803798 self ._transaction .span = None
804799 self ._with_transaction_span = None
@@ -908,8 +903,7 @@ async def start_transaction(
908903 await self ._transaction .reset ()
909904 self ._transaction .state = _TxnState .STARTING
910905 if self ._with_transaction_span is not None :
911- # Reuse with_transaction's shared span so a retried call still
912- # produces exactly one "transaction" span.
906+ # Reuse it so a retried with_transaction() still produces one span.
913907 self ._transaction .span = self ._with_transaction_span
914908 elif _otel ._is_tracing_enabled (self ._transaction .client .options .tracing ):
915909 self ._transaction .span = _otel .start_transaction_span (
@@ -921,9 +915,8 @@ async def start_transaction(
921915 def _end_own_transaction_span (self ) -> None :
922916 """End and clear the transaction span, unless with_transaction() owns it.
923917
924- A no-op while ``_with_transaction_span`` is set: that span is shared
925- across every retry, so ending it here would kill it on the first failed
926- attempt.
918+ That span is shared across retries, so ending it here would kill it on
919+ the first failed attempt.
927920 """
928921 if self ._transaction .span is not None and self ._with_transaction_span is None :
929922 _otel .end_transaction_span (self ._transaction .span )
@@ -949,9 +942,8 @@ async def commit_transaction(self) -> None:
949942 # We're explicitly retrying the commit, move the state back to
950943 # "in progress" so that in_transaction returns true.
951944 self ._transaction .state = _TxnState .IN_PROGRESS
952- # A direct-API retry needs a fresh span: the prior attempt's
953- # finally block already ended and cleared it. with_transaction
954- # pins its shared span instead, see _end_own_transaction_span.
945+ # The prior attempt's finally block already ended and cleared the
946+ # span, so an explicit commit retry needs a fresh one.
955947 if self ._transaction .span is None and _otel ._is_tracing_enabled (
956948 self ._transaction .client .options .tracing
957949 ):
0 commit comments