Skip to content

Commit f0feea3

Browse files
committed
Preserve target-exit errors during deferred asyncio discovery
Check the known runtime address when optional image lookup fails, and preserve fatal exceptions through async discovery.
1 parent 7d04fce commit f0feea3

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,6 +4687,28 @@ def test_dead_process_raises_not_degrades(self):
46874687
else:
46884688
self.fail("ProcessLookupError never raised for dead process")
46894689

4690+
def test_dead_process_before_first_async_read(self):
4691+
script_body = """\
4692+
import asyncio
4693+
sock.sendall(b"ready")
4694+
sock.recv(16)
4695+
"""
4696+
with self._target_process(script_body) as (p, client_socket, _):
4697+
_wait_for_signal(client_socket, b"ready")
4698+
unwinders = [
4699+
RemoteUnwinder(p.pid, debug=debug)
4700+
for debug in (False, True)
4701+
]
4702+
4703+
p.kill()
4704+
p.wait()
4705+
4706+
for debug, unwinder in zip((False, True), unwinders):
4707+
for method in ("get_async_stack_trace", "get_all_awaited_by"):
4708+
with self.subTest(debug=debug, method=method):
4709+
with self.assertRaises(ProcessLookupError):
4710+
getattr(unwinder, method)()
4711+
46904712

46914713
@requires_remote_subprocess_debugging()
46924714
class TestFrameChainLimits(RemoteInspectionTestBase):

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
2222
address = search_windows_map_for_section(handle, "AsyncioD", L"_asyncio",
2323
NULL);
2424
if (address == 0) {
25-
if (!_Py_RemoteDebug_HasPermissionError()) {
25+
if (!_Py_RemoteDebug_IsFatalReadError()) {
2626
PyObject *exc = PyErr_GetRaisedException();
2727
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
2828
_PyErr_ChainExceptions1(exc);
@@ -33,7 +33,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
3333
address = search_linux_map_for_section(handle, "AsyncioDebug", "python",
3434
NULL);
3535
if (address == 0) {
36-
if (!_Py_RemoteDebug_HasPermissionError()) {
36+
if (!_Py_RemoteDebug_IsFatalReadError()) {
3737
PyObject *exc = PyErr_GetRaisedException();
3838
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
3939
_PyErr_ChainExceptions1(exc);
@@ -42,7 +42,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle)
4242
#elif defined(__APPLE__) && TARGET_OS_OSX
4343
address = search_loaded_images_for_section(handle, "AsyncioDebug", NULL);
4444
if (address == 0) {
45-
if (!_Py_RemoteDebug_HasPermissionError()) {
45+
if (!_Py_RemoteDebug_IsFatalReadError()) {
4646
PyObject *exc = PyErr_GetRaisedException();
4747
PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process.");
4848
_PyErr_ChainExceptions1(exc);
@@ -92,12 +92,22 @@ ensure_async_debug_offsets(RemoteUnwinderObject *unwinder)
9292
return -1;
9393
}
9494
if (result < 0) {
95-
if (!_Py_RemoteDebug_HasPermissionError()) {
96-
PyErr_Clear();
97-
PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available");
98-
set_exception_cause(unwinder, PyExc_RuntimeError,
99-
"AsyncioDebug section unavailable - asyncio module may not be loaded in target process");
95+
if (_Py_RemoteDebug_IsFatalReadError()) {
96+
return -1;
97+
}
98+
PyErr_Clear();
99+
/* Image discovery can fail without reporting that the target exited.
100+
Read a known runtime address before reporting unavailable asyncio. */
101+
char runtime_byte;
102+
if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle,
103+
unwinder->runtime_start_address, sizeof(runtime_byte),
104+
&runtime_byte) < 0)
105+
{
106+
return -1;
100107
}
108+
PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available");
109+
set_exception_cause(unwinder, PyExc_RuntimeError,
110+
"AsyncioDebug section unavailable - asyncio module may not be loaded in target process");
101111
return -1;
102112
}
103113

0 commit comments

Comments
 (0)