@@ -573,29 +573,30 @@ def start_as_current_span(
573573 "Tags will be ignored."
574574 )
575575
576- with tracer .start_as_current_span (
577- name ,
578- context = ctx ,
579- attributes = {
580- SPAN_TYPE : span_type ,
581- PARENT_SPAN_PATH : parsed ["path" ],
582- PARENT_SPAN_IDS_PATH : parsed ["span_ids_path" ],
583- ** (label_props ),
584- ** (tag_props ),
585- # Association properties are attached to context above
586- # and the relevant attributes are populated in the processor
587- },
588- ) as span :
589- if not isinstance (span , LaminarSpan ):
590- span = LaminarSpan (span )
591- span .set_input (input )
592- yield span
593-
594576 try :
595- detach_context (isolated_context_token )
596- context_api .detach (ctx_token )
597- except Exception :
598- pass
577+ with tracer .start_as_current_span (
578+ name ,
579+ context = ctx ,
580+ attributes = {
581+ SPAN_TYPE : span_type ,
582+ PARENT_SPAN_PATH : parsed ["path" ],
583+ PARENT_SPAN_IDS_PATH : parsed ["span_ids_path" ],
584+ ** (label_props ),
585+ ** (tag_props ),
586+ # Association properties are attached to context above
587+ # and the relevant attributes are populated in the processor
588+ },
589+ ) as span :
590+ if not isinstance (span , LaminarSpan ):
591+ span = LaminarSpan (span )
592+ span .set_input (input )
593+ yield span
594+ finally :
595+ try :
596+ detach_context (isolated_context_token )
597+ context_api .detach (ctx_token )
598+ except Exception :
599+ pass
599600
600601 @classmethod
601602 def start_span (
@@ -852,10 +853,6 @@ def use_span(
852853 else :
853854 yield LaminarSpan (span )
854855
855- context_api .detach (context_token )
856- detach_context (isolated_context_token )
857- wrapper .pop_span_context ()
858-
859856 # Record only exceptions that inherit Exception class but not BaseException, because
860857 # classes that directly inherit BaseException are not technically errors, e.g. GeneratorExit.
861858 # See https://github.com/open-telemetry/opentelemetry-python/issues/4484
@@ -883,6 +880,9 @@ def use_span(
883880 raise
884881
885882 finally :
883+ context_api .detach (context_token )
884+ detach_context (isolated_context_token )
885+ wrapper .pop_span_context ()
886886 if end_on_exit :
887887 span .end ()
888888
0 commit comments