diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 78c66c0e0fc1de..b084b2154ccd2f 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2980,6 +2980,29 @@ def test_unsupported_platform_error(self): @requires_remote_subprocess_debugging() class TestDetectionOfThreadStatus(RemoteInspectionTestBase): + @unittest.skipIf( + sys.platform not in ("linux", "darwin", "win32"), + "Test only runs on supported platforms (Linux, macOS, or Windows)", + ) + @unittest.skipIf( + sys.platform == "android", "Android raises Linux-specific exception" + ) + def test_cpu_mode_refreshes_thread_status(self): + script = """\ + sock.recv(1) + while True: + pass + """ + with self._target_process(script) as (p, sock, _): + unwinder = RemoteUnwinder(p.pid, mode=PROFILING_MODE_CPU) + _get_stack_trace_with_retry( + unwinder, condition=lambda traces: not traces[0].threads + ) + sock.sendall(b"x") + _get_stack_trace_with_retry( + unwinder, condition=lambda traces: bool(traces[0].threads) + ) + def _run_thread_status_test(self, mode, check_condition): """ Common pattern for thread status detection tests. diff --git a/Misc/NEWS.d/next/Library/2026-12-03-20-50-37.gh-issue-138122.NqOgly.rst b/Misc/NEWS.d/next/Library/2026-12-03-20-50-37.gh-issue-138122.NqOgly.rst new file mode 100644 index 00000000000000..b2f67b8a4a54c8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-20-50-37.gh-issue-138122.NqOgly.rst @@ -0,0 +1,2 @@ +Fix empty CPU profiles in :mod:`profiling.sampling` when running 32-bit +Python on Windows Server 2022. diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 532fed28741738..82259d6a42d24f 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -103,6 +103,8 @@ typedef struct { #ifdef MS_WINDOWS #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) +#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L) +#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) #define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL) #endif @@ -371,6 +373,12 @@ typedef struct { UnwinderStats stats; // statistics for performance analysis InterpreterTstateCacheEntry cached_tstates[INTERPRETER_THREAD_CACHE_SIZE]; InterpreterGenerationCacheEntry cached_generations[INTERPRETER_THREAD_CACHE_SIZE]; +#ifdef MS_WINDOWS + int win_use_process_snapshot; + ULONG win_process_buffer_size; + void *win_process_buffer; + SYSTEM_PROCESS_INFORMATION *win_process_info; // Points into win_process_buffer +#endif #ifdef Py_GIL_DISABLED uint32_t tlbc_generation; _Py_hashtable_t *tlbc_cache; diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index d9d7b61633a106..59b82c7653a3fe 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -688,6 +688,10 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self return NULL; } +#ifdef MS_WINDOWS + self->win_process_info = NULL; +#endif + // Iterate over all interpreters uintptr_t current_interpreter = self->interpreter_addr; while (current_interpreter != 0) { @@ -1315,6 +1319,9 @@ RemoteUnwinder_dealloc(PyObject *op) RemoteUnwinderObject *self = RemoteUnwinder_CAST(op); PyTypeObject *tp = Py_TYPE(self); +#ifdef MS_WINDOWS + PyMem_Free(self->win_process_buffer); +#endif #ifdef Py_REMOTE_DEBUG_SUPPORTS_BLOCKING if (self->threads_stopped) { _Py_RemoteDebug_ResumeAllThreads(self, &self->threads_state); diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 66c40d875ab3b3..ae7961dd38fbdb 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -166,6 +166,60 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ +#ifdef MS_WINDOWS +static SYSTEM_PROCESS_INFORMATION * +win32_get_process_info(RemoteUnwinderObject *unwinder) +{ + for (;;) { + ULONG needed; + NTSTATUS status = NtQuerySystemInformation( + SystemProcessInformation, unwinder->win_process_buffer, + unwinder->win_process_buffer_size, &needed); + if (status == STATUS_SUCCESS) { + break; + } + if (status != STATUS_INFO_LENGTH_MISMATCH || + needed <= unwinder->win_process_buffer_size) { + return NULL; + } + void *buffer = PyMem_Realloc(unwinder->win_process_buffer, needed); + if (buffer == NULL) { + return NULL; + } + unwinder->win_process_buffer = buffer; + unwinder->win_process_buffer_size = needed; + } + + SYSTEM_PROCESS_INFORMATION *process = unwinder->win_process_buffer; + while ((ULONG_PTR)process->UniqueProcessId != (ULONG_PTR)unwinder->handle.pid) { + if (process->NextEntryOffset == 0) { + return NULL; + } + process = (SYSTEM_PROCESS_INFORMATION *)((char *)process + process->NextEntryOffset); + } + return process; +} + +static int +get_snapshot_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) +{ + if (unwinder->win_process_info == NULL) { + unwinder->win_process_info = win32_get_process_info(unwinder); + } + SYSTEM_PROCESS_INFORMATION *process = unwinder->win_process_info; + if (process != NULL) { + SYSTEM_THREAD_INFORMATION *threads = (SYSTEM_THREAD_INFORMATION *)(process + 1); + for (ULONG i = 0; i < process->NumberOfThreads; i++) { + if ((ULONG_PTR)threads[i].ClientId.UniqueThread == tid) { + return threads[i].ThreadState == WIN32_THREAD_RUNNING + ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; + } + } + } + return THREAD_STATE_UNKNOWN; +} +#endif + static int get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { #if defined(__APPLE__) && TARGET_OS_OSX @@ -217,6 +271,9 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { return THREAD_STATE_UNKNOWN; } #elif defined(MS_WINDOWS) + if (unwinder->win_use_process_snapshot) { + return get_snapshot_thread_status(unwinder, tid); + } HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid); if (thread == NULL) { return THREAD_STATE_UNKNOWN; @@ -227,6 +284,11 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION, &info, sizeof(info), &size); CloseHandle(thread); + if (result == STATUS_INVALID_INFO_CLASS) { + // WOW64 on Windows Server 2022 does not support this information class. + unwinder->win_use_process_snapshot = 1; + return get_snapshot_thread_status(unwinder, tid); + } if (result != STATUS_SUCCESS || size != sizeof(info) || (ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid || (ULONG_PTR)info.ClientId.UniqueThread != tid)