Skip to content
Permalink
Browse files
Merge pull request #3541 from rubinius/wait-readable-writable
Update raising of WaitReadable/WaitWritable exceptions
  • Loading branch information
brixen committed Dec 25, 2015
2 parents fde04fc + 6216f2d commit efc8770ac034b1d0cb4e87379716bc778d52aa84
Showing with 75 additions and 14 deletions.
  1. +16 −0 kernel/common/io.rb
  2. +2 −6 spec/ruby/core/io/read_nonblock_spec.rb
  3. +16 −0 spec/ruby/core/io/write_nonblock_spec.rb
  4. +36 −5 vm/builtin/exception.cpp
  5. +3 −1 vm/builtin/exception.hpp
  6. +2 −2 vm/builtin/io.cpp
@@ -14,6 +14,22 @@ class EAGAINWaitWritable < Errno::EAGAIN
include ::IO::WaitWritable
end

class EWOULDBLOCKWaitReadable < Errno::EAGAIN
include WaitReadable
end

class EWOULDBLOCKWaitWritable < Errno::EAGAIN
include WaitWritable
end

class EINPROGRESSWaitReadable < Errno::EINPROGRESS
include WaitReadable
end

class EINPROGRESSWaitWritable < Errno::EINPROGRESS
include WaitWritable
end

# Import platform constants

SEEK_SET = Rubinius::Config['rbx.platform.io.SEEK_SET']
@@ -11,12 +11,8 @@
@write.close rescue nil
end

it "raises EAGAIN when there is no data" do
lambda { @read.read_nonblock(5) }.should raise_error(Errno::EAGAIN)
end

it "raises IO::WaitReadable when there is no data" do
lambda { @read.read_nonblock(5) }.should raise_error(IO::WaitReadable)
it "raises IO::EAGAINWaitReadable when there is no data" do
lambda { @read.read_nonblock(5) }.should raise_error(IO::EAGAINWaitReadable)
end

it "returns at most the number of bytes requested" do
@@ -34,6 +34,22 @@
end
end

describe 'IO#write_nonblock' do
before do
@read, @write = IO.pipe
end

after do
@read.close
@write.close
end

it 'raises IO::EAGAINWaitWritable when the operation would block' do
proc { loop { @write.write_nonblock('a' * 10_000) } }
.should raise_error(IO::EAGAINWaitWritable)
end
end

describe "IO#write_nonblock" do
it_behaves_like :io_write, :write_nonblock
end
@@ -313,15 +313,46 @@ namespace rubinius {
RubyException::raise(exc);
}

void Exception::errno_eagain_error(STATE, const char* reason) {
void Exception::errno_wait_readable(STATE, int error) {
Exception* exc;
Class* exc_class = as<Class>(G(io)->get_const(state, "EAGAINWaitReadable"));
Class* exc_class;

String* message = nil<String>();
if(error == EAGAIN) {
exc_class = as<Class>(G(io)->get_const(state, "EAGAINWaitReadable"));
}
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
else if(error == EWOULDBLOCK) {
exc_class = as<Class>(G(io)->get_const(state, "EWOULDBLOCKWaitReadable"));
}
#endif
else {
exc_class = get_errno_error(state, Fixnum::from(error));
}

if(reason) {
message = String::create(state, reason);
String* message = String::create(state, "read would block");

exc = make_errno_exception(state, exc_class, message, cNil);

RubyException::raise(exc);
}

void Exception::errno_wait_writable(STATE, int error) {
Exception* exc;
Class* exc_class;

if(error == EAGAIN) {
exc_class = as<Class>(G(io)->get_const(state, "EAGAINWaitWritable"));
}
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
else if(error == EWOULDBLOCK) {
exc_class = as<Class>(G(io)->get_const(state, "EWOULDBLOCKWaitWritable"));
}
#endif
else {
exc_class = get_errno_error(state, Fixnum::from(error));
}

String* message = String::create(state, "write would block");

exc = make_errno_exception(state, exc_class, message, cNil);

@@ -88,7 +88,9 @@ namespace rubinius {

static void errno_error(STATE, const char* reason = NULL, int ern = 0,
const char* entity = 0);
static void errno_eagain_error(STATE, const char* reason);

static void errno_wait_readable(STATE, int error);
static void errno_wait_writable(STATE, int error);

/**
* Convenience predicates for checking the class of an
@@ -727,7 +727,7 @@ namespace rubinius {
int res = ::select(fd+1, &set, 0, 0, &tv);

if(res == 0) {
Exception::errno_eagain_error(state, "no data ready");
Exception::errno_wait_readable(state, EAGAIN);
return 0;
} else if(res <= 0) {
Exception::errno_error(state, "read(2) failed");
@@ -834,7 +834,7 @@ namespace rubinius {

// We can use byte_address() here since we use an explicit size
int n = ::write(descriptor_->to_native(), buf->byte_address(), buf_size);
if(n == -1) Exception::errno_error(state, "write_nonblock");
if(n == -1) Exception::errno_wait_writable(state, errno);

state->vm()->metrics().system.write_bytes += n;

0 comments on commit efc8770

Please sign in to comment.