title: How to fix hanging popen3 in Ruby?
body: <p>I am getting unexpected behaviour using popen3, which I want to use to run a command like tool ala <code>cmd &lt; file1 &gt; file2</code>. The below example hangs, so that <code>stdout done</code> is never reached. Using other tools than <code>cat</code> may cause hanging, so that <code>stdin done</code> is never reached. I suspect, I am suffering from buffering, but how do I fix this? </p>
  
  <pre><code>#!/usr/bin/env ruby
  
  require 'open3'
  
  Open3.popen3("cat") do |stdin, stdout, stderr, wait_thr|
    stdin.puts "foobar"
  
    puts "stdin done"
  
    stdout.each_line { |line| puts line }
  
    puts "stdout done"
  
    puts wait_thr.value
  end
  
  puts "all done"
  </code></pre>