From 76cc927de7da0d9184d4e1d2b150f348342eedec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 19:12:41 +0100 Subject: [PATCH 1/7] fallback --- Lib/test/test_external_inspection.py | 17 +++++ Modules/_remote_debugging/_remote_debugging.h | 10 +++ Modules/_remote_debugging/module.c | 10 +++ Modules/_remote_debugging/threads.c | 63 +++++++++++++++++++ 4 files changed, 100 insertions(+) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 78c66c0e0fc1de..c17ffb96369638 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2980,6 +2980,23 @@ def test_unsupported_platform_error(self): @requires_remote_subprocess_debugging() class TestDetectionOfThreadStatus(RemoteInspectionTestBase): + @unittest.skipUnless(sys.platform == "win32", "Windows thread state sampling") + 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/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 532fed28741738..117ea91a4f48f6 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -371,6 +371,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; + void *win_process_buffer; + ULONG win_process_buffer_size; + 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; @@ -658,6 +664,10 @@ extern int find_running_frame( uintptr_t *frame ); +#ifdef MS_WINDOWS +extern SYSTEM_PROCESS_INFORMATION *win32_get_process_info(RemoteUnwinderObject *unwinder); +#endif + extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index d9d7b61633a106..b185d44bb1882d 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -688,6 +688,13 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self return NULL; } +#ifdef MS_WINDOWS + if (self->win_use_process_snapshot && + (self->mode == PROFILING_MODE_CPU || self->mode == PROFILING_MODE_ALL)) { + self->win_process_info = win32_get_process_info(self); + } +#endif + // Iterate over all interpreters uintptr_t current_interpreter = self->interpreter_addr; while (current_interpreter != 0) { @@ -1315,6 +1322,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..91b47438062ce7 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -19,6 +19,9 @@ #endif #ifdef MS_WINDOWS +#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L) +#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) + enum { /* ThreadSystemThreadInformation is not named in the Windows SDK. */ WIN32_THREAD_SYSTEM_INFORMATION = 40, @@ -166,6 +169,57 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ +#ifdef MS_WINDOWS +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) +{ + 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,12 @@ 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; + unwinder->win_process_info = win32_get_process_info(unwinder); + 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) From 2436d54664f2ffb7fb035a6e030be0351566c729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 20:10:08 +0100 Subject: [PATCH 2/7] simpler --- Modules/_remote_debugging/_remote_debugging.h | 10 --- Modules/_remote_debugging/module.c | 10 --- Modules/_remote_debugging/threads.c | 76 ++++--------------- 3 files changed, 13 insertions(+), 83 deletions(-) diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 117ea91a4f48f6..532fed28741738 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -371,12 +371,6 @@ 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; - void *win_process_buffer; - ULONG win_process_buffer_size; - 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; @@ -664,10 +658,6 @@ extern int find_running_frame( uintptr_t *frame ); -#ifdef MS_WINDOWS -extern SYSTEM_PROCESS_INFORMATION *win32_get_process_info(RemoteUnwinderObject *unwinder); -#endif - extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index b185d44bb1882d..d9d7b61633a106 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -688,13 +688,6 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self return NULL; } -#ifdef MS_WINDOWS - if (self->win_use_process_snapshot && - (self->mode == PROFILING_MODE_CPU || self->mode == PROFILING_MODE_ALL)) { - self->win_process_info = win32_get_process_info(self); - } -#endif - // Iterate over all interpreters uintptr_t current_interpreter = self->interpreter_addr; while (current_interpreter != 0) { @@ -1322,9 +1315,6 @@ 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 91b47438062ce7..f59ce205d8aa6b 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -19,9 +19,6 @@ #endif #ifdef MS_WINDOWS -#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L) -#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) - enum { /* ThreadSystemThreadInformation is not named in the Windows SDK. */ WIN32_THREAD_SYSTEM_INFORMATION = 40, @@ -169,57 +166,6 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ -#ifdef MS_WINDOWS -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) -{ - 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 @@ -271,9 +217,6 @@ 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; @@ -283,13 +226,20 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { NTSTATUS result = NtQueryInformationThread( 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; - unwinder->win_process_info = win32_get_process_info(unwinder); - return get_snapshot_thread_status(unwinder, tid); + if (result == (NTSTATUS)0xC0000003L) { // STATUS_INVALID_INFO_CLASS + // Some versions of WOW64 do not support ThreadSystemThreadInformation. + // Check whether the thread ran while we verified its process ID. + ULONG64 before, after; + int valid = QueryThreadCycleTime(thread, &before) && + GetProcessIdOfThread(thread) == (DWORD)unwinder->handle.pid && + QueryThreadCycleTime(thread, &after); + CloseHandle(thread); + if (!valid) { + return THREAD_STATE_UNKNOWN; + } + return after != before ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; } + CloseHandle(thread); if (result != STATUS_SUCCESS || size != sizeof(info) || (ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid || (ULONG_PTR)info.ClientId.UniqueThread != tid) From 96a65b545a94fda0fe43e824852aa175f33aadd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 20:18:33 +0100 Subject: [PATCH 3/7] Revert "simpler" This reverts commit 2436d54664f2ffb7fb035a6e030be0351566c729. --- Modules/_remote_debugging/_remote_debugging.h | 10 +++ Modules/_remote_debugging/module.c | 10 +++ Modules/_remote_debugging/threads.c | 76 +++++++++++++++---- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 532fed28741738..117ea91a4f48f6 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -371,6 +371,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; + void *win_process_buffer; + ULONG win_process_buffer_size; + 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; @@ -658,6 +664,10 @@ extern int find_running_frame( uintptr_t *frame ); +#ifdef MS_WINDOWS +extern SYSTEM_PROCESS_INFORMATION *win32_get_process_info(RemoteUnwinderObject *unwinder); +#endif + extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index d9d7b61633a106..b185d44bb1882d 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -688,6 +688,13 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self return NULL; } +#ifdef MS_WINDOWS + if (self->win_use_process_snapshot && + (self->mode == PROFILING_MODE_CPU || self->mode == PROFILING_MODE_ALL)) { + self->win_process_info = win32_get_process_info(self); + } +#endif + // Iterate over all interpreters uintptr_t current_interpreter = self->interpreter_addr; while (current_interpreter != 0) { @@ -1315,6 +1322,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 f59ce205d8aa6b..91b47438062ce7 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -19,6 +19,9 @@ #endif #ifdef MS_WINDOWS +#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L) +#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) + enum { /* ThreadSystemThreadInformation is not named in the Windows SDK. */ WIN32_THREAD_SYSTEM_INFORMATION = 40, @@ -166,6 +169,57 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ +#ifdef MS_WINDOWS +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) +{ + 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; @@ -226,20 +283,13 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { NTSTATUS result = NtQueryInformationThread( thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION, &info, sizeof(info), &size); - if (result == (NTSTATUS)0xC0000003L) { // STATUS_INVALID_INFO_CLASS - // Some versions of WOW64 do not support ThreadSystemThreadInformation. - // Check whether the thread ran while we verified its process ID. - ULONG64 before, after; - int valid = QueryThreadCycleTime(thread, &before) && - GetProcessIdOfThread(thread) == (DWORD)unwinder->handle.pid && - QueryThreadCycleTime(thread, &after); - CloseHandle(thread); - if (!valid) { - return THREAD_STATE_UNKNOWN; - } - return after != before ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; - } 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; + unwinder->win_process_info = win32_get_process_info(unwinder); + 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) From 53046a55b3391f18ccaf60b757c6f237224216ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 20:50:44 +0100 Subject: [PATCH 4/7] news --- .../next/Library/2026-12-03-20-50-37.gh-issue-138122.NqOgly.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-12-03-20-50-37.gh-issue-138122.NqOgly.rst 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. From 5c978cc444b43966be1a4b48bdae95b1488cb569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 20:53:27 +0100 Subject: [PATCH 5/7] up --- Modules/_remote_debugging/_remote_debugging.h | 6 ++---- Modules/_remote_debugging/module.c | 5 +---- Modules/_remote_debugging/threads.c | 9 ++++----- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 117ea91a4f48f6..0a90108479c2b3 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 @@ -664,10 +666,6 @@ extern int find_running_frame( uintptr_t *frame ); -#ifdef MS_WINDOWS -extern SYSTEM_PROCESS_INFORMATION *win32_get_process_info(RemoteUnwinderObject *unwinder); -#endif - extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index b185d44bb1882d..59b82c7653a3fe 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -689,10 +689,7 @@ _remote_debugging_RemoteUnwinder_get_stack_trace_impl(RemoteUnwinderObject *self } #ifdef MS_WINDOWS - if (self->win_use_process_snapshot && - (self->mode == PROFILING_MODE_CPU || self->mode == PROFILING_MODE_ALL)) { - self->win_process_info = win32_get_process_info(self); - } + self->win_process_info = NULL; #endif // Iterate over all interpreters diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 91b47438062ce7..ae7961dd38fbdb 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -19,9 +19,6 @@ #endif #ifdef MS_WINDOWS -#define STATUS_INVALID_INFO_CLASS ((NTSTATUS)0xC0000003L) -#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) - enum { /* ThreadSystemThreadInformation is not named in the Windows SDK. */ WIN32_THREAD_SYSTEM_INFORMATION = 40, @@ -170,7 +167,7 @@ find_running_frame( * ============================================================================ */ #ifdef MS_WINDOWS -SYSTEM_PROCESS_INFORMATION * +static SYSTEM_PROCESS_INFORMATION * win32_get_process_info(RemoteUnwinderObject *unwinder) { for (;;) { @@ -206,6 +203,9 @@ win32_get_process_info(RemoteUnwinderObject *unwinder) 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); @@ -287,7 +287,6 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { if (result == STATUS_INVALID_INFO_CLASS) { // WOW64 on Windows Server 2022 does not support this information class. unwinder->win_use_process_snapshot = 1; - unwinder->win_process_info = win32_get_process_info(unwinder); return get_snapshot_thread_status(unwinder, tid); } if (result != STATUS_SUCCESS || size != sizeof(info) || From 5bf9f91d058bf81e2ea834e7cc6c3df750be62ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 20:53:34 +0100 Subject: [PATCH 6/7] follow the convention --- Lib/test/test_external_inspection.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index c17ffb96369638..b084b2154ccd2f 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2980,7 +2980,13 @@ def test_unsupported_platform_error(self): @requires_remote_subprocess_debugging() class TestDetectionOfThreadStatus(RemoteInspectionTestBase): - @unittest.skipUnless(sys.platform == "win32", "Windows thread state sampling") + @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) From de28f9ab8ac201e4ff8bc02b6e8fde5cc9fc0d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Thu, 3 Dec 2026 21:02:30 +0100 Subject: [PATCH 7/7] 24B instead of 32B.... --- Modules/_remote_debugging/_remote_debugging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 0a90108479c2b3..82259d6a42d24f 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -375,8 +375,8 @@ typedef struct { InterpreterGenerationCacheEntry cached_generations[INTERPRETER_THREAD_CACHE_SIZE]; #ifdef MS_WINDOWS int win_use_process_snapshot; - void *win_process_buffer; 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