Skip to content

Commit 5c4cbf1

Browse files
vstinnersrinivasreddy
authored andcommitted
pythongh-58689: Fix os.kill() error handling on Windows (python#128932)
1 parent f12e00e commit 5c4cbf1

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Modules/posixmodule.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9582,42 +9582,33 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
95829582

95839583
Py_RETURN_NONE;
95849584
#else /* !MS_WINDOWS */
9585-
PyObject *result;
95869585
DWORD sig = (DWORD)signal;
9587-
DWORD err;
9588-
HANDLE handle;
95899586

95909587
#ifdef HAVE_WINDOWS_CONSOLE_IO
95919588
/* Console processes which share a common console can be sent CTRL+C or
95929589
CTRL+BREAK events, provided they handle said events. */
95939590
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
95949591
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
9595-
err = GetLastError();
9596-
PyErr_SetFromWindowsErr(err);
9597-
}
9598-
else {
9599-
Py_RETURN_NONE;
9592+
return PyErr_SetFromWindowsErr(0);
96009593
}
9594+
Py_RETURN_NONE;
96019595
}
96029596
#endif /* HAVE_WINDOWS_CONSOLE_IO */
96039597

96049598
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
96059599
attempt to open and terminate the process. */
9606-
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
9600+
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
96079601
if (handle == NULL) {
9608-
err = GetLastError();
9609-
return PyErr_SetFromWindowsErr(err);
9602+
return PyErr_SetFromWindowsErr(0);
96109603
}
96119604

9612-
if (TerminateProcess(handle, sig) == 0) {
9613-
err = GetLastError();
9614-
result = PyErr_SetFromWindowsErr(err);
9615-
} else {
9616-
result = Py_NewRef(Py_None);
9605+
BOOL res = TerminateProcess(handle, sig);
9606+
CloseHandle(handle);
9607+
if (res == 0) {
9608+
return PyErr_SetFromWindowsErr(0);
96179609
}
96189610

9619-
CloseHandle(handle);
9620-
return result;
9611+
Py_RETURN_NONE;
96219612
#endif /* !MS_WINDOWS */
96229613
}
96239614
#endif /* HAVE_KILL */

0 commit comments

Comments
 (0)