Harden private desktop process ownership and cleanup
This commit is contained in:
1 parent
2a4e793d70
commit
5c0f6a3fd3
2 files changed
+584
-24
No files matched your search
@@ -7,10 +7,14 @@ desktop, starts a process on that desktop, and closes resources it owns.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from contextlib import ExitStack
|
||||||
import ctypes
|
import ctypes
|
||||||
|
from ctypes import wintypes
|
||||||
|
import math
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import time
|
||||||
from typing import Mapping, Sequence
|
from typing import Mapping, Sequence
|
||||||
|
|
||||||
from .errors import TaskCenterError
|
from .errors import TaskCenterError
|
||||||
@@ -30,52 +34,319 @@ def build_startup_info(desktop_name: str) -> StartupInfo:
|
|||||||
class _Win32Api:
|
class _Win32Api:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
try:
|
try:
|
||||||
import win32con, win32job, win32process, win32service
|
import win32api, win32con, win32event, win32job, win32process, win32service
|
||||||
except ImportError as error:
|
except ImportError as error:
|
||||||
raise TaskCenterError("private_desktop", "pywin32 is required for private desktop supervision") from error
|
raise TaskCenterError("private_desktop", "pywin32 is required for private desktop supervision") from error
|
||||||
self.con, self.job, self.process, self.service = win32con, win32job, win32process, win32service
|
self.con, self.job, self.process, self.service = win32con, win32job, win32process, win32service
|
||||||
|
self.event = win32event
|
||||||
|
self.open_process = win32api.OpenProcess
|
||||||
|
self._pending_cleanup = []
|
||||||
|
|
||||||
def create_desktop(self, name: str):
|
def create_desktop(self, name: str):
|
||||||
return self.service.CreateDesktop(name, 0, self.con.GENERIC_ALL, None)
|
return self.service.CreateDesktop(name, 0, self.con.GENERIC_ALL, None)
|
||||||
|
|
||||||
def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None):
|
def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None,
|
||||||
|
*, stdout=None, stderr=None):
|
||||||
startup = self.process.STARTUPINFO()
|
startup = self.process.STARTUPINFO()
|
||||||
startup.lpDesktop = desktop
|
startup.lpDesktop = desktop
|
||||||
flags = getattr(self.con, "CREATE_NO_WINDOW", 0)
|
flags = self.con.CREATE_NO_WINDOW | self.con.CREATE_SUSPENDED
|
||||||
handle, thread, pid, _ = self.process.CreateProcess(
|
|
||||||
str(command[0]), subprocess.list2cmdline([str(item) for item in command]),
|
|
||||||
None, None, False, flags, env, str(cwd) if cwd else None, startup,
|
|
||||||
)
|
|
||||||
thread.Close()
|
|
||||||
job = self.job.CreateJobObject(None, "")
|
job = self.job.CreateJobObject(None, "")
|
||||||
|
process = None
|
||||||
try:
|
try:
|
||||||
info = self.job.QueryInformationJobObject(job, self.job.JobObjectExtendedLimitInformation)
|
info = self.job.QueryInformationJobObject(job, self.job.JobObjectExtendedLimitInformation)
|
||||||
info["BasicLimitInformation"]["LimitFlags"] |= self.job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
info["BasicLimitInformation"]["LimitFlags"] |= self.job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
||||||
self.job.SetInformationJobObject(job, self.job.JobObjectExtendedLimitInformation, info)
|
self.job.SetInformationJobObject(job, self.job.JobObjectExtendedLimitInformation, info)
|
||||||
|
streams = (None, None)
|
||||||
|
if stdout is not None or stderr is not None:
|
||||||
|
(handle, thread, pid, _), streams = _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr)
|
||||||
|
else:
|
||||||
|
handle, thread, pid, _ = self.process.CreateProcess(
|
||||||
|
str(command[0]), subprocess.list2cmdline([str(item) for item in command]),
|
||||||
|
None, None, False, flags, env, str(cwd) if cwd else None, startup,
|
||||||
|
)
|
||||||
|
process = _Win32Process(self.process, self.job, self.event, handle, job, int(pid), command,
|
||||||
|
open_process=getattr(self, 'open_process', None))
|
||||||
|
process.stdout, process.stderr = streams
|
||||||
|
process.thread = thread
|
||||||
self.job.AssignProcessToJobObject(job, handle)
|
self.job.AssignProcessToJobObject(job, handle)
|
||||||
except Exception:
|
process.job_assigned = True
|
||||||
handle.Close(); job.Close()
|
self.process.ResumeThread(thread)
|
||||||
|
thread.Close()
|
||||||
|
process.thread = None
|
||||||
|
except Exception as error:
|
||||||
|
try:
|
||||||
|
if process is not None:
|
||||||
|
# Assignment can fail before the job owns this suspended process.
|
||||||
|
process.close()
|
||||||
|
else:
|
||||||
|
job.Close()
|
||||||
|
except Exception as cleanup_error:
|
||||||
|
pending = getattr(self, '_pending_cleanup', None)
|
||||||
|
if pending is None:
|
||||||
|
self._pending_cleanup = pending = []
|
||||||
|
pending.append(process if process is not None else job)
|
||||||
|
raise TaskCenterError('private_desktop', f'launch failed: {error}; cleanup failed: {cleanup_error}') from error
|
||||||
raise
|
raise
|
||||||
return _Win32Process(self.process, self.job, handle, job, int(pid))
|
return process
|
||||||
|
|
||||||
|
def close_pending(self):
|
||||||
|
errors, remaining = [], []
|
||||||
|
for resource in getattr(self, '_pending_cleanup', []):
|
||||||
|
try:
|
||||||
|
resource.close() if isinstance(resource, _Win32Process) else resource.Close()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
remaining.append(resource)
|
||||||
|
self._pending_cleanup = remaining
|
||||||
|
_raise_cleanup_errors(errors)
|
||||||
|
|
||||||
|
|
||||||
|
def _raise_cleanup_errors(errors):
|
||||||
|
if errors:
|
||||||
|
raise TaskCenterError('private_desktop_cleanup', '; '.join(str(error) for error in errors))
|
||||||
|
|
||||||
|
|
||||||
|
def _stop_and_wait(process):
|
||||||
|
errors = []
|
||||||
|
running = True
|
||||||
|
try:
|
||||||
|
running = process.poll() is None
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
if running:
|
||||||
|
try:
|
||||||
|
process.terminate()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
try:
|
||||||
|
process.wait(timeout=5.0)
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
return errors
|
||||||
|
|
||||||
|
|
||||||
|
def _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr):
|
||||||
|
"""Use an explicit inheritance list; never inherit unrelated parent handles."""
|
||||||
|
import msvcrt
|
||||||
|
import pywintypes
|
||||||
|
|
||||||
|
class NativeStartup(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
('cb', wintypes.DWORD), ('lpReserved', wintypes.LPWSTR),
|
||||||
|
('lpDesktop', wintypes.LPWSTR), ('lpTitle', wintypes.LPWSTR),
|
||||||
|
('dwX', wintypes.DWORD), ('dwY', wintypes.DWORD),
|
||||||
|
('dwXSize', wintypes.DWORD), ('dwYSize', wintypes.DWORD),
|
||||||
|
('dwXCountChars', wintypes.DWORD), ('dwYCountChars', wintypes.DWORD),
|
||||||
|
('dwFillAttribute', wintypes.DWORD), ('dwFlags', wintypes.DWORD),
|
||||||
|
('wShowWindow', wintypes.WORD), ('cbReserved2', wintypes.WORD),
|
||||||
|
('lpReserved2', ctypes.POINTER(ctypes.c_byte)),
|
||||||
|
('hStdInput', wintypes.HANDLE), ('hStdOutput', wintypes.HANDLE), ('hStdError', wintypes.HANDLE),
|
||||||
|
]
|
||||||
|
class ExtendedStartup(ctypes.Structure):
|
||||||
|
_fields_ = [('StartupInfo', NativeStartup), ('lpAttributeList', ctypes.c_void_p)]
|
||||||
|
class ProcessInformation(ctypes.Structure):
|
||||||
|
_fields_ = [('hProcess', wintypes.HANDLE), ('hThread', wintypes.HANDLE),
|
||||||
|
('dwProcessId', wintypes.DWORD), ('dwThreadId', wintypes.DWORD)]
|
||||||
|
|
||||||
|
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||||
|
initialize = kernel.InitializeProcThreadAttributeList
|
||||||
|
initialize.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(ctypes.c_size_t)]
|
||||||
|
initialize.restype = wintypes.BOOL
|
||||||
|
update = kernel.UpdateProcThreadAttribute
|
||||||
|
update.argtypes = [ctypes.c_void_p, wintypes.DWORD, ctypes.c_size_t, ctypes.c_void_p,
|
||||||
|
ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p]
|
||||||
|
update.restype = wintypes.BOOL
|
||||||
|
delete = kernel.DeleteProcThreadAttributeList
|
||||||
|
delete.argtypes = [ctypes.c_void_p]
|
||||||
|
delete.restype = None
|
||||||
|
create = kernel.CreateProcessW
|
||||||
|
create.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p,
|
||||||
|
wintypes.BOOL, wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR,
|
||||||
|
ctypes.POINTER(ExtendedStartup), ctypes.POINTER(ProcessInformation)]
|
||||||
|
create.restype = wintypes.BOOL
|
||||||
|
readers = [None, None]
|
||||||
|
try:
|
||||||
|
with ExitStack() as children:
|
||||||
|
def output_handle(target, channel):
|
||||||
|
if target == subprocess.PIPE:
|
||||||
|
read_fd, write_fd = os.pipe()
|
||||||
|
readers[channel] = os.fdopen(read_fd, 'rb')
|
||||||
|
writer = children.enter_context(os.fdopen(write_fd, 'wb'))
|
||||||
|
elif target is None or target == subprocess.DEVNULL:
|
||||||
|
writer = children.enter_context(open(os.devnull, 'wb'))
|
||||||
|
else:
|
||||||
|
writer = children.enter_context(os.fdopen(os.dup(target.fileno()), 'wb'))
|
||||||
|
handle = msvcrt.get_osfhandle(writer.fileno())
|
||||||
|
os.set_handle_inheritable(handle, True)
|
||||||
|
return handle
|
||||||
|
|
||||||
|
stdin_file = children.enter_context(open(os.devnull, 'rb'))
|
||||||
|
stdin_handle = msvcrt.get_osfhandle(stdin_file.fileno())
|
||||||
|
os.set_handle_inheritable(stdin_handle, True)
|
||||||
|
stdout_handle = output_handle(stdout, 0)
|
||||||
|
stderr_handle = stdout_handle if stderr == subprocess.STDOUT else output_handle(stderr, 1)
|
||||||
|
handles = list(dict.fromkeys((stdin_handle, stdout_handle, stderr_handle)))
|
||||||
|
inherited = (wintypes.HANDLE * len(handles))(*handles)
|
||||||
|
size = ctypes.c_size_t()
|
||||||
|
initialize(None, 1, 0, ctypes.byref(size))
|
||||||
|
attributes = ctypes.create_string_buffer(size.value)
|
||||||
|
if not initialize(attributes, 1, 0, ctypes.byref(size)):
|
||||||
|
raise ctypes.WinError(ctypes.get_last_error())
|
||||||
|
try:
|
||||||
|
if not update(attributes, 0, 0x20002, inherited, ctypes.sizeof(inherited), None, None):
|
||||||
|
raise ctypes.WinError(ctypes.get_last_error())
|
||||||
|
startup = ExtendedStartup()
|
||||||
|
startup.StartupInfo.cb = ctypes.sizeof(startup)
|
||||||
|
startup.StartupInfo.lpDesktop = desktop
|
||||||
|
startup.StartupInfo.dwFlags = 0x100 # STARTF_USESTDHANDLES
|
||||||
|
startup.StartupInfo.hStdInput = stdin_handle
|
||||||
|
startup.StartupInfo.hStdOutput = stdout_handle
|
||||||
|
startup.StartupInfo.hStdError = stderr_handle
|
||||||
|
startup.lpAttributeList = ctypes.addressof(attributes)
|
||||||
|
information = ProcessInformation()
|
||||||
|
environment = None if env is None else ctypes.create_unicode_buffer(
|
||||||
|
'\0'.join(f'{key}={value}' for key, value in sorted(env.items(), key=lambda item: item[0].upper())) + '\0\0')
|
||||||
|
command_line = ctypes.create_unicode_buffer(subprocess.list2cmdline([str(item) for item in command]))
|
||||||
|
if not create(str(command[0]), command_line, None, None, True,
|
||||||
|
flags | 0x80000 | 0x400, environment, str(cwd) if cwd else None,
|
||||||
|
ctypes.byref(startup), ctypes.byref(information)):
|
||||||
|
raise ctypes.WinError(ctypes.get_last_error())
|
||||||
|
result = (pywintypes.HANDLE(information.hProcess), pywintypes.HANDLE(information.hThread),
|
||||||
|
information.dwProcessId, information.dwThreadId)
|
||||||
|
finally:
|
||||||
|
delete(attributes)
|
||||||
|
return result, readers
|
||||||
|
except BaseException:
|
||||||
|
for reader in readers:
|
||||||
|
if reader is not None:
|
||||||
|
reader.close()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
class _Win32Process:
|
class _Win32Process:
|
||||||
def __init__(self, process_api, job_api, handle, job, pid: int):
|
def __init__(self, process_api, job_api, event_api, handle, job, pid: int, args, *, open_process=None):
|
||||||
self._process_api, self._job_api = process_api, job_api
|
self._process_api, self._job_api = process_api, job_api
|
||||||
|
self._event_api = event_api
|
||||||
|
self._open_process = open_process
|
||||||
|
self._descendant_handles = []
|
||||||
|
self._descendant_pids = set()
|
||||||
self.handle, self.job, self.pid = handle, job, pid
|
self.handle, self.job, self.pid = handle, job, pid
|
||||||
|
self.args = args
|
||||||
|
self.thread = None
|
||||||
|
self.job_assigned = False
|
||||||
|
self.returncode = None
|
||||||
|
self.stdout = self.stderr = None
|
||||||
|
|
||||||
def poll(self):
|
def poll(self):
|
||||||
code = self._process_api.GetExitCodeProcess(self.handle)
|
if self.returncode is not None or self.handle is None:
|
||||||
return None if code == 259 else code
|
return self.returncode
|
||||||
|
status = self._event_api.WaitForSingleObject(self.handle, 0)
|
||||||
|
if status == 258: # WAIT_TIMEOUT: 259 is also a valid *completed* exit code.
|
||||||
|
return None
|
||||||
|
if status != 0:
|
||||||
|
raise OSError(f'process wait failed with status {status}')
|
||||||
|
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||||
|
return self.returncode
|
||||||
|
|
||||||
|
def wait(self, timeout=None):
|
||||||
|
if self.returncode is not None:
|
||||||
|
return self.returncode
|
||||||
|
if self.handle is None:
|
||||||
|
raise OSError('owned process handle is closed before exit was confirmed')
|
||||||
|
milliseconds = 0xFFFFFFFF if timeout is None else min(max(math.ceil(timeout * 1000), 0), 0xFFFFFFFE)
|
||||||
|
status = self._event_api.WaitForSingleObject(self.handle, milliseconds)
|
||||||
|
if status == 258:
|
||||||
|
raise subprocess.TimeoutExpired(self.args, timeout)
|
||||||
|
if status != 0:
|
||||||
|
raise OSError(f'process wait failed with status {status}')
|
||||||
|
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||||
|
return self.returncode
|
||||||
|
|
||||||
def terminate(self):
|
def terminate(self):
|
||||||
|
if self.handle is None:
|
||||||
|
return
|
||||||
|
if not self.job_assigned:
|
||||||
|
self._process_api.TerminateProcess(self.handle, 1)
|
||||||
|
return
|
||||||
|
errors = []
|
||||||
|
try:
|
||||||
|
self._remember_descendants()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
try:
|
try:
|
||||||
self._job_api.TerminateJobObject(self.job, 1)
|
self._job_api.TerminateJobObject(self.job, 1)
|
||||||
except Exception:
|
except Exception:
|
||||||
self._process_api.TerminateProcess(self.handle, 1)
|
self._process_api.TerminateProcess(self.handle, 1)
|
||||||
|
_raise_cleanup_errors(errors)
|
||||||
|
|
||||||
|
kill = terminate
|
||||||
|
|
||||||
|
def _remember_descendants(self):
|
||||||
|
if self._open_process is None or self.job is None:
|
||||||
|
return
|
||||||
|
for pid in self._job_api.QueryInformationJobObject(self.job, self._job_api.JobObjectBasicProcessIdList):
|
||||||
|
if pid == self.pid or pid in self._descendant_pids:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
self._descendant_handles.append(self._open_process(0x100000, False, pid))
|
||||||
|
self._descendant_pids.add(pid)
|
||||||
|
except OSError as error:
|
||||||
|
if getattr(error, 'winerror', error.args[0]) != 87:
|
||||||
|
raise
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.handle.Close(); self.job.Close()
|
errors = []
|
||||||
|
if self.handle is not None:
|
||||||
|
errors.extend(_stop_and_wait(self))
|
||||||
|
deadline = time.monotonic() + 5.0
|
||||||
|
if self.job is not None and self.job_assigned:
|
||||||
|
try:
|
||||||
|
# A completed root can leave grandchildren holding the desktop.
|
||||||
|
self._remember_descendants()
|
||||||
|
self._job_api.TerminateJobObject(self.job, 1)
|
||||||
|
while self._job_api.QueryInformationJobObject(
|
||||||
|
self.job, self._job_api.JobObjectBasicAccountingInformation,
|
||||||
|
)['ActiveProcesses']:
|
||||||
|
if time.monotonic() >= deadline:
|
||||||
|
raise TimeoutError('owned job descendants did not exit within 5 seconds')
|
||||||
|
time.sleep(0.01)
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
retained = []
|
||||||
|
for descendant in self._descendant_handles:
|
||||||
|
try:
|
||||||
|
# Job accounting can reach zero before a terminated process is signaled.
|
||||||
|
remaining = max(0, math.ceil((deadline - time.monotonic()) * 1000))
|
||||||
|
if self._event_api.WaitForSingleObject(descendant, remaining) != 0:
|
||||||
|
raise TimeoutError('owned job descendant exit was not confirmed within 5 seconds')
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
retained.append(descendant)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
descendant.Close()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
retained.append(descendant)
|
||||||
|
self._descendant_handles = retained
|
||||||
|
for name in ('job', 'thread', 'handle'):
|
||||||
|
handle = getattr(self, name)
|
||||||
|
if name == 'handle' and self.returncode is None:
|
||||||
|
# Keep the only termination/wait capability until cleanup can retry.
|
||||||
|
continue
|
||||||
|
if handle is not None:
|
||||||
|
try:
|
||||||
|
handle.Close()
|
||||||
|
setattr(self, name, None)
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
for stream in (self.stdout, self.stderr):
|
||||||
|
if stream is not None:
|
||||||
|
try:
|
||||||
|
stream.close()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
_raise_cleanup_errors(errors)
|
||||||
|
|
||||||
|
|
||||||
class PrivateDesktop:
|
class PrivateDesktop:
|
||||||
@@ -111,6 +382,8 @@ class PrivateDesktop:
|
|||||||
*,
|
*,
|
||||||
cwd: Path | None = None,
|
cwd: Path | None = None,
|
||||||
env: Mapping[str, str] | None = None,
|
env: Mapping[str, str] | None = None,
|
||||||
|
stdout=None,
|
||||||
|
stderr=None,
|
||||||
) -> subprocess.Popen[str]:
|
) -> subprocess.Popen[str]:
|
||||||
if not self.enabled or self._handle is None:
|
if not self.enabled or self._handle is None:
|
||||||
raise TaskCenterError("private_desktop", "process launch requires an owned private desktop")
|
raise TaskCenterError("private_desktop", "process launch requires an owned private desktop")
|
||||||
@@ -118,19 +391,36 @@ class PrivateDesktop:
|
|||||||
raise TaskCenterError("private_desktop", "private desktop command is empty")
|
raise TaskCenterError("private_desktop", "private desktop command is empty")
|
||||||
if self._api is None:
|
if self._api is None:
|
||||||
raise TaskCenterError("private_desktop", "private desktop process API is unavailable")
|
raise TaskCenterError("private_desktop", "private desktop process API is unavailable")
|
||||||
process = self._api.create_process(command, build_startup_info(self.name).lpDesktop, cwd, env)
|
options = {} if stdout is None and stderr is None else {'stdout': stdout, 'stderr': stderr}
|
||||||
|
process = self._api.create_process(command, build_startup_info(self.name).lpDesktop, cwd, env, **options)
|
||||||
self._processes.append(process)
|
self._processes.append(process)
|
||||||
return process
|
return process
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
|
errors, remaining = [], []
|
||||||
for process in reversed(self._processes):
|
for process in reversed(self._processes):
|
||||||
if process.poll() is None:
|
process_errors = _stop_and_wait(process)
|
||||||
process.terminate()
|
errors.extend(process_errors)
|
||||||
close = getattr(process, "close", None)
|
failed = bool(process_errors)
|
||||||
if close:
|
try:
|
||||||
close()
|
process.close()
|
||||||
self._processes.clear()
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
failed = True
|
||||||
|
if failed:
|
||||||
|
remaining.append(process)
|
||||||
|
self._processes = list(reversed(remaining))
|
||||||
|
close_pending = getattr(self._api, 'close_pending', None)
|
||||||
|
if close_pending:
|
||||||
|
try:
|
||||||
|
close_pending()
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
close_desktop = getattr(self._handle, "CloseDesktop", None)
|
close_desktop = getattr(self._handle, "CloseDesktop", None)
|
||||||
if close_desktop:
|
if close_desktop:
|
||||||
close_desktop()
|
try:
|
||||||
self._handle = None
|
close_desktop()
|
||||||
|
self._handle = None
|
||||||
|
except Exception as error:
|
||||||
|
errors.append(error)
|
||||||
|
_raise_cleanup_errors(errors)
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from uuid import uuid4
|
||||||
|
from types import SimpleNamespace
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||||
from dx_productivity.trial_task_center.isolated_desktop import PrivateDesktop, build_startup_info
|
from dx_productivity.trial_task_center.isolated_desktop import PrivateDesktop, build_startup_info
|
||||||
|
from dx_productivity.trial_task_center.isolated_desktop import _Win32Api, _Win32Process
|
||||||
|
|
||||||
|
|
||||||
def test_startup_info_always_targets_private_desktop():
|
def test_startup_info_always_targets_private_desktop():
|
||||||
@@ -16,3 +22,267 @@ def test_private_desktop_never_falls_back_to_input_desktop(monkeypatch):
|
|||||||
monkeypatch.setattr(desktop, '_handle', None)
|
monkeypatch.setattr(desktop, '_handle', None)
|
||||||
with pytest.raises(TaskCenterError, match='private desktop'):
|
with pytest.raises(TaskCenterError, match='private desktop'):
|
||||||
desktop.start_process(('cad.exe',))
|
desktop.start_process(('cad.exe',))
|
||||||
|
|
||||||
|
|
||||||
|
class Handle:
|
||||||
|
def __init__(self, name, events, fail_once=False):
|
||||||
|
self.name, self.events, self.fail_once = name, events, fail_once
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
|
def Close(self):
|
||||||
|
self.events.append(('close', self.name))
|
||||||
|
if self.fail_once:
|
||||||
|
self.fail_once = False
|
||||||
|
raise OSError('close failed')
|
||||||
|
self.closed = True
|
||||||
|
|
||||||
|
|
||||||
|
def native_api(*, assign_error=False):
|
||||||
|
events = []
|
||||||
|
process_handle = Handle('process', events)
|
||||||
|
thread = Handle('thread', events)
|
||||||
|
job = Handle('job', events)
|
||||||
|
state = {'running': True, 'code': 259}
|
||||||
|
def create(*args):
|
||||||
|
events.append(('create', args[5]))
|
||||||
|
return process_handle, thread, 42, 43
|
||||||
|
def assign(*_args):
|
||||||
|
events.append(('assign',))
|
||||||
|
if assign_error:
|
||||||
|
raise OSError('assign failed')
|
||||||
|
def terminate(*_args):
|
||||||
|
events.append(('terminate',))
|
||||||
|
state['running'] = False
|
||||||
|
def wait(_handle, timeout):
|
||||||
|
events.append(('wait', timeout))
|
||||||
|
return 258 if state['running'] else 0
|
||||||
|
api = _Win32Api.__new__(_Win32Api)
|
||||||
|
api.con = SimpleNamespace(CREATE_NO_WINDOW=0x8000000, CREATE_SUSPENDED=4)
|
||||||
|
api.process = SimpleNamespace(
|
||||||
|
STARTUPINFO=lambda: SimpleNamespace(), CreateProcess=create,
|
||||||
|
ResumeThread=lambda _thread: events.append(('resume',)),
|
||||||
|
GetExitCodeProcess=lambda _handle: state['code'], TerminateProcess=terminate,
|
||||||
|
)
|
||||||
|
api.job = SimpleNamespace(
|
||||||
|
CreateJobObject=lambda *_args: job,
|
||||||
|
QueryInformationJobObject=lambda _job, kind: (
|
||||||
|
{'BasicLimitInformation': {'LimitFlags': 0}} if kind == 9 else {'ActiveProcesses': int(state['running'])}),
|
||||||
|
SetInformationJobObject=lambda *_args: events.append(('limits',)),
|
||||||
|
AssignProcessToJobObject=assign, TerminateJobObject=terminate,
|
||||||
|
JobObjectExtendedLimitInformation=9, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE=0x2000,
|
||||||
|
JobObjectBasicAccountingInformation=1,
|
||||||
|
)
|
||||||
|
api.event = SimpleNamespace(WaitForSingleObject=wait)
|
||||||
|
return api, events, state, process_handle, job
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_is_suspended_until_job_assignment():
|
||||||
|
api, events, *_ = native_api()
|
||||||
|
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
assert process.pid == 42
|
||||||
|
assert ('create', 0x8000004) in events
|
||||||
|
assert events.index(('assign',)) < events.index(('resume',))
|
||||||
|
|
||||||
|
|
||||||
|
def test_failed_job_assignment_terminates_and_reaps_owned_process():
|
||||||
|
api, events, state, process_handle, job = native_api(assign_error=True)
|
||||||
|
with pytest.raises(Exception, match='assign failed'):
|
||||||
|
api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
assert not state['running']
|
||||||
|
assert events.index(('wait', 0)) < events.index(('close', 'process'))
|
||||||
|
assert process_handle.closed and job.closed
|
||||||
|
assert ('resume',) not in events
|
||||||
|
|
||||||
|
|
||||||
|
def test_wait_uses_signal_and_accepts_real_exit_code_259():
|
||||||
|
api, events, state, *_ = native_api()
|
||||||
|
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
with pytest.raises(subprocess.TimeoutExpired):
|
||||||
|
process.wait(timeout=0.025)
|
||||||
|
assert ('wait', 25) in events
|
||||||
|
assert process.poll() is None
|
||||||
|
state['running'] = False
|
||||||
|
assert process.poll() == 259
|
||||||
|
assert process.wait(timeout=0) == 259
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_close_attempts_all_handles_and_retries_failed_handle():
|
||||||
|
api, events, state, process_handle, job = native_api()
|
||||||
|
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
process_handle.fail_once = True
|
||||||
|
with pytest.raises(TaskCenterError, match='close failed'):
|
||||||
|
process.close()
|
||||||
|
assert job.closed
|
||||||
|
assert not state['running']
|
||||||
|
process.close()
|
||||||
|
assert process_handle.closed
|
||||||
|
process.close()
|
||||||
|
assert events.count(('close', 'job')) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_desktop_cleanup_continues_after_process_error_and_retries():
|
||||||
|
events = []
|
||||||
|
class OwnedProcess:
|
||||||
|
def __init__(self, failing=False):
|
||||||
|
self.failing = failing
|
||||||
|
def poll(self):
|
||||||
|
return None
|
||||||
|
def terminate(self):
|
||||||
|
events.append('terminate')
|
||||||
|
def wait(self, timeout):
|
||||||
|
events.append(('wait', timeout))
|
||||||
|
def close(self):
|
||||||
|
events.append('process-close')
|
||||||
|
if self.failing:
|
||||||
|
self.failing = False
|
||||||
|
raise OSError('process close failed')
|
||||||
|
class DesktopHandle:
|
||||||
|
def CloseDesktop(self):
|
||||||
|
events.append('desktop-close')
|
||||||
|
desktop = PrivateDesktop('private', enabled=True)
|
||||||
|
desktop._handle = DesktopHandle()
|
||||||
|
desktop._processes = [OwnedProcess(), OwnedProcess(failing=True)]
|
||||||
|
with pytest.raises(TaskCenterError, match='process close failed'):
|
||||||
|
desktop.close()
|
||||||
|
assert events.count('process-close') == 2
|
||||||
|
assert events[-1] == 'desktop-close'
|
||||||
|
assert ('wait', 5.0) in events
|
||||||
|
desktop.close()
|
||||||
|
assert events.count('process-close') == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_desktop_close_reports_bounded_process_timeout_and_closes_handles():
|
||||||
|
events = []
|
||||||
|
process = SimpleNamespace(
|
||||||
|
poll=lambda: None, terminate=lambda: events.append('terminate'),
|
||||||
|
wait=lambda timeout: (_ for _ in ()).throw(subprocess.TimeoutExpired('owned', timeout)),
|
||||||
|
close=lambda: events.append('process-close'),
|
||||||
|
)
|
||||||
|
desktop = PrivateDesktop('private', enabled=True)
|
||||||
|
desktop._handle = SimpleNamespace(CloseDesktop=lambda: events.append('desktop-close'))
|
||||||
|
desktop._processes = [process]
|
||||||
|
with pytest.raises(TaskCenterError, match='timed out'):
|
||||||
|
desktop.close()
|
||||||
|
assert events == ['terminate', 'process-close', 'desktop-close']
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != 'nt', reason='native Windows desktop/process boundary')
|
||||||
|
def test_native_private_process_has_captured_output_and_real_exit_259():
|
||||||
|
with PrivateDesktop('dx-test-' + uuid4().hex, enabled=True) as desktop:
|
||||||
|
process = desktop.start_process(
|
||||||
|
(sys.executable, '-c', "import sys,os; print('private-out',flush=True); print('private-err',file=sys.stderr,flush=True); os._exit(259)"),
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
|
)
|
||||||
|
assert process.stdout.readline() == b'private-out\r\n'
|
||||||
|
assert process.stderr.readline() == b'private-err\r\n'
|
||||||
|
assert process.wait(timeout=5) == 259
|
||||||
|
assert process.poll() == 259
|
||||||
|
|
||||||
|
|
||||||
|
def test_failed_rollback_termination_still_releases_owned_handles():
|
||||||
|
api, events, state, process_handle, job = native_api(assign_error=True)
|
||||||
|
api.process.TerminateProcess = lambda *_args: (_ for _ in ()).throw(OSError('terminate failed'))
|
||||||
|
with pytest.raises(Exception, match='assign failed'):
|
||||||
|
api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
assert job.closed
|
||||||
|
assert ('close', 'thread') in events
|
||||||
|
# A process outside the job still needs its handle for a cleanup retry.
|
||||||
|
assert not process_handle.closed
|
||||||
|
def terminate(*_args):
|
||||||
|
state['running'] = False
|
||||||
|
api.process.TerminateProcess = terminate
|
||||||
|
api.close_pending()
|
||||||
|
assert process_handle.closed
|
||||||
|
|
||||||
|
|
||||||
|
def test_poll_failure_does_not_skip_termination_or_bounded_wait():
|
||||||
|
events = []
|
||||||
|
def failed_poll():
|
||||||
|
raise OSError('poll failed')
|
||||||
|
process = SimpleNamespace(
|
||||||
|
poll=failed_poll, terminate=lambda: events.append('terminate'),
|
||||||
|
wait=lambda timeout: events.append(('wait', timeout)),
|
||||||
|
close=lambda: events.append('close'),
|
||||||
|
)
|
||||||
|
desktop = PrivateDesktop('private', enabled=True)
|
||||||
|
desktop._processes = [process]
|
||||||
|
with pytest.raises(TaskCenterError, match='poll failed'):
|
||||||
|
desktop.close()
|
||||||
|
assert events == ['terminate', ('wait', 5.0), 'close']
|
||||||
|
|
||||||
|
|
||||||
|
def test_desktop_handle_close_failure_is_retried():
|
||||||
|
events = []
|
||||||
|
handle = Handle('desktop', events, fail_once=True)
|
||||||
|
handle.CloseDesktop = handle.Close
|
||||||
|
desktop = PrivateDesktop('private', enabled=True)
|
||||||
|
desktop._handle = handle
|
||||||
|
with pytest.raises(TaskCenterError, match='close failed'):
|
||||||
|
desktop.close()
|
||||||
|
assert desktop._handle is handle
|
||||||
|
desktop.close()
|
||||||
|
assert handle.closed and desktop._handle is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_descendant_handles_are_owned_before_terminating_the_job():
|
||||||
|
api, events, _state, *_ = native_api()
|
||||||
|
api.job.JobObjectBasicProcessIdList = 3
|
||||||
|
query = api.job.QueryInformationJobObject
|
||||||
|
api.job.QueryInformationJobObject = lambda job, kind: [42, 43] if kind == 3 else query(job, kind)
|
||||||
|
def open_child(_access, _inherit, pid):
|
||||||
|
events.append(('open-child', pid))
|
||||||
|
return Handle('child', events)
|
||||||
|
api.open_process = open_child
|
||||||
|
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
desktop = PrivateDesktop('private', enabled=True)
|
||||||
|
desktop._processes = [process]
|
||||||
|
desktop.close()
|
||||||
|
assert events.index(('open-child', 43)) < events.index(('terminate',))
|
||||||
|
assert ('close', 'child') in events
|
||||||
|
|
||||||
|
|
||||||
|
def test_unconfirmed_descendant_exit_retains_handle_for_cleanup_retry():
|
||||||
|
api, events, state, *_ = native_api()
|
||||||
|
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
|
||||||
|
child = Handle('child', events)
|
||||||
|
process._descendant_handles = [child]
|
||||||
|
original_wait = api.event.WaitForSingleObject
|
||||||
|
pending = {'child': True}
|
||||||
|
api.event.WaitForSingleObject = lambda handle, timeout: (
|
||||||
|
258 if handle is child and pending['child'] else original_wait(handle, timeout))
|
||||||
|
with pytest.raises(TaskCenterError, match='descendant exit'):
|
||||||
|
process.close()
|
||||||
|
assert not child.closed
|
||||||
|
pending['child'] = False
|
||||||
|
process.close()
|
||||||
|
assert child.closed
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != 'nt', reason='native Windows job descendant cleanup')
|
||||||
|
@pytest.mark.parametrize('parent_exits', [True, False])
|
||||||
|
def test_native_private_job_kills_descendant_after_root_exit(parent_exits):
|
||||||
|
import win32api
|
||||||
|
import win32event
|
||||||
|
import win32service
|
||||||
|
import pywintypes
|
||||||
|
|
||||||
|
name = 'dx-test-' + uuid4().hex
|
||||||
|
child_handle = None
|
||||||
|
try:
|
||||||
|
with PrivateDesktop(name, enabled=True) as desktop:
|
||||||
|
process = desktop.start_process((
|
||||||
|
sys.executable, '-c',
|
||||||
|
"import subprocess,sys,time; p=subprocess.Popen([sys.executable,'-c','import time;time.sleep(60)']); print(p.pid,flush=True)"
|
||||||
|
+ ('' if parent_exits else '; time.sleep(60)'),
|
||||||
|
), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
child_pid = int(process.stdout.readline())
|
||||||
|
child_handle = win32api.OpenProcess(0x100000, False, child_pid)
|
||||||
|
if parent_exits:
|
||||||
|
assert process.wait(timeout=5) == 0
|
||||||
|
assert win32event.WaitForSingleObject(child_handle, 0) == 0
|
||||||
|
with pytest.raises(pywintypes.error):
|
||||||
|
unexpected = win32service.OpenDesktop(name, 0, False, 1)
|
||||||
|
unexpected.CloseDesktop()
|
||||||
|
finally:
|
||||||
|
if child_handle is not None:
|
||||||
|
child_handle.Close()
|
||||||
Reference in new issue
Block a user