From c758ce8f40c4ff2e753619b70680c0539b97864c Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 28 Oct 2026 11:24:10 +0800 Subject: [PATCH] fix(runtime): stop Windows identity polling memory leak Reuse the FILETIME structure and cached kernel32 bindings rather than creating a new ctypes pointer type for every identity query. Python before 3.14 retains those types in its global pointer cache, so idle WebUI and gateway polling could accumulate gigabytes. Keep each identity query live and preserve the native HANDLE signatures and handle cleanup. Add a native Windows regression that checks identity stability and a bounded set of pointer types, including on Python 3.14 where ctypes no longer retains the types globally. Fixes #6506 Co-authored-by: snailak <141750781+snailak@users.noreply.github.com> --- nanobot/process_runtime.py | 47 +++++++++++++++++++++-------------- tests/gateway/test_runtime.py | 26 +++++++++++++++++++ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/nanobot/process_runtime.py b/nanobot/process_runtime.py index e94f96205..635ac7908 100644 --- a/nanobot/process_runtime.py +++ b/nanobot/process_runtime.py @@ -717,41 +717,52 @@ def _darwin_process_birth(pid: int) -> tuple[int, int, int] | None: return process_group, started_at_seconds, started_at_microseconds -def _windows_process_identity(pid: int) -> str | None: - if os.name != "nt": - return None +# Python before 3.14 retains each distinct pointer type in a global ctypes cache. +class _FileTime(ctypes.Structure): + _fields_ = [("low", ctypes.c_uint32), ("high", ctypes.c_uint32)] - class FileTime(ctypes.Structure): - _fields_ = [("low", ctypes.c_uint32), ("high", ctypes.c_uint32)] + @property + def value(self) -> int: + return (int(self.high) << 32) | int(self.low) - @property - def value(self) -> int: - return (int(self.high) << 32) | int(self.low) - process_query_limited_information = 0x1000 +_FILE_TIME_PTR = ctypes.POINTER(_FileTime) + + +@lru_cache(maxsize=1) +def _windows_kernel32() -> Any: + """Reuse the native bindings while querying process identity on every call.""" kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD] kernel32.OpenProcess.restype = wintypes.HANDLE kernel32.GetProcessTimes.argtypes = [ wintypes.HANDLE, - ctypes.POINTER(FileTime), - ctypes.POINTER(FileTime), - ctypes.POINTER(FileTime), - ctypes.POINTER(FileTime), + _FILE_TIME_PTR, + _FILE_TIME_PTR, + _FILE_TIME_PTR, + _FILE_TIME_PTR, ] kernel32.GetProcessTimes.restype = wintypes.BOOL kernel32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)] kernel32.GetExitCodeProcess.restype = wintypes.BOOL kernel32.CloseHandle.argtypes = [wintypes.HANDLE] kernel32.CloseHandle.restype = wintypes.BOOL - handle = kernel32.OpenProcess(process_query_limited_information, False, pid) + return kernel32 + + +def _windows_process_identity(pid: int) -> str | None: + if os.name != "nt": + return None + + kernel32 = _windows_kernel32() + handle = kernel32.OpenProcess(0x1000, False, pid) if not handle: return None try: - creation_time = FileTime() - exit_time = FileTime() - kernel_time = FileTime() - user_time = FileTime() + creation_time = _FileTime() + exit_time = _FileTime() + kernel_time = _FileTime() + user_time = _FileTime() ok = kernel32.GetProcessTimes( handle, ctypes.byref(creation_time), diff --git a/tests/gateway/test_runtime.py b/tests/gateway/test_runtime.py index d896d5404..025f796fd 100644 --- a/tests/gateway/test_runtime.py +++ b/tests/gateway/test_runtime.py @@ -942,6 +942,32 @@ def test_managed_background_hands_off_virtualenv_launcher_pid(tmp_path: Path) -> launcher.wait(timeout=3) +@pytest.mark.skipif(os.name != "nt", reason="requires Windows process identity APIs") +def test_windows_identity_polling_reuses_pointer_types(tmp_path, monkeypatch): + import ctypes + + pointer_types = set() + pointer = ctypes.POINTER + + def record_pointer_type(cls): + result = pointer(cls) + pointer_types.add(result) + return result + + monkeypatch.setattr(ctypes, "POINTER", record_pointer_type) + runtime = GatewayRuntime(paths=_paths(tmp_path)) + pid = os.getpid() + identity = runtime.process_identity(pid) + assert isinstance(identity, str) and identity.isdecimal() + warmed_types = pointer_types.copy() + + for _ in range(100): + assert runtime.process_identity(pid) == identity + + # Fresh pointer types remain cached on Python versions before 3.14. + assert pointer_types == warmed_types + + def test_windows_process_probe_never_sends_ctrl_c(monkeypatch): monkeypatch.setattr( "nanobot.process_runtime._windows_process_identity",