-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-18299: extending a kill_on_os_error() in Popen #13908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Using context manager kill subprocess if a python's exception raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests for this new feature are required
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Andrew, thanks for review. I will replenish it. |
Lib/test/test_subprocess.py
Outdated
@@ -2814,6 +2814,11 @@ def test_stopped(self): | |||
|
|||
self.assertEqual(returncode, -3) | |||
|
|||
def test_kill_on_error(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks it only can add positive testcase due to the core code just only yield self
If somebody have a good idea about negatigve testcase, pls ping me;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could create a Popen subclass that overrides either __enter__
or __exit__
to raise an exception. (or do the equivalent using unittest.mock)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of them can not work in here. for the reason:
- We use contextmanager in the new function and it can not work with
__enter__
or__exit__
. - Looks like
yield proc
can not be mocked to raise a error.
I have made the requested changes; please review again |
Thanks for making the requested changes! @asvetlov: please review the changes made to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not entirely clear to me what this method is intended to do. Its name doesn't make that obvious to me, "on error" is ambiguous.
Doc/library/subprocess.rst
Outdated
@@ -723,6 +723,11 @@ Instances of the :class:`Popen` class have the following methods: | |||
On Windows :meth:`kill` is an alias for :meth:`terminate`. | |||
|
|||
|
|||
.. method:: Popen.kill_on_error() | |||
|
|||
Kills the child when a python error raised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a python error is raised from where and when?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, Gregory, thank for your elaborative review:).
one practical case would be found in https://bugs.python.org/issue25122(due to some race condition on FreeBSD) and i updated the subprocess.rst too.
Lib/subprocess.py
Outdated
|
||
@contextlib.contextmanager | ||
def kill_on_error(self): | ||
"""Using context manager to kill subprocess if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does using a context manager kill the process and why would someone reading this understand that to be what it does? Does it matter to the docstring reader if a context manager is used?
similar to the documentation comment: if an python exception is raised is non-specific. raised from where and when? this could easily be misinterpreted to have something to do with the exit status of the child process or even exceptions raised within it.
"on error" is a very ambiguious method name. someone reading code calling this won't intuitively understand what it does other than hopefully kill the process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using context manager can use try...except...
statement to trap the error. I udpated the docstring too.
I use on_os_error to replace on_error.
Lib/subprocess.py
Outdated
try: | ||
self.kill() | ||
except OSError: | ||
# process already terminated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI - There is another reason why self.kill() can fail: PermissionError. When a setuid process is launched (or potentially other system configurations enforcing process policies) you may get a PermissionError trying to signal that process.
When a PermissionError happens, the wait() call will block until the process dies on its own, if ever. https://bugs.python.org/issue37091 covers this one. It isn't clear if we should do anything for this situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks try all possible exceptions or remove all possible exceptions is fine.
Lib/test/test_subprocess.py
Outdated
@@ -2814,6 +2814,11 @@ def test_stopped(self): | |||
|
|||
self.assertEqual(returncode, -3) | |||
|
|||
def test_kill_on_error(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could create a Popen subclass that overrides either __enter__
or __exit__
to raise an exception. (or do the equivalent using unittest.mock)
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be put in the comfy chair! |
I have made the requested changes; please review again |
I'm rejecting this PR. This is not an API we need on the There may be utility on a context manager that tries to terminate a process when an exception is escaping the context, but that belongs in its own library. It isn't something users of subprocess need on a regular basis. The PR references bpo-18299 but I see no relation to that bug. If something like this would've been used in that bug maybe this belongs in Misc review notes for posterity here rather than trying to connect them with parts of the diffs: The description in the documentation still makes no sense. We don't protect anyone from race conditions or hangs. And the method name is still misleading. It appears to attempt to kill the process if any exception escapes the context manager followed by waiting for it regardless of kill succeeding or not (that wait may hang if not). Yet the method name is confusing as a call to Popen.kill() tries to kill the process immediately while a call to Popen.kill_on_os_error() does nothing unless the caller uses it as a context manager. A better name if this were to be a method at all would be something more along the lines of .get_killing_context. It is possible to write unittests for this functionality, tests would be required. |
Copy that. |
Using context manager kill subprocess if a python's exception raised.
https://bugs.python.org/issue18299