Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

try:
from concurrent import interpreters
from concurrent.futures import InterpreterPoolExecutor
except ImportError:
interpreters = None

Expand Down Expand Up @@ -214,6 +215,15 @@ def _cleanup_sockets(*sockets):
)


def _asyncio_in_subinterpreter():
import asyncio

async def sub_worker():
await asyncio.sleep(2)

asyncio.run(sub_worker())


def requires_subinterpreters(meth):
"""Decorator to skip a test if subinterpreters are not supported."""
return unittest.skipIf(interpreters is None, "subinterpreters required")(
Expand Down Expand Up @@ -492,6 +502,36 @@ async def main():
self.assertIn(main_name, names)
self.assertEqual([len(n) for n in names if n.startswith("x")], [255])

@skip_if_not_supported
@requires_subinterpreters
def test_all_awaited_by_covers_every_interpreter(self):
# gh-158880
async def main_worker():
await asyncio.sleep(SHORT_TIMEOUT)

async def main():
with InterpreterPoolExecutor() as pool:
loop = asyncio.get_running_loop()
loop.run_in_executor(pool, _asyncio_in_subinterpreter)
task = asyncio.create_task(main_worker(), name="main_worker")
self.addCleanup(task.cancel)
for _ in busy_retry(SHORT_TIMEOUT):
await asyncio.sleep(0)
stacks = [
[frame.funcname.rpartition(".")[2]
for frame in coro.call_stack]
for info in RemoteUnwinder(
os.getpid()).get_all_awaited_by()
Comment on lines +523 to +524

@maurycy maurycy Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to silence https://github.com/python/cpython/actions/runs/37499870518/job/112393935495#step:6:1023 is by wrapping this in except TRANSIENT_ERRORS:

Perhaps a better approach, similar to #158801

from _queue import SimpleQueue
from test import support
go = threading.Lock()
stop = threading.Lock()
go.acquire()
stop.acquire()
ready = SimpleQueue()
def leaf():
ready.put(None)
stop.acquire()
def start_leaf():
ready.put(None)
go.acquire()
leaf()
def park():
ready.put(None)
stop.acquire()

# SimpleQueue.put() and Lock.acquire() do not push Python frames.
# Once notified, the worker's stack stays stable until go is released.
threading.Thread(target=leaf, daemon=True).start()
ready.get(timeout=support.SHORT_TIMEOUT)
for _ in range(16):
threading.Thread(target=park, daemon=True).start()
ready.get(timeout=support.SHORT_TIMEOUT)
threading.Thread(target=start_leaf, daemon=True).start()
ready.get(timeout=support.SHORT_TIMEOUT)
u = RemoteUnwinder(os.getpid(), all_threads=True, cache_frames=False)
assert leaf_count(u) == 1
go.release()
ready.get(timeout=support.SHORT_TIMEOUT)

for task in info.awaited_by
for coro in task.coroutine_stack
]
if ["sleep", "sub_worker"] in stacks:
return stacks

stacks = asyncio.run(main())
self.assertIn(["sleep", "sub_worker"], stacks)
self.assertIn(["sleep", "main_worker"], stacks)

@skip_if_not_supported
def test_recursive_coroutine_stack_is_not_truncated(self):
# gh-158522
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``python -m asyncio ps`` and ``pstree`` showing tasks from only one
interpreter. Patch by Timofei Ivankov.
1 change: 1 addition & 0 deletions Modules/_remote_debugging/_remote_debugging.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ extern int collect_frames_with_cache(

extern int iterate_threads(
RemoteUnwinderObject *unwinder,
uintptr_t interpreter_addr,
thread_processor_func processor,
void *context
);
Expand Down
72 changes: 55 additions & 17 deletions Modules/_remote_debugging/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,9 @@ _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *s
if (ensure_async_debug_offsets(self) < 0) {
return NULL;
}
if (refresh_generation_caches_for_interpreter(self, self->interpreter_addr) < 0) {
PyObject *seen = PySet_New(NULL);

@maurycy maurycy Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a list:

uintptr_t current_interpreter = self->interpreter_addr;
while (current_interpreter != 0) {

current_interpreter = GET_MEMBER(uintptr_t, interp_state_buffer,
self->debug_offsets.interpreter_state.next);

const size_t MAX_INTERPRETERS = 256;
size_t interp_count = 0;
while (current_interp != 0 && interp_count < MAX_INTERPRETERS) {

Is there a reproducible race scenario where we'd see a cycle? (It was quiet easy to reproduce issues like ABA.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I couldn't reproduce a cycle. I thought the interpreter walk could loop
because of the previous PR, that's why I added the set. I agree with your suggestion and will rework it soon

if (seen == NULL) {
set_exception_cause(self, PyExc_MemoryError, "Failed to create interpreter set");
return NULL;
}

Expand All @@ -968,30 +970,65 @@ _remote_debugging_RemoteUnwinder_get_all_awaited_by_impl(RemoteUnwinderObject *s
goto result_err;
}

// Process all threads
if (iterate_threads(self, process_thread_for_awaited_by, result) < 0) {
goto result_err;
}
// gh-158880: Tasks live in every interpreter, not only the one at the list head
for (uintptr_t interp = self->interpreter_addr; interp != 0; ) {
PyObject *addr = PyLong_FromUnsignedLongLong(interp);
if (addr == NULL) {
set_exception_cause(self, PyExc_MemoryError, "Failed to create interpreter address");
goto result_err;
}
Py_ssize_t seen_count = PySet_GET_SIZE(seen);
int marked = PySet_Add(seen, addr);
Py_DECREF(addr);
if (marked < 0) {
set_exception_cause(self, PyExc_RuntimeError, "Failed to mark interpreter as seen");
goto result_err;
}
if (PySet_GET_SIZE(seen) == seen_count) {
// already walked
break;
}

uintptr_t head_addr = self->interpreter_addr
+ (uintptr_t)self->async_debug_offsets.asyncio_interpreter_state.asyncio_tasks_head;
if (refresh_generation_caches_for_interpreter(self, interp) < 0) {
goto result_err;
}

// On top of a per-thread task lists used by default by asyncio to avoid
// contention, there is also a fallback per-interpreter list of tasks;
// any tasks still pending when a thread is destroyed will be moved to the
// per-interpreter task list. It's unlikely we'll find anything here, but
// interesting for debugging.
if (append_awaited_by(self, 0, head_addr, result))
{
set_exception_cause(self, PyExc_RuntimeError, "Failed to append interpreter awaited_by in get_all_awaited_by");
goto result_err;
// Process all threads
if (iterate_threads(self, interp, process_thread_for_awaited_by, result) < 0) {
goto result_err;
}

uintptr_t head_addr = interp
+ (uintptr_t)self->async_debug_offsets.asyncio_interpreter_state.asyncio_tasks_head;

// On top of a per-thread task lists used by default by asyncio to avoid
// contention, there is also a fallback per-interpreter list of tasks;
// any tasks still pending when a thread is destroyed will be moved to
// the per-interpreter task list. It's unlikely we'll find anything
// here, but interesting for debugging.
if (append_awaited_by(self, 0, head_addr, result))
{
set_exception_cause(self, PyExc_RuntimeError, "Failed to append interpreter awaited_by in get_all_awaited_by");
goto result_err;
}

if (_Py_RemoteDebug_PagedReadRemoteMemory(
&self->handle,
interp + (uintptr_t)self->debug_offsets.interpreter_state.next,
sizeof(void*),
&interp) < 0) {
set_exception_cause(self, PyExc_RuntimeError, "Failed to read next interpreter address");
goto result_err;
}
}

_Py_RemoteDebug_ClearCache(&self->handle);
Py_DECREF(seen);
return result;

result_err:
_Py_RemoteDebug_ClearCache(&self->handle);
Py_DECREF(seen);
Py_XDECREF(result);
return NULL;
}
Expand Down Expand Up @@ -1064,7 +1101,8 @@ _remote_debugging_RemoteUnwinder_get_async_stack_trace_impl(RemoteUnwinderObject
}

// Process all threads
if (iterate_threads(self, process_thread_for_async_stack_trace, result) < 0) {
if (iterate_threads(self, self->interpreter_addr,

@maurycy maurycy Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, get_async_stack_trace() still has the same problem. Maybe it should be a follow-up, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it has the problem, i planned to open a separate issue for this to avoid "big-bang" pr

process_thread_for_async_stack_trace, result) < 0) {
goto result_err;
}

Expand Down
3 changes: 2 additions & 1 deletion Modules/_remote_debugging/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
int
iterate_threads(
RemoteUnwinderObject *unwinder,
uintptr_t interpreter_addr,
thread_processor_func processor,
void *context
) {
Expand All @@ -37,7 +38,7 @@ iterate_threads(

if (0 > _Py_RemoteDebug_PagedReadRemoteMemory(
&unwinder->handle,
unwinder->interpreter_addr + (uintptr_t)unwinder->debug_offsets.interpreter_state.threads_head,
interpreter_addr + (uintptr_t)unwinder->debug_offsets.interpreter_state.threads_head,
sizeof(void*),
&thread_state_addr))
{
Expand Down
Loading