title: Running bash in subprocess breaks stdout of tty if interrupted while waiting on `read -s`?
body: <p>As @Bakuriu points out in the comments this is basically the same problem as in <a href="https://stackoverflow.com/questions/31808863/bash-ctrlc-during-input-breaks-current-terminal">BASH: Ctrl+C during input breaks current terminal</a>  However, I can only reproduce the problem when bash is run as a subprocess of another executable, and not directly from bash, where it seems to handle terminal cleanup fine.  I would be interested in an answer as to why bash seems to be broken in this regard.</p>
  
  <p>I have a Python script meant to log the output of subprocess that is started by that script.  If the subprocess happens to be a bash script that at some point reads user input by calling the <code>read -s</code> built-in (the <code>-s</code>, which prevents echoing of entered characters, being key), and the user interrupts the script (i.e. by Ctrl-C), then bash fails to restore output to the tty, even though it continues to accept input.</p>
  
  <p>I whittled this down to a simple example:</p>
  
  <pre><code>$ cat test.py
  #!/usr/bin/python
  import subprocess as sp
  p = sp.Popen(['bash', '-c', 'read -s foo; echo $foo'])
  p.wait()
  </code></pre>
  
  <p>Upon running <code>./test.py</code> it will wait for some input.  If you type some input and press Enter the script returns and echos your input as expected, and there is no issue.  However, if you immediately hit "Ctrl-C", Python displayed a traceback for the <code>KeyboardInterrupt</code>, and then returns to the bash prompt.  However, nothing you type is displayed to the terminal.  Typing <code>reset&lt;enter&gt;</code> successfully resets the terminal, however.</p>
  
  <p>I'm somewhat at a loss as to exactly what's happening here.</p>
  
  <p><strong>Update:</strong> I managed to reproduce this without Python in the mix either.  I was trying to run bash in strace to see if I could glean anything that was going on.  With the following bash script:</p>
  
  <pre><code>$ cat read.sh
  #!/bin/bash
  read -s foo
  echo $foo
  </code></pre>
  
  <p>Running <code>strace ./read.sh</code> and immediately hitting Ctrl-C produces:</p>
  
  <pre><code>...
  ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B38400 opost isig icanon -echo ...}) = 0
  brk(0x1a93000)                          = 0x1a93000
  read(0, Process 25487 detached
   &lt;detached ...&gt;
  </code></pre>
  
  <p>Where PID 25487 was <code>read.sh</code>.  This leaves the terminal in the same broken state.  However, <code>strace -I1 ./read.sh</code> simply interrupts the <code>./read.sh</code> process and returns to a normal, non-broken terminal.</p>