Handle native exited-child errors during private cleanup

This commit is contained in:
阿白叔 committed 2026-10-15 11:15:34 +08:00
1 parent 5c0f6a3fd3
commit f26e868bfc
2 files changed
+31 -2

No files matched your search

@@ -290,8 +290,13 @@ class _Win32Process:
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:
except Exception as error:
# pywintypes.error is not an OSError. A child that exited
# between the job snapshot and OpenProcess reports error 87.
code = getattr(error, 'winerror', None)
if code is None and error.args:
code = error.args[0]
if code != 87:
raise
def close(self):
+24
View File
@@ -241,6 +241,30 @@ def test_descendant_handles_are_owned_before_terminating_the_job():
assert ('close', 'child') in events
@pytest.mark.parametrize('error_code', [87, 5])
def test_native_descendant_open_error_only_ignores_already_exited_process(error_code):
# pywintypes.error derives directly from Exception, rather than OSError.
class NativeError(Exception):
pass
error = NativeError(error_code, 'OpenProcess', 'native process open failed')
api, events, _state, *_ = native_api()
api.job.JobObjectBasicProcessIdList = 3
query = api.job.QueryInformationJobObject
api.job.QueryInformationJobObject = lambda job, kind: [43] if kind == 3 else query(job, kind)
def open_child(*_args):
raise error
api.open_process = open_child
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
if error_code == 87:
process._remember_descendants()
assert process._descendant_handles == []
else:
with pytest.raises(NativeError) as caught:
process._remember_descendants()
assert caught.value is error
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)