Skip to content

Commit 20ca63e

Browse files
committed
add paragraph about multiple threads
1 parent b4e3c99 commit 20ca63e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

peps/pep-0768.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,37 @@ feature. This way, tools can offer a useful error message explaining why they
329329
won't work, instead of believing that they have attached and then never having
330330
their 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 or leverage any other mechanism
356+
that can force the target process to resume execution.
357+
358+
The execution pattern closely resembles how Python handles signals internally.
359+
The interpreter guarantees that debug code only runs at safe points, never
360+
interrupting atomic operations within the interpreter itself. This approach
361+
ensures that debugging operations cannot corrupt the interpreter state while
362+
still providing timely execution in most real-world scenarios.
332363

333364
Backwards Compatibility
334365
=======================

0 commit comments

Comments
 (0)