@@ -51,16 +51,24 @@ def _set_patch_target(
5151# Also patch threading.Thread so echion can track thread lifetimes
5252def init_stack_v2 () -> None :
5353 if config .stack .enabled and stack_v2 .is_available :
54- _thread_set_native_id = ddtrace_threading .Thread ._set_native_id # type: ignore[attr-defined]
55- _thread_bootstrap_inner = ddtrace_threading .Thread ._bootstrap_inner # type: ignore[attr-defined]
56-
57- def thread_set_native_id (self , * args , ** kwargs ):
58- _thread_set_native_id (self , * args , ** kwargs )
59- stack_v2 .register_thread (self .ident , self .native_id , self .name )
60-
61- def thread_bootstrap_inner (self , * args , ** kwargs ):
54+ _thread_set_native_id = typing .cast (
55+ typing .Callable [[threading .Thread ], None ],
56+ ddtrace_threading .Thread ._set_native_id , # type: ignore[attr-defined]
57+ )
58+ _thread_bootstrap_inner = typing .cast (
59+ typing .Callable [[threading .Thread ], None ],
60+ ddtrace_threading .Thread ._bootstrap_inner , # type: ignore[attr-defined]
61+ )
62+
63+ def thread_set_native_id (self : threading .Thread ) -> None :
64+ _thread_set_native_id (self )
65+ if self .ident is not None and self .native_id is not None :
66+ stack_v2 .register_thread (self .ident , self .native_id , self .name )
67+
68+ def thread_bootstrap_inner (self : threading .Thread , * args : typing .Any , ** kwargs : typing .Any ) -> None :
6269 _thread_bootstrap_inner (self , * args , ** kwargs )
63- stack_v2 .unregister_thread (self .ident )
70+ if self .ident is not None :
71+ stack_v2 .unregister_thread (self .ident )
6472
6573 ddtrace_threading .Thread ._set_native_id = thread_set_native_id # type: ignore[attr-defined]
6674 ddtrace_threading .Thread ._bootstrap_inner = thread_bootstrap_inner # type: ignore[attr-defined]
0 commit comments