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
52 changes: 52 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,21 @@ def _cleanup_sockets(*sockets):
)


def _asyncio_in_subinterpreter(ready, release):
"""Park an asyncio task in a subinterpreter until released."""
import asyncio

def wait(loop):
# Signal via the loop, so the task has already suspended
loop.call_soon_threadsafe(ready.put, None)
release.get()

async def sub_worker():
await asyncio.to_thread(wait, asyncio.get_running_loop())

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 +508,42 @@ 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
ready = interpreters.create_queue()
release = interpreters.create_queue()

async def main_worker():
await asyncio.sleep(SHORT_TIMEOUT)

async def main():
with InterpreterPoolExecutor() as pool:
try:
loop = asyncio.get_running_loop()
loop.run_in_executor(pool, _asyncio_in_subinterpreter,
ready, release)
task = asyncio.create_task(main_worker(),
name="main_worker")
self.addCleanup(task.cancel)
await asyncio.sleep(0)
ready.get(timeout=SHORT_TIMEOUT)
return [
[frame.funcname.rpartition(".")[2]
for frame in coro.call_stack]
for info in RemoteUnwinder(
os.getpid()).get_all_awaited_by()
Comment on lines +535 to +536

@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)

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.

I thought about how to stop the test flaking, so I decided to use queues in the test rather than silence exceptions. I ran the tests overnight many times, and there were no failures, ci here passed too

for task in info.awaited_by
for coro in task.coroutine_stack
]
finally:
release.put(None)

stacks = asyncio.run(main())
self.assertIn(["to_thread", "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
53 changes: 34 additions & 19 deletions Modules/_remote_debugging/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,33 +958,47 @@ _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) {
return NULL;
}

PyObject *result = PyList_New(0);
if (result == NULL) {
set_exception_cause(self, PyExc_MemoryError, "Failed to create awaited_by result list");
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
uintptr_t interp = self->interpreter_addr;
while (interp != 0) {
if (refresh_generation_caches_for_interpreter(self, interp) < 0) {
goto result_err;
}

uintptr_t head_addr = self->interpreter_addr
+ (uintptr_t)self->async_debug_offsets.asyncio_interpreter_state.asyncio_tasks_head;
// Process all threads
if (iterate_threads(self, interp, process_thread_for_awaited_by, result) < 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;
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);
Expand Down Expand Up @@ -1064,7 +1078,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