title: Ruby: read_nonblock(???) breaks "system" command
body: <p>I have two versions of a method, I encounter the same problem with both.</p>
  <h1>version 1</h1>
  <pre><code>def read_char
    $stdin.raw!
    input = $stdin.getc.chr
    if input == &quot;\e&quot; then
       input &lt;&lt; $stdin.read_nonblock(3) rescue nil
       input &lt;&lt; $stdin.read_nonblock(2) rescue nil
    end
  ensure
    $stdin.cooked!
    return input
   end
  </code></pre>
  <h1>version 2</h1>
  <pre><code>def read_char
    system(&quot;stty raw -echo&quot;) 
    input = $stdin.getc.chr
    if input == &quot;\e&quot; then
      input &lt;&lt; $stdin.read_nonblock(3) rescue nil
      input &lt;&lt; $stdin.read_nonblock(2) rescue nil
    end
  ensure
    system(&quot;stty -raw echo&quot;)  
    return input
  end
  </code></pre>
  <p>I have the same problem with both.
  Specifically they modify the behavior of  the Kernel#system command.</p>
  <p>As an example:</p>
  <pre><code>system(&quot;irb&quot;)
  c=read_char
  system(&quot;irb&quot;)
  </code></pre>
  <p>It is easy to see the behavior of system is changed and in the second version the inout is not drawn from the tty.</p>
  <p>Note that this does not happen when you press any key, just a key that gets translated to an escape sequence, such as the cursor keys.</p>
  <p>So how is this repaired?</p>
  <h1>PS</h1>
  <p>Someone suggested that I run system(&quot;strace -o logfile irb&quot;) in the second spot and check the read statements. Instead I straced both irbs and compared. The read statements were the same for all but the last one, and they were all to so's. The final read looked different:</p>
  <pre><code>1st system: read(0, &quot;1\n&quot;, 1024)                    = 2
  2nd system: read(0, 0x7ff805a53000, 1024)           = -1 EAGAIN (Resource temporarily unavailable)
  
  0x7ff805a53000 is the address of a memory mapped region.
  </code></pre>
  <h1>PPS</h1>
  <p>I am doing this on a linux system.</p>