Skip to content

Commit 35710a5

Browse files
miss-islingtonpablogsalasvetlovtanloongvstinner
authored
[3.15] gh-157605: Accept empty native-thread stacks when sampling (#161784)
Accept empty Python stacks in native threads. Distinguish interpreter-owned C frames from cleared Python frames to prevent false <native> markers. (cherry picked from commit 80f9aa0) (cherry picked from commit eeafc73) (cherry picked from commit 921de8f) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Tan Long <tanloong@foxmail.com> Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ab9e306 commit 35710a5

3 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,84 @@ def test_self_trace(self):
19781978
self.assertEqual(this_thread_stack[1].funcname, "TestGetStackTrace.test_self_trace")
19791979
self.assertTrue(this_thread_stack[1].filename.endswith("test_external_inspection.py"))
19801980

1981+
@skip_if_not_supported
1982+
@unittest.skipIf(
1983+
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
1984+
"Test only runs on Linux with process_vm_readv support",
1985+
)
1986+
def test_empty_native_thread_stack(self):
1987+
_testcapi = import_module("_testcapi")
1988+
lock = threading.Lock()
1989+
lock.acquire()
1990+
# A built-in callback leaves the C thread's Python stack empty.
1991+
_testcapi.call_in_temporary_c_thread(lock.acquire, False)
1992+
try:
1993+
for cache_frames, native in ((False, False), (False, True),
1994+
(True, False), (True, True)):
1995+
with self.subTest(cache_frames=cache_frames, native=native):
1996+
unwinder = RemoteUnwinder(
1997+
os.getpid(), all_threads=True, cache_frames=cache_frames,
1998+
native=native,
1999+
)
2000+
_get_stack_trace_with_retry(
2001+
unwinder, condition=lambda trace: len(trace[0].threads) == 2,
2002+
)
2003+
threads = unwinder.get_stack_trace()[0].threads
2004+
native_stack, python_stack = sorted(
2005+
(thread.frame_info for thread in threads), key=len,
2006+
)
2007+
self.assertEqual(native_stack, [])
2008+
self.assertEqual(
2009+
python_stack[0].funcname,
2010+
"TestGetStackTrace.test_empty_native_thread_stack",
2011+
)
2012+
finally:
2013+
lock.release()
2014+
_testcapi.join_temporary_c_thread()
2015+
2016+
@skip_if_not_supported
2017+
@unittest.skipIf(
2018+
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
2019+
"Test only runs on Linux with process_vm_readv support",
2020+
)
2021+
def test_popping_python_frame_is_not_native(self):
2022+
script_body = """\
2023+
def leaf(depth):
2024+
if depth:
2025+
leaf(depth - 1)
2026+
2027+
while True:
2028+
leaf(300)
2029+
"""
2030+
2031+
with self._target_process(script_body) as (process, _, _):
2032+
unwinder = RemoteUnwinder(
2033+
process.pid, native=True, gc=False, cache_frames=False,
2034+
)
2035+
# The unwinder can attach before the main loop starts.
2036+
_get_stack_trace_with_retry(
2037+
unwinder,
2038+
condition=lambda trace: trace[0].threads and any(
2039+
frame.funcname == "leaf"
2040+
for frame in trace[0].threads[0].frame_info
2041+
),
2042+
)
2043+
samples = 0
2044+
for _ in range(10_000):
2045+
try:
2046+
threads = unwinder.get_stack_trace()[0].threads
2047+
except TRANSIENT_ERRORS:
2048+
continue
2049+
if not threads:
2050+
continue
2051+
frames = threads[0].frame_info
2052+
names = [frame.funcname for frame in frames]
2053+
if "leaf" not in names:
2054+
continue
2055+
samples += 1
2056+
self.assertNotIn(("leaf", "<native>"), zip(names, names[1:]))
2057+
self.assertGreater(samples, 1000)
2058+
19812059
@skip_if_not_supported
19822060
@unittest.skipIf(
19832061
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`profiling.sampling` failing when a native thread has an empty Python
2+
stack.

‎Modules/_remote_debugging/frames.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,23 @@ find_frame_in_chunks(StackChunkList *chunks, uintptr_t remote_ptr)
163163
* FRAME PARSING FUNCTIONS
164164
* ============================================================================ */
165165

166+
enum { FRAME_PARSE_INTERPRETER = 2 };
167+
166168
int
167169
is_frame_valid(
168170
RemoteUnwinderObject *unwinder,
169171
uintptr_t frame_addr,
170172
uintptr_t code_object_addr
171173
) {
172174
if ((void*)code_object_addr == NULL) {
173-
return 0;
175+
return 0; // Frame being cleared
174176
}
175177

176178
void* frame = (void*)frame_addr;
177179

178180
char owner = GET_MEMBER(char, frame, unwinder->debug_offsets.interpreter_frame.owner);
179181
if (owner == FRAME_OWNED_BY_INTERPRETER) {
180-
return 0; // C frame or sentinel base frame
182+
return FRAME_PARSE_INTERPRETER; // C frame or sentinel base frame
181183
}
182184

183185
if (owner != FRAME_OWNED_BY_GENERATOR && owner != FRAME_OWNED_BY_THREAD) {
@@ -280,6 +282,7 @@ process_frame_chain(
280282
ctx->last_frame_visited = 0;
281283

282284
while ((void*)frame_addr != NULL) {
285+
int parse_result = 0;
283286
PyObject *frame = NULL;
284287
uintptr_t next_frame_addr = 0;
285288
last_frame_addr = frame_addr;
@@ -300,14 +303,13 @@ process_frame_chain(
300303
}
301304

302305
if (ctx->chunks && ctx->chunks->count > 0) {
303-
int parse_result = parse_frame_from_chunks(
306+
parse_result = parse_frame_from_chunks(
304307
unwinder, &frame, frame_addr, &next_frame_addr, ctx->chunks);
305308
if (parse_result == 0) {
306309
goto parsed_frame;
307310
}
308311
PyErr_Clear();
309312
}
310-
int parse_result;
311313
if (ctx->prefetch.frame && ctx->prefetch.frame_addr == frame_addr) {
312314
parse_result = parse_frame_buffer(
313315
unwinder, &frame, ctx->prefetch.frame, &next_frame_addr);
@@ -329,19 +331,19 @@ process_frame_chain(
329331
continue;
330332
}
331333

332-
if (frame == NULL && PyList_GET_SIZE(ctx->frame_info) == 0) {
333-
const char *e = "Failed to parse initial frame in chain";
334-
PyErr_SetString(PyExc_RuntimeError, e);
335-
return -1;
336-
}
337334
PyObject *extra_frame = NULL;
338335
if (unwinder->gc && frame_addr == ctx->gc_frame) {
339336
_Py_DECLARE_STR(gc, "<GC>");
340337
extra_frame = &_Py_STR(gc);
341338
}
339+
// A leading frame without Python code marks no transition between
340+
// Python frames: it is a frame being popped or C code the thread is
341+
// returning into.
342342
else if (unwinder->native &&
343343
frame == NULL &&
344+
parse_result == FRAME_PARSE_INTERPRETER &&
344345
next_frame_addr &&
346+
PyList_GET_SIZE(ctx->frame_info) > 0 &&
345347
!(unwinder->gc && next_frame_addr == ctx->gc_frame))
346348
{
347349
_Py_DECLARE_STR(native, "<native>");

0 commit comments

Comments
 (0)