SBH
SBH

Reputation: 120

Python Thread Breaking Terminal

Hello minds of stackoverflow,

I've run into a perplexing bug. I have a python script that creates a new thread that ssh's into a remote machine and starts a process. However, this process does not return on its own (and I want it to keep running throughout the duration of my script). In order to force the thread to return, at the end of my script I ssh into the machine again and kill -9 the process. This is working well, expect for the fact that it breaks the terminal.

To start the thread I run the following code:

t = threading.Thread(target=run_vUE_rfal, args=(vAP.IP, vUE.IP))
t.start()

The function run_vUE_rfal is as follows:

cmd = "sudo ssh -ti ~/.ssh/my_key.pem user@%s 'sudo /opt/company_name/rfal/bin/vUE-rfal -l 3 -m -d %s -i %s'" % (vUE_IP, vAP_IP, vUE_IP)
output = commands.getstatusoutput(cmd)
return

It seems when the command is run, it somehow breaks my terminal. It is broken in that instead of creating a new line for each print, it appends the WIDTH of my terminal in whitespace to the end of each line and prints it as seemingly one long string. Also, I am unable to see my keyboard input to that terminal, but it still successfully read. My terminal looks something like this:

normal formatted output
normal formatted output
running vUE-rfal
print1
      print2
            print3_extra_long
                             print4

If I replace the body of the run_vUE_rfal function with some simple prints, the terminal does not break. I have many other ssh's and telnets in this script that work fine. However, this is the only one I'm running in a separate thread as it is the only one that does not return. I need to maintain the ability to close the process of the remote machine when my script is finished.

Any explanations to the cause and idea for a fix are much appreciated.

Thanks in advance.

Upvotes: 6

Views: 1622

Answers (1)

deets
deets

Reputation: 6395

It seems the process you control is changing terminal settings. These are bypassing stderr and stdout - for good reasons. E.g. ssh itself needs this to ask users for passwords even when it's output is being redirected.

A way to solve this could be to use the python-module pexpect (it's a 3rd-party library) to launch your process, as it will create its' own fake-tty you don't care about.

BTW, to "repair" your terminal, use the reset command. As you already noticed, you can enter commands. reset will set the terminal to default settings.

Upvotes: 2

Related Questions