This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bugale bugale
Recipients bugale bugale
Date 2021-06-28.17:29:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624901359.36.0.684419072719.issue44527@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-06-28 17:29:19bugale bugalesetrecipients: + bugale bugale
2021-06-28 17:29:19bugale bugalesetmessageid: <1624901359.36.0.684419072719.issue44527@roundup.psfhosted.org>
2021-06-28 17:29:19bugale bugalelinkissue44527 messages
2021-06-28 17:29:19bugale bugalecreate