title: Hanging parent process after running a timed-out subprocess and piping results
body: <p>I wrote some code to run a script (via a subprocess) and kill the child process after a certain timeout. I'm running a script called "runtime_hang_script.sh" that just contains "./runtime_hang," which runs an infinite loop. I'm also redirecting stdout to a pipe -- I plan to write it to both sys.stdout and to a file (aka I'm trying to implement tee). However, my code hangs after the subprocess times out. Note that this ONLY hangs when running "sh runtime_hang_script.sh" and not "./runtime_hang." Also, this doesn't hang when I try piping directly to a file or when I don't read from the pipe.</p>
  
  <p>I've tried other implementations of creating a timed subprocess, but I keep on getting the same issue. I've even tried raising a signal at the end of the problem -- for some reason, the signal is raised earlier than anticipated, so this doesn't work either. Any help would be appreciated. Thanks in advance!</p>
  
  <pre><code>process = None
  
  def run():
      global process
      timeout_secs = 5
      args = ['sh', 'runtime_hang_script.sh']
      sys.stdout.flush()
  
      process = subprocess.Popen(args, stdout=subprocess.PIPE, bufsize=1)
  
      with process.stdout:
          for line in iter(process.stdout.readline, b''):
              sys.stdout.write(line.decode('utf-8'))
          sys.stdout.flush()
  
      process.wait()
  
  
  proc_thread = threading.Thread(target=run)
  proc_thread.start()
  proc_thread.join(5)
  
  print(proc_thread.is_alive())
  
  if proc_thread.is_alive():
      process.kill()
  </code></pre>