Skip to content

Commit c32fc82

Browse files
author
normal
committed
socket: avoid fcntl for read/write_nonblock on Linux
On platforms where MSG_DONTWAIT works reliably on all sockets (so far, I know of Linux), we can avoid fcntl syscalls and implement IO#write_nonblock and IO#read_nonblock in terms of the socket-specific send and recv family of syscalls. This avoids side effects on the socket, and also encourages generic code to be written in cases where IO wrappers like OpenSSL::SSL::SSLSocket are used. Perhaps in the future, side-effect-free non-blocking I/O can be standard on all files and OSes: https://cr.yp.to/unix/nonblock.html * ext/socket/lib/socket.rb (read_nonblock, write_nonblock): Linux-specific wrapper without side effects [ruby-core:80780] [Feature #13362] * test/socket/test_basicsocket.rb (test_read_write_nonblock): new test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 0013fda commit c32fc82

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ with all sufficient information, see the ChangeLog file or Redmine
6464
* Random.raw_seed renamed to become Random.urandom. It is now
6565
applicable to non-seeding purposes due to [Bug #9569].
6666

67+
* BasicSocket#read_nonblock and BasicSocket#write_nonblock no
68+
longer sets the O_NONBLOCK file description flag as side effect
69+
[Feature #13362]
70+
6771
=== Stdlib compatibility issues (excluding feature bug fixes)
6872

6973
=== C API updates

ext/socket/lib/socket.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,24 @@ def recvmsg_nonblock(dlen = nil, flags = 0, clen = nil,
442442
scm_rights: false, exception: true)
443443
__recvmsg_nonblock(dlen, flags, clen, scm_rights, exception)
444444
end
445+
446+
# Linux-specific optimizations to avoid fcntl for IO#read_nonblock
447+
# and IO#write_nonblock using MSG_DONTWAIT
448+
# Do other platforms suport MSG_DONTWAIT reliably?
449+
if RUBY_PLATFORM =~ /linux/ && Socket.const_defined?(:MSG_DONTWAIT)
450+
def read_nonblock(len, str = nil, exception: true) # :nodoc:
451+
case rv = __recv_nonblock(len, 0, str, exception)
452+
when '' # recv_nonblock returns empty string on EOF
453+
exception ? raise(EOFError, 'end of file reached') : nil
454+
else
455+
rv
456+
end
457+
end
458+
459+
def write_nonblock(buf, exception: true) # :nodoc:
460+
__sendmsg_nonblock(buf, 0, nil, nil, exception)
461+
end
462+
end
445463
end
446464

447465
class Socket < BasicSocket

test/socket/test_basicsocket.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
begin
44
require "socket"
55
require "test/unit"
6+
require "io/nonblock"
67
rescue LoadError
78
end
89

@@ -152,4 +153,53 @@ def test_for_fd
152153
sock.close
153154
end
154155
end
156+
157+
def test_read_write_nonblock
158+
socks do |sserv, ssock, csock|
159+
set_nb = true
160+
buf = String.new
161+
if ssock.respond_to?(:nonblock?)
162+
assert_not_predicate(ssock, :nonblock?)
163+
assert_not_predicate(csock, :nonblock?)
164+
165+
# Linux may use MSG_DONTWAIT to avoid setting O_NONBLOCK
166+
if RUBY_PLATFORM.match?(/linux/) && Socket.const_defined?(:MSG_DONTWAIT)
167+
set_nb = false
168+
end
169+
end
170+
assert_equal :wait_readable, ssock.read_nonblock(1, buf, exception: false)
171+
assert_equal 5, csock.write_nonblock('hello')
172+
IO.select([ssock])
173+
assert_same buf, ssock.read_nonblock(5, buf, exception: false)
174+
assert_equal 'hello', buf
175+
buf = '*' * 16384
176+
n = 0
177+
178+
case w = csock.write_nonblock(buf, exception: false)
179+
when Integer
180+
n += w
181+
when :wait_writable
182+
break
183+
end while true
184+
185+
assert_equal :wait_writable, w
186+
assert_raise(IO::WaitWritable) { loop { csock.write_nonblock(buf) } }
187+
assert_operator n, :>, 0
188+
assert_not_predicate(csock, :nonblock?, '[Feature #13362]') unless set_nb
189+
csock.close
190+
191+
case r = ssock.read_nonblock(16384, buf, exception: false)
192+
when String
193+
next
194+
when nil
195+
break
196+
else
197+
flunk "unexpected read_nonblock return: #{r.inspect}"
198+
end while true
199+
200+
assert_raise(EOFError) { ssock.read_nonblock(1) }
201+
202+
assert_not_predicate(ssock, :nonblock?) unless set_nb
203+
end
204+
end
155205
end if defined?(BasicSocket)

0 commit comments

Comments
 (0)