[New-bugs-announce] [issue44527] subprocess.run gets stuck indefinitely

bugale bugale report at bugs.python.org
Mon Jun 28 13:29:19 EDT 2021


New submission from bugale bugale <bugalit at 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 <report at bugs.python.org>
<https://bugs.python.org/issue44527>
_______________________________________


More information about the New-bugs-announce mailing list