Skip to content

Fix WinError 87 not handled in convert_oserror() (#2519) - #2970

Open
omprakash2929 wants to merge 1 commit into
giampaolo:masterfrom
omprakash2929:fix-windows-signal-winerror87
Open

Fix WinError 87 not handled in convert_oserror() (#2519)#2970
omprakash2929 wants to merge 1 commit into
giampaolo:masterfrom
omprakash2929:fix-windows-signal-winerror87

Conversation

@omprakash2929

Copy link
Copy Markdown
Contributor

Summary

Fixes #2519

Process.send_signal(CTRL_C_EVENT/CTRL_BREAK_EVENT) on Windows can
raise a raw OSError (WinError 87, "the parameter is incorrect")
instead of NoSuchProcess, when the target process terminates on its
own right before the signal is sent (race condition).

Root cause

convert_oserror() in _pswindows.py only converts ProcessLookupError
and permission errors into psutil-friendly exceptions. WinError 87
raised by os.kill() in this race condition wasn't handled, so it
leaked out as a raw OSError.

Fix

Added a check for winerror == 87 in convert_oserror(), converting
it into NoSuchProcess, consistent with how other "process
disappeared" cases are already handled.

Testing

  • tests/test_windows.py::TestProcess::test_ctrl_signals = PASSED
  • tests/test_windows.py::TestProcess::test_send_signal = PASSED
  • Attempted to reproduce the exact race condition locally with a
    stress test script; the underlying scenario is timing-dependent
    (as noted by the original reporter), so the fix targets the error
    code specifically, matching the documented WinError 87 behavior.

@github-actions github-actions Bot added badexc psutil raises something other than NoSuchProcess, AccessDenied, ZombieProcess or TimeoutExpired bug type windows platform : Windows specific labels Aug 9, 2026
@omprakash2929

Copy link
Copy Markdown
Contributor Author

@giampaolo This PR addresses the WinError 87 issue reported in #2519.

Since the race condition is timing-dependent and hard to reproduce
deterministically in a local test, I focused on making the fix logically
correct based on the documented error (WinError 87 - invalid parameter
raised by os.kill() when the process disappears right before the signal
is sent), and verified it doesn't break the existing signal-related tests:

  • tests/test_windows.py::TestProcess::test_ctrl_signals = PASSED
  • tests/test_windows.py::TestProcess::test_send_signal = PASSED

Happy to adjust the approach if you'd prefer a different way to handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

badexc psutil raises something other than NoSuchProcess, AccessDenied, ZombieProcess or TimeoutExpired bug type windows platform : Windows specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] SystemError raised by process.send_signal(signal.CTRL_BREAK_EVENT)

1 participant