Python subprocess.Popen blocks with shell and pipe

sobel

Running the following command: yes | head -1, from the shell, outputs y. Using python's subprocess module to call it with a shell hangs indefinitely:

import subprocess

arg = "yes | head -1"
process = subprocess.Popen(arg,
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)

print "Command started: %d" % process.pid
r = process.communicate()
print "Command ended: %s %s" % r

Killing the process exogenously using kill does not help, nor does making the child process it's session leader using preexec_fn=os.setsid.

What could be causing this behavior, and is there anyway I can prevent it?

I'm running python 2.7.3 and my /bin/sh is GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

sobel

So it turns out that this is caused due to a known issue with python not resetting signal handlers before exec in the subprocess module.

This is the same problem that causes 'yes' reporting error with subprocess communicate(), except with GNU yes, the EPIPE returned by a write causes the program to abort, whereas with BSD yes (which is used on OS X as far as I can tell), the return code of the write is not checked, so without a SIGPIPE, yes will not terminate.

It can be fixed by backporting the reset_signals code, as in the above linked question:

def restore_signals():
    signals = ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ')
    for sig in signals:
        if hasattr(signal, sig):
            signal.signal(getattr(signal, sig), signal.SIG_DFL)

then setting preexec_fn=restore_signals in the Popen call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python subprocess popen pipe

From Dev

Python 3 subprocess pipe blocks

From Dev

Python 3 subprocess pipe blocks

From Dev

Python subprocess.Popen pipe IO blocking unexpectedly

From Dev

executing shell script using subprocess.Popen in Python?

From Dev

Python popen shell command wait till subprocess has finished

From Dev

Passing a "shell script" as a string to Python subprocess.Popen

From Dev

Python subprocess.Popen error handling with shell=True/False issues

From Dev

python popen pipe in loop

From Dev

Python subprocess: pipe an image blob to imagemagick shell command

From Dev

Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

From Dev

Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

From Dev

pid of a process created by python subprocess.Popen(shell=True) is not the pid of the spawned shell

From Dev

Python subprocess module and PIPE

From Dev

Python subprocess Popen passing arguments

From Dev

Python Subprocess.Popen and rdesktop

From Dev

Python subprocess/Popen stdout is truncated

From Dev

Formatting a command in python subprocess popen

From Dev

Python use timeout for subprocess with Popen

From Dev

Python subprocess Popen passing arguments

From Dev

python subprocess popen starts immediately

From Dev

Python subprocess/Popen stdout is truncated

From Dev

Formatting a command in python subprocess popen

From Dev

Python redirection in subprocess.Popen

From Dev

Subprocess Popen to run python command

From Dev

subprocess.Popen is not running shell command

From Dev

Python's Subprocess.Popen With Shell=True. Wait till it is completed

From Dev

convert from bash shell to python3 using subprocess.Popen()

From Dev

Python's Subprocess.Popen With Shell=True. Wait till it is completed

Related Related

  1. 1

    Python subprocess popen pipe

  2. 2

    Python 3 subprocess pipe blocks

  3. 3

    Python 3 subprocess pipe blocks

  4. 4

    Python subprocess.Popen pipe IO blocking unexpectedly

  5. 5

    executing shell script using subprocess.Popen in Python?

  6. 6

    Python popen shell command wait till subprocess has finished

  7. 7

    Passing a "shell script" as a string to Python subprocess.Popen

  8. 8

    Python subprocess.Popen error handling with shell=True/False issues

  9. 9

    python popen pipe in loop

  10. 10

    Python subprocess: pipe an image blob to imagemagick shell command

  11. 11

    Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

  12. 12

    Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

  13. 13

    pid of a process created by python subprocess.Popen(shell=True) is not the pid of the spawned shell

  14. 14

    Python subprocess module and PIPE

  15. 15

    Python subprocess Popen passing arguments

  16. 16

    Python Subprocess.Popen and rdesktop

  17. 17

    Python subprocess/Popen stdout is truncated

  18. 18

    Formatting a command in python subprocess popen

  19. 19

    Python use timeout for subprocess with Popen

  20. 20

    Python subprocess Popen passing arguments

  21. 21

    python subprocess popen starts immediately

  22. 22

    Python subprocess/Popen stdout is truncated

  23. 23

    Formatting a command in python subprocess popen

  24. 24

    Python redirection in subprocess.Popen

  25. 25

    Subprocess Popen to run python command

  26. 26

    subprocess.Popen is not running shell command

  27. 27

    Python's Subprocess.Popen With Shell=True. Wait till it is completed

  28. 28

    convert from bash shell to python3 using subprocess.Popen()

  29. 29

    Python's Subprocess.Popen With Shell=True. Wait till it is completed

HotTag

Archive