Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Aha, subtle.</p> <p>There is a hidden, blocking <code>ensure</code> clause at the end of the IO#popen block in the second case. The Timeout::Error <em>is</em> raised raised timely, but you cannot <code>rescue</code> it until execution returns from that implicit <code>ensure</code> clause.</p> <p><a href="http://rxr.whitequark.org/mri/ident?v=1.9.3;i=rb_io_s_popen" rel="nofollow noreferrer">Under the hood</a>, <code>IO.popen(cmd) { |io| ... }</code> does something like this:</p> <pre><code>def my_illustrative_io_popen(cmd, &amp;block) begin pio = IO.popen(cmd) block.call(pio) # This *is* interrupted... ensure pio.close # ...but then control goes here, which blocks on cmd's termination end </code></pre> <p>and the IO#close call is really more-or-less a <code>pclose(3)</code>, which is blocking you in <code>waitpid(2)</code> until the sleeping child exits.</p> <p>You can verify this like so:</p> <pre><code>#!/usr/bin/env ruby require 'timeout' BEGIN { $BASETIME = Time.now.to_i } def xputs(msg) puts "%4.2f: %s" % [(Time.now.to_f - $BASETIME), msg] end begin Timeout.timeout(3) do begin xputs "popen(sleep 10)" pio = IO.popen("sleep 10") sleep 100 # or loop over pio.gets or whatever ensure xputs "Entering ensure block" #Process.kill 9, pio.pid # &lt;--- This would solve your problem! pio.close xputs "Leaving ensure block" end end rescue Timeout::Error =&gt; ex xputs "rescuing: #{ex}" end </code></pre> <p><strong>So, what can you do?</strong></p> <p>You'll have to do it the explicit way, since the interpreter doesn't expose a way to override the IO#popen <code>ensure</code> logic. You can use the above code as a starting template and uncomment the <code>kill()</code> line, for example.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload