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.

classification
Title: subprocess.Popen.communicate causing local tty terminal settings to change inconsistently
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: JanSpurny, kflavin, martin.panter
Priority: normal Keywords:

Created on 2014-10-17 20:44 by kflavin, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
a.py JanSpurny, 2015-12-29 17:24
Messages (4)
msg229602 - (view) Author: Kyle (kflavin) Date: 2014-10-17 20:44
I'm not sure if this is a bash or Python issue.  I'm trying to run a command on a remote server, using the subprocess module.  I call ssh inside of subprocess.Popen(...).communicate(), like so:

output = subprocess.Popen(["/bin/env %s /usr/bin/ssh -ttt %s@%s -- %s;" % (env, user, host, command)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()

Following the call to communicate(), my terminal settings are changed.  However, it's not always repeatable.  Sometimes it happens, and other times it does not.  When it does happen, I've noticed that the following tty options are ON prior to the command executing, and OFF afterwards (output from stty -a):
icrnl opost isig icanon echo echoe echok

Something with the communicate() call seems to cause the issue.  I don't actually have to print anything to the screen from Python for it to occur.

The problem goes away if I remove the "-t" option to ssh, however, I'm passing through the TERM environmental variable, and need -t to be set.  Because of this, I'm not sure if the problem is with the Python call, or something within Bash.

I've been able to repeat this on Ubuntu 13.10 running Python 2.7.5, and on Red Hat 6.4 running Python 2.6.6.
msg233872 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-01-12 02:09
I tried in Python 2.7.9 on Linux, with env = "TERM=xterm", cmd = "date", and couldn’t get any of those seven terminal settings to be turned off
msg257199 - (view) Author: Jan Spurny (JanSpurny) Date: 2015-12-29 17:24
I'm probably experiencing the same problem - and I've found a way to demonstrate it (almost) reliably:

    import multiprocessing
    import subprocess
    import os

    def x():
        fn = '/tmp/somevideo.avi'
        p = subprocess.Popen('mplayer -identify -frames 0 ' + fn, shell=True,
                             stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        stdout, stderr = p.communicate('')

    os.system('stty -a > 1.txt')
    processes = []
    for i in xrange(2):
        p = multiprocessing.Process(target=x)
        p.start()
        processes.append(p)

    for p in processes:
        p.join()

    os.system('stty -a > 2.txt')
    os.system('diff 1.txt 2.txt')

The result is:

    < isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
    ---
    > isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt


when I replace the Popen call with:    

    p = subprocess.Popen(['mplayer', '-identify', '-frames', '0', fn], shell=False,

the problem is no longer there (diff prints nothing).

It's clear that the problem is caused by mplayer, which usualy runs
interactively and captures user's input.. but I'm pretty sure it's still a bug.

I'm using Debian 8.2, amd64, python2.7.9
msg257200 - (view) Author: Jan Spurny (JanSpurny) Date: 2015-12-29 17:25
(The video file must exist for this to work)
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66852
2015-12-29 17:25:31JanSpurnysetmessages: + msg257200
2015-12-29 17:24:19JanSpurnysetfiles: + a.py
nosy: + JanSpurny
messages: + msg257199

2015-01-12 02:09:01martin.pantersetnosy: + martin.panter
messages: + msg233872
2014-10-17 20:44:33kflavincreate