FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-153962: Re-raise unexpected `OSError` in subprocess `Popen._internal_poll` by fedonman · Pull Request #153964 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 7 additions & 0 deletions Lib/subprocess.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,13 @@ def _internal_poll(self, _deadstate=None, _del_safe=_del_safe):
# can't get the status.
# http://bugs.python.org/issue15756
self.returncode = 0
else:
# An unexpected error (e.g. EINVAL) must not be
# silently swallowed, leaving returncode as None:
# surface it to the caller. This branch is never
# reached from __del__, which always passes a
# non-None _deadstate.
raise
Comment on lines +2195 to +2200

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It harms readability if the comment is 5 times large than the actual code, especially if the code is presumably unreachable. I suggest either to leave a minimal one-line comment or to remove it.

finally:
self._waitpid_lock.release()
return self.returncode
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_subprocess.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,25 @@ def test_leak_fast_process_del_killed(self):
else:
self.assertNotIn(ident, [id(o) for o in subprocess._active])

def test_internal_poll_reraises_unexpected_oserror(self):
# An unexpected OSError from waitpid() (i.e. not ECHILD, and not
# during the __del__/_deadstate path) must not be silently swallowed
# by poll(); doing so would leave returncode as None and mask a real
# failure.
p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(3)"])
self.addCleanup(p.wait)
self.addCleanup(p.kill)

def unexpected_waitpid(pid, flags):
raise OSError(errno.EINVAL, "simulated unexpected error")

with mock.patch.object(subprocess._del_safe, "waitpid",
unexpected_waitpid):
with self.assertRaises(OSError) as cm:
p.poll()
self.assertEqual(cm.exception.errno, errno.EINVAL)
self.assertIsNone(p.returncode)

def test_close_fds_after_preexec(self):
fd_status = support.findfile("fd_status.py", subdir="subprocessdata")

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
On POSIX, :meth:`subprocess.Popen.poll` now re-raises an unexpected
:exc:`OSError` from the underlying :func:`os.waitpid` call instead of
silently ignoring it and leaving :attr:`!returncode` set to ``None``. The
special handling of :const:`~errno.ECHILD` is unchanged.
Loading

Back | FazBrowse Home | New Git URL