New submission from bugale bugale <buga...@gmail.com>:

The implementation for subprocess.run on Windows has a bug that causes it to 
hang indefinitely in some scenarios.
The issue can be easily reproduced by this code:

import subprocess
subprocess.run(['cmd.exe', '/c', 'ping 1.2.3.4 -n 9999'], capture_output=True, 
timeout=1)

Instead of exiting after 1 second, it hangs indefinitely.

After looking at the code a bit, I found the issue:
https://github.com/python/cpython/blob/efe7d08d178a7c09bcca994f2068b019c8633d83/Lib/subprocess.py#L512

            if _mswindows:
                # Windows accumulates the output in a single blocking
                # read() call run on child threads, with the timeout
                # being done in a join() on those threads.  communicate()
                # _after_ kill() is required to collect that and add it
                # to the exception.
                exc.stdout, exc.stderr = process.communicate()

In the case of Windows, after the process is killed, communicate is called 
without a timeout. This usually works because after the process is killed the 
pipes are closed and the communicate returns almost immediately.
However, if the created subprocess created other processes that hold the pipes, 
they are not closed and this line blocks indefinitely.

----------
components: Library (Lib)
messages: 396653
nosy: bugale bugale
priority: normal
severity: normal
status: open
title: subprocess.run gets stuck indefinitely
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44527>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to