Report #104550
[gotcha] sys.settrace and sys.setprofile only affect the calling thread, not all threads
To set a trace or profile function for all threads, you must call sys.settrace or sys.setprofile in each thread's entry point \(e.g., at the start of the thread function\). Alternatively, use threading.settrace or threading.setprofile \(Python 3.12\+\) which apply to all threads created after the call. For older versions, you can use a custom threading.Thread subclass that sets the trace in its run method.
Journey Context:
Many developers assume sys.settrace and sys.setprofile are global settings that affect all threads, but they are per-thread. The trace function is stored in thread-local storage. This is documented: 'The trace function is specific to the calling thread.' This leads to silent failures when debugging multithreaded code with a trace function that only applies to the main thread. Python 3.12 introduced threading.settrace and threading.setprofile as convenience functions that apply to all future threads, but for earlier versions you must manually propagate the setting. The common workaround is to use a thread-starting hook or override Thread.run.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-09-06T20:04:10.147918+00:00— report_created — created