@@ -329,6 +329,36 @@ feature. This way, tools can offer a useful error message explaining why they
329329won't work, instead of believing that they have attached and then never having
330330their script run.
331331
332+ Multi-threading Considerations
333+ -----------------------------
334+
335+ Debugging code injected through this interface executes opportunistically in
336+ whichever thread first encounters a safe evaluation point after the request is
337+ made. This behavior mirrors how Python handles signals, providing a reliable
338+ execution model without adding overhead. For developers needing to target
339+ specific threads, the debug script can be installed only on the desired thread
340+ structure or in all of them if needed.
341+
342+ The Global Interpreter Lock (GIL) continues to govern execution as normal when
343+ debug code runs. This means if a target thread is currently executing a C
344+ extension that holds the GIL without releasing it, the debug code will wait
345+ until that operation completes and the GIL becomes available. However, the
346+ interface introduces no additional GIL contention beyond what the debugging
347+ code itself requires. Importantly, the interface remains fully compatible with
348+ Python's free-threaded mode, where the GIL is not held, allowing debugger code
349+ to execute in any available thread.
350+
351+ In situations where all threads in the target process are blocked—waiting on I/O
352+ operations, sleep states, or external resources—the debugging code might not
353+ execute immediately. In these cases, debuggers can send a pre-registered signal
354+ to the process, which will interrupt the sleep state and force thread scheduling,
355+ creating an opportunity for the debug code to run.
356+
357+ The execution pattern closely resembles how Python handles signals internally.
358+ The interpreter guarantees that debug code only runs at safe points, never
359+ interrupting atomic operations within the interpreter itself. This approach
360+ ensures that debugging operations cannot corrupt the interpreter state while
361+ still providing timely execution in most real-world scenarios.
332362
333363Backwards Compatibility
334364=======================
0 commit comments