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] run() sometimes ignores timeout in Windows #87512
Comments
|
cmd = 'echo spam; echo eggs >&2; sleep 2'
try: p = subprocess.run(cmd, shell=True, capture_output=True,
text=True, timeout=1)
except subprocess.TimeoutExpired as e: ex = e
>>> ex.stdout, ex.stderr
(b'spam\n', b'eggs\n')On Windows, The poses the problem that the second The primary issue is that the pipe handles may be inherited by one or more descendant processes (e.g. via Another problem is that Also, args = 'python -c "import time; time.sleep(99)"'
p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
try: p.communicate(timeout=1)
except: pass
p.kill() # terminates the shell process -- not python.exe
with p: pass # stdout.close() blocks until python.exe exitsThe Windows implementation of The With the proposed changes, |
|
Demo def _read_output(self, fileobj):
handle = msvcrt.get_osfhandle(fileobj.fileno())
output = self._fileobj2output[fileobj]
while True:
try:
size = _winapi.PeekNamedPipe(handle)[0] or 1
data = _winapi.ReadFile(handle, size)[0]
except BrokenPipeError:
break
except OSError as e:
if e.winerror == _winapi.ERROR_OPERATION_ABORTED:
# Should this be mapped to InterruptedError
# (EINTR) in PC/errmap.h?
break
raise
output.append(data)
fileobj.close()
def _communicate(self, input, endtime, orig_timeout):
if not self._communication_started:
self._fileobj2thread = {}
self._fileobj2output = {}
if self.stdout:
self._fileobj2output[self.stdout] = []
if self.stderr:
self._fileobj2output[self.stderr] = []
stdout = self._fileobj2output.get(self.stdout)
stderr = self._fileobj2output.get(self.stderr)
thread_list = []
for fileobj in (self.stdin, self.stdout, self.stderr):
if fileobj is None:
continue
if fileobj in self._fileobj2thread:
thread = self._fileobj2thread[fileobj]
else:
if fileobj == self.stdin:
target, args = self._stdin_write, (input,)
else:
target, args = self._read_output, (fileobj,)
thread = threading.Thread(target=target, args=args,
daemon=True)
thread.start()
self._fileobj2thread[fileobj] = thread
thread_list.append(thread)
for thread in thread_list:
thread.join(self._remaining_time(endtime))
if thread.is_alive():
self._check_timeout(endtime, orig_timeout,
stdout, stderr, skip_check_and_raise=True)
# Join partial reads.
if stdout is not None:
stdout = b''.join(stdout)
if stderr is not None:
stderr = b''.join(stderr)
if self.text_mode:
if stdout is not None:
stdout = self._translate_newlines(stdout,
self.stdout.encoding, self.stdout.errors)
if stderr is not None:
stderr = self._translate_newlines(stderr,
self.stderr.encoding, self.stderr.errors)
return (stdout, stderr)
def _cancel_io(self):
if not hasattr(self, '_fileobj2thread'):
return
for fileobj in (self.stdin, self.stdout, self.stderr):
thread = self._fileobj2thread.get(fileobj)
if thread is None or not thread.is_alive():
continue
try:
handle = _winapi.OpenThread(
_winapi.TERMINATE_THREAD, False, thread.ident)
except OSError:
pass
else:
try:
try:
_winapi.CancelSynchronousIo(handle)
except OSError:
pass
finally:
_winapi.CloseHandle(handle)
def __exit__(self, exc_type, value, traceback):
if _mswindows:
self._cancel_io()
# rest unchanged... |


eryksun commentedFeb 28, 2021
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: