Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
Description
Bug report
Bug description:
I will start with saying that documentation forsubprocess.Popen is not clear. I was usingsubprocess.run(..., input=..., ...) and needed to monitor other things while the created process is running, so I changed it tosubprocess.Popen. The only way to send input wasPopen.communicate(input=...), however I wanted to have a timeout. My initial idea was.communicate(input=..., timeout=0), catch the exception, and then continue with.wait(timeout=STEP), however it was hanging. I worked it around by launching.communicate without timeout in a separate thread.
Then, I tested a few platforms and looked at the implementation. On Windows, my initial idea works. On Linux, it hangs. After looking into the implementation, I deduced I shouldn't use.wait(), but rather.communicate() and set the input only for the first invocation. If that's the intention, it would be great to have it documented.
Anyway, the following example hangs on a few Linux versions and few Python versions, including current main branch:
importsubprocessproc=subprocess.Popen( ["cat","-"],stdin=subprocess.PIPE)try:proc.communicate(input="abcd\n".encode(),timeout=0)exceptsubprocess.TimeoutExpired:passprint("it will hang:")proc.communicate()# initially: proc.wait()print("finished")
It looks like a bug, and this line:
Line 2108 in9cd5427
| ifself.stdinandinput: |
which should be changed to:
ifself.stdinandnotself.stdin.closedandself._input:
After this change, the example above no longer hangs. I'll open PR with the mentioned change.
CPython versions tested on:
3.12, 3.14, CPython main branch
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-141473: Fix subprocess.Popen.communicate to send input to stdin upon a subsequent post-timeout call #141477
- [3.14] gh-141473: Fix subprocess.Popen.communicate to send input to stdin upon a subsequent post-timeout call (GH-141477) #142059
- [3.13] gh-141473: Fix subprocess.Popen.communicate to send input to stdin upon a subsequent post-timeout call (GH-141477) #142060
- gh-141473: Document not calling Popen.wait after Popen.communicate times out. #142101
- [3.14] gh-141473: Document not calling Popen.wait after Popen.communicate times out. (GH-142101) #142124
- [3.13] gh-141473: Document not calling Popen.wait after Popen.communicate times out. (GH-142101) #142125