11from __future__ import annotations
22
3- import atexit
43import inspect
54import sys
65import traceback
76import warnings
8- from functools import cached_property , partial
7+ from functools import cached_property
98from time import time
109from typing import TYPE_CHECKING , Any , Callable , ContextManager , Iterable , Literal , Sequence , TypeVar , Union , cast
1110
2120from ..version import VERSION
2221from . import async_
2322from .auto_trace import AutoTraceModule , install_auto_tracing
24- from .config import GLOBAL_CONFIG , LogfireConfig
23+ from .config import GLOBAL_CONFIG , OPEN_SPANS , LogfireConfig
2524from .constants import (
2625 ATTRIBUTES_JSON_SCHEMA_KEY ,
2726 ATTRIBUTES_LOG_LEVEL_NUM_KEY ,
@@ -1577,20 +1576,20 @@ def shutdown(self, timeout_millis: int = 30_000, flush: bool = True) -> bool: #
15771576class FastLogfireSpan :
15781577 """A simple version of `LogfireSpan` optimized for auto-tracing."""
15791578
1580- __slots__ = ('_span' , '_token' , '_atexit' )
1579+ # __weakref__ is needed for the OPEN_SPANS WeakSet.
1580+ __slots__ = ('_span' , '_token' , '__weakref__' )
15811581
15821582 def __init__ (self , span : trace_api .Span ) -> None :
15831583 self ._span = span
15841584 self ._token = context_api .attach (trace_api .set_span_in_context (self ._span ))
1585- self ._atexit = partial (self .__exit__ , None , None , None )
1586- atexit .register (self ._atexit )
1585+ OPEN_SPANS .add (self )
15871586
15881587 def __enter__ (self ) -> FastLogfireSpan :
15891588 return self
15901589
15911590 @handle_internal_errors ()
15921591 def __exit__ (self , exc_type : type [BaseException ] | None , exc_value : BaseException | None , traceback : Any ) -> None :
1593- atexit . unregister (self . _atexit )
1592+ OPEN_SPANS . remove (self )
15941593 context_api .detach (self ._token )
15951594 _exit_span (self ._span , exc_value )
15961595 self ._span .end ()
@@ -1615,7 +1614,6 @@ def __init__(
16151614 self ._token : None | object = None
16161615 self ._span : None | trace_api .Span = None
16171616 self .end_on_exit = True
1618- self ._atexit : Callable [[], None ] | None = None
16191617
16201618 if not TYPE_CHECKING : # pragma: no branch
16211619
@@ -1633,8 +1631,7 @@ def __enter__(self) -> LogfireSpan:
16331631 if self ._token is None : # pragma: no branch
16341632 self ._token = context_api .attach (trace_api .set_span_in_context (self ._span ))
16351633
1636- self ._atexit = partial (self .__exit__ , None , None , None )
1637- atexit .register (self ._atexit )
1634+ OPEN_SPANS .add (self )
16381635
16391636 return self
16401637
@@ -1643,8 +1640,7 @@ def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseExceptio
16431640 if self ._token is None : # pragma: no cover
16441641 return
16451642
1646- if self ._atexit : # pragma: no branch
1647- atexit .unregister (self ._atexit )
1643+ OPEN_SPANS .remove (self )
16481644
16491645 context_api .detach (self ._token )
16501646 self ._token = None
@@ -1656,8 +1652,6 @@ def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseExceptio
16561652 if end_on_exit_ :
16571653 self .end ()
16581654
1659- self ._token = None
1660-
16611655 @property
16621656 def message_template (self ) -> str | None : # pragma: no cover
16631657 return self ._get_attribute (ATTRIBUTES_MESSAGE_TEMPLATE_KEY , None )
0 commit comments