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
subprocess - uncaught PermissionError in send_signal can cause hang #81272
Comments
Python 3.7.3 Encountered a condition where uncaught PermissionError caused a hang Traceback (most recent call last):
File "/usr/lib/python3.7/subprocess.py", line 474, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.7/subprocess.py", line 939, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.7/subprocess.py", line 1707, in _communicate
self.wait(timeout=self._remaining_time(endtime))
File "/usr/lib/python3.7/subprocess.py", line 990, in wait
return self._wait(timeout=timeout)
File "/usr/lib/python3.7/subprocess.py", line 1616, in _wait
raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command '['sudo', '-u', 'chrome', '/snap/bin/chromium', '--headless', '--disable-gpu', '--hide-scrollbars', '--ignore-certificate-errors', '--enable-sandbox', '--incognito', '--mute-audio', '--disable-databases', '--enable-strict-powerful-feature-restrictions', '--no-pings', '--no-referrers', '--timeout=30000', '--window-size=1280,1000', '--screenshot=[REDACTED]_screenshot.png', 'https://[REDACTED]']' timed out after 59.9998986274004 seconds
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.7/subprocess.py", line 476, in run
process.kill()
File "/usr/lib/python3.7/subprocess.py", line 1756, in kill
self.send_signal(signal.SIGKILL)
File "/usr/lib/python3.7/subprocess.py", line 1746, in send_signal
os.kill(self.pid, sig)
PermissionError: [Errno 1] Operation not permitted |
Clarifying: The problem is that the TimeoutExpired exception that is desired was replaced with a PermissionError that the caller isn't expecting to catch and handle. |
even with that initially proposed patch, the subprocess.run code is going to hang if the .kill() fails without raising an exception: The run() TimeoutExpired path calls communicate() after the kill to consume all remaining output: and the __exit__ code path calls wait(). both assume that the kill() succeeded because the normal case is not someone launching a child process that they are unable to kill (as happens when sudo or similar is used). Today's workarounds: Don't use timeout when launching a child you cannot kill. OR launch it via an intermediate process that you can kill (shell=True or equivalent). To address this issue, catching a PermissionError exception around both of those kill() calls and preventing it from calling either communicate() or wait() as we cannot force the child process to exit makes sense. With some indication that this happened left as a hint to the user on the TimeoutExpired itself. |
I think this was fixed in bpo-37424. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: