diff --git a/ChangeLog b/ChangeLog index 6dbf72fb48..e20c73f3d2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Sun Feb 22 00:51:58 2009 Tanaka Akira + + * io.c (io_getpartial): error message describes what should be + waited after nonblocking error. + (rb_io_write_nonblock): ditto. + + * ext/socket/init.c (s_recvfrom_nonblock): ditto. + (s_accept_nonblock): ditto. + + * ext/socket/socket.c (sock_connect_nonblock): ditto. + + * ext/socket/ancdata.c (bsock_sendmsg_internal): ditto. + (bsock_recvmsg_internal): ditto. + Sun Feb 22 00:31:42 2009 Tanaka Akira * ext/socket/ancdata.c (bsock_recvmsg_internal): check recvmsg error diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c index e88829db9b..f7763e90a0 100644 --- a/ext/socket/ancdata.c +++ b/ext/socket/ancdata.c @@ -1030,8 +1030,11 @@ bsock_sendmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock) goto retry; } - if (ss == -1) + if (ss == -1) { + if (nonblock && errno == EWOULDBLOCK) + rb_sys_fail("sendmsg(2) WANT_WRITE"); rb_sys_fail("sendmsg(2)"); + } return SSIZET2NUM(ss); } @@ -1290,8 +1293,11 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock) goto retry; } - if (ss == -1) + if (ss == -1) { + if (nonblock && errno == EWOULDBLOCK) + rb_sys_fail("recvmsg(2) WANT_READ"); rb_sys_fail("recvmsg(2)"); + } if (grow_buffer) { int grown = 0; diff --git a/ext/socket/init.c b/ext/socket/init.c index 4665e3e49a..5cf0cbd85c 100644 --- a/ext/socket/init.c +++ b/ext/socket/init.c @@ -192,6 +192,8 @@ s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from) slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, (struct sockaddr*)&buf, &alen); if (slen < 0) { + if (errno == EWOULDBLOCK) + rb_sys_fail("recvfrom(2) WANT_READ"); rb_sys_fail("recvfrom(2)"); } if (slen < RSTRING_LEN(str)) { @@ -453,6 +455,8 @@ s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen rb_io_set_nonblock(fptr); fd2 = accept(fptr->fd, (struct sockaddr*)sockaddr, len); if (fd2 < 0) { + if (errno == EWOULDBLOCK || errno == ECONNABORTED || errno == EPROTO) + rb_sys_fail("accept(2) WANT_READ"); rb_sys_fail("accept(2)"); } make_fd_nonblock(fd2); diff --git a/ext/socket/socket.c b/ext/socket/socket.c index ce7bf9995f..8c92301bd9 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -311,6 +311,8 @@ sock_connect_nonblock(VALUE sock, VALUE addr) rb_io_set_nonblock(fptr); n = connect(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LEN(addr)); if (n < 0) { + if (errno == EINPROGRESS) + rb_sys_fail("connect(2) WANT_WRITE"); rb_sys_fail("connect(2)"); } diff --git a/io.c b/io.c index 2ed05b3d56..59bf9662a7 100644 --- a/io.c +++ b/io.c @@ -1754,6 +1754,8 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock) if (n < 0) { if (!nonblock && rb_io_wait_readable(fptr->fd)) goto again; + if (nonblock && errno == EWOULDBLOCK) + rb_sys_fail("WANT_READ"); rb_sys_fail_path(fptr->pathv); } else if (n == 0) { @@ -1952,7 +1954,11 @@ rb_io_write_nonblock(VALUE io, VALUE str) rb_io_set_nonblock(fptr); n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str)); - if (n == -1) rb_sys_fail_path(fptr->pathv); + if (n == -1) { + if (errno == EWOULDBLOCK) + rb_sys_fail("WANT_WRITE"); + rb_sys_fail_path(fptr->pathv); + } return LONG2FIX(n); } diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 0c1c6b2d2b..0f448dbf39 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -857,6 +857,30 @@ class TestIO < Test::Unit::TestCase end) end + def test_read_nonblock_error + return if !have_nonblock? + with_pipe {|r, w| + begin + r.read_nonblock 4096 + rescue Errno::EWOULDBLOCK + assert_match(/WANT_READ/, $!.message) + end + } + end + + def test_write_nonblock_error + return if !have_nonblock? + with_pipe {|r, w| + begin + loop { + w.write_nonblock "a"*100000 + } + rescue Errno::EWOULDBLOCK + assert_match(/WANT_WRITE/, $!.message) + end + } + end + def test_gets pipe(proc do |w| w.write "foobarbaz" diff --git a/test/socket/test_nonblock.rb b/test/socket/test_nonblock.rb index abc844ceb8..665648a358 100644 --- a/test/socket/test_nonblock.rb +++ b/test/socket/test_nonblock.rb @@ -124,9 +124,18 @@ class TestSocketNonblock < Test::Unit::TestCase af, port, host, addr = serv.addr c = TCPSocket.new(addr, port) s = serv.accept - return c, s + if block_given? + begin + yield c, s + ensure + c.close if !c.closed? + s.close if !s.closed? + end + else + return c, s + end ensure - serv.close if serv + serv.close if serv && !serv.closed? end def test_tcp_recv_nonblock @@ -181,4 +190,65 @@ class TestSocketNonblock < Test::Unit::TestCase end =end + def test_sendmsg_nonblock_error + tcp_pair {|c, s| + begin + loop { + c.sendmsg_nonblock("a" * 100000) + } + rescue Errno::EWOULDBLOCK + assert_match(/WANT_WRITE/, $!.message) + end + } + end + + def test_recvmsg_nonblock_error + tcp_pair {|c, s| + begin + c.recvmsg_nonblock(4096) + rescue Errno::EWOULDBLOCK + assert_match(/WANT_READ/, $!.message) + end + } + end + + def test_recv_nonblock_error + tcp_pair {|c, s| + begin + c.recv_nonblock(4096) + rescue Errno::EWOULDBLOCK + assert_match(/WANT_READ/, $!.message) + end + } + end + + def test_connect_nonblock_error + serv = TCPServer.new("127.0.0.1", 0) + af, port, host, addr = serv.addr + c = Socket.new(:INET, :STREAM) + begin + c.connect_nonblock(Socket.sockaddr_in(port, "127.0.0.1")) + rescue Errno::EINPROGRESS + assert_match(/WANT_WRITE/, $!.message) + end + ensure + serv.close if serv && !serv.closed? + c.close if c && !c.closed? + end + + def test_accept_nonblock_error + serv = Socket.new(:INET, :STREAM) + serv.bind(Socket.sockaddr_in(0, "127.0.0.1")) + serv.listen(5) + port = serv.local_address.ip_port + begin + s, _ = serv.accept_nonblock + rescue Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO + assert_match(/WANT_READ/, $!.message) + end + ensure + serv.close if serv && !serv.closed? + s.close if s && !s.closed? + end + end if defined?(Socket)