Skip to content

Commit dfd75df

Browse files
[3.12] gh-58689: Fix os.kill() error handling on Windows (GH-128932) (#128938)
gh-58689: Fix os.kill() error handling on Windows (GH-128932) (cherry picked from commit 939df0f) Co-authored-by: Victor Stinner <[email protected]>
1 parent 1c85f1b commit dfd75df

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Modules/posixmodule.c

+9-18
Original file line numberDiff line numberDiff line change
@@ -8981,42 +8981,33 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
89818981

89828982
Py_RETURN_NONE;
89838983
#else /* !MS_WINDOWS */
8984-
PyObject *result;
89858984
DWORD sig = (DWORD)signal;
8986-
DWORD err;
8987-
HANDLE handle;
89888985

89898986
#ifdef HAVE_WINDOWS_CONSOLE_IO
89908987
/* Console processes which share a common console can be sent CTRL+C or
89918988
CTRL+BREAK events, provided they handle said events. */
89928989
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
89938990
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
8994-
err = GetLastError();
8995-
PyErr_SetFromWindowsErr(err);
8996-
}
8997-
else {
8998-
Py_RETURN_NONE;
8991+
return PyErr_SetFromWindowsErr(0);
89998992
}
8993+
Py_RETURN_NONE;
90008994
}
90018995
#endif /* HAVE_WINDOWS_CONSOLE_IO */
90028996

90038997
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
90048998
attempt to open and terminate the process. */
9005-
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
8999+
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
90069000
if (handle == NULL) {
9007-
err = GetLastError();
9008-
return PyErr_SetFromWindowsErr(err);
9001+
return PyErr_SetFromWindowsErr(0);
90099002
}
90109003

9011-
if (TerminateProcess(handle, sig) == 0) {
9012-
err = GetLastError();
9013-
result = PyErr_SetFromWindowsErr(err);
9014-
} else {
9015-
result = Py_NewRef(Py_None);
9004+
BOOL res = TerminateProcess(handle, sig);
9005+
CloseHandle(handle);
9006+
if (res == 0) {
9007+
return PyErr_SetFromWindowsErr(0);
90169008
}
90179009

9018-
CloseHandle(handle);
9019-
return result;
9010+
Py_RETURN_NONE;
90209011
#endif /* !MS_WINDOWS */
90219012
}
90229013
#endif /* HAVE_KILL */

0 commit comments

Comments
 (0)