title: Ruby - Open3 not finishing subprocess
body: <p>I'm using:</p>
  <pre><code>- Ruby 1.9.3-p448
  - Windows Server 2008
  </code></pre>
  <p>I have a file that contains commands that is used by a program, I'm using it in this way</p>
  <pre><code>C:\&gt; PATH_TO_FOLDER/program.exe file.txt
  </code></pre>
  <p><code>File.txt</code> have some commands so <code>program.exe</code> will do the following:</p>
  <pre><code>- Execute commands 
  - Reads from a DB using an ODBC method used by program
  - Outputs result in a txt file
  </code></pre>
  <p>Using powershell this command works fine and as expected.</p>
  <p>Now I have this in a file (<code>app.rb</code>):</p>
  <pre><code>require 'sinatra'
  require 'open3'
  
  get '/process' do
    program_path = &quot;path to program.exe&quot;
    file_name = &quot;file.txt&quot;
    Open3.popen3(program_path, file_name) do |i, o, e, w|
      # I have some commands here to execute but just as an example I'm using o.read
      puts o.read
    end
  end
  </code></pre>
  <p>Now when using this by accessing <code>http://localhost/process</code>, <code>Open3</code> works by doing this (I'm not 100% sure but after trying several times I think is the only option)</p>
  <ul>
  <li><p>Reads commands and executes them (this is ok)</p>
  </li>
  <li><p>Tries to read from DB by using ODBC method (Here is my problem. I
  need to receive some output from <code>Open3</code> so I can show it in a browser, but I guess when it tries to read it starts another process that <code>Open3</code> is not aware of, so Open3 goes on and finish without waiting for it)</p>
  </li>
  <li><p>Exits</p>
  </li>
  </ul>
  <p>Exits</p>
  <p>I've found about following:</p>
  <ul>
  <li>Use <code>Thread.join</code> (in this case, <code>w.join</code>) in order to wait for process to finish, but it doesn't work</li>
  <li><code>Open4</code> seems to handle child process but doesn't work on Windows</li>
  <li><code>Process.wait(pid)</code>, in this case <code>pid = w.pid</code>, but also doesn't work</li>
  <li><code>Timeout.timeout(n)</code>, the problem here is that I'm not sure how long
  will it take.</li>
  </ul>
  <p>Is there any way of handling this? (waiting for <code>Open3</code> subprocess so I get proper output).</p>