Issue #5138 has been updated by tenderlovemaking (Aaron Patterson).

File nonblock_no_tuple.patch added
File nonblock_tuple.patch added

=begin
Hi, I've updated the patch to apply against trunk (please find it attached).

Matz, akr, with regard to get_* vs try_read_*, I don't think it will work.  For example, should try_write_* be set_*?  I think it would look strange to say "io.set_nonblock(bytes)".  We also have `sysread_nonblock`, should that be `sysget_nonblock`?  Changing the method names is fine, but I don't think "get/set" pair works well.

As for Erlang style return values.  It seems interesting, but that means every call to `try_read_nonblock` would allocate an array.  The only possible return values would be:

[bytes, nil]  # successful read
[nil, nil] # EOF
[nil, :wait_readable]
[nil, :wait_writable]

In this case it seems easier if we stick with one return value rather than two.  I really want this feature, so I've also prepared a patch with the "tuple" solution (please find it attached).

In order to demonstrate a usecase, we can take rbuf_fill from net/http as an example.  Please find the current method definition here:

  https://github.com/ruby/ruby/blob/07dc8257039f69b41cca50ff49ce738d3df7b362/lib/net/protocol.rb#L151-L169

Here is what it would look like with the tuple method:

  def rbuf_fill
    loop do
      chunk, err = @io.try_read_nonblock(BUFSIZE)
  
      case err
      when :wait_readable
        unless IO.select([@io], nil, nil, @read_timeout)
          raise Net::ReadTimeout
        end
      when :wait_writable
        # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
        # http://www.openssl.org/support/faq.html#PROG10
        unless IO.select(nil, [@io], nil, @read_timeout)
          raise Net::ReadTimeout
        end
      else
        raise EOFError unless chunk
        @rbuf << chunk
        break
      end
    end
  end

Here is what it looks like with just a single return value:

  def rbuf_fill
    loop do
      chunk = @io.try_read_nonblock(BUFSIZE)
  
      case chunk
      when :wait_readable
        unless IO.select([@io], nil, nil, @read_timeout)
          raise Net::ReadTimeout
        end
      when :wait_writable
        # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
        # http://www.openssl.org/support/faq.html#PROG10
        unless IO.select(nil, [@io], nil, @read_timeout)
          raise Net::ReadTimeout
        end
      when nil then raise EOFError
      else
        @rbuf << chunk
        break
      end
    end
  end

We can express nonblocking reads with `loop` rather than using begin/end + an exception and retry as the loop construct.
=end
----------------------------------------
Feature #5138: Add nonblocking IO that does not use exceptions for EOF and EWOULDBLOCK
https://bugs.ruby-lang.org/issues/5138#change-40383

Author: wycats (Yehuda Katz)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


The current Ruby I/O classes have non-blocking methods (read_nonblock and write_nonblock). These methods will never block, and if they would block, they raise an exception instead (IO::WaitReadable or IO::WaitWritable). In addition, if the IO is at EOF, they raise an EOFError.

These exceptions are raised repeatedly in virtually every use of the non-blocking methods. This patch adds a pair of methods (try_read_nonblock and try_write_nonblock) that have the same semantics as the existing methods, but they return Symbols instead of raising exceptions for these routine cases:

* :read_would_block
* :write_would_block
* :eof

The patch contains updates for IO, StringIO, and OpenSSL. The updates are fully documented and tested.


-- 
http://bugs.ruby-lang.org/