From: "Christoffer Lernö" Date: 2007-10-15T04:51:10+09:00 Subject: Re: write_nonblock is blocking? On 14 Oct 2007, at 18:14, Tanaka Akira wrote: > In article , > Christoffer Lernö writes: > >> Could this be due to me running >> >> ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1] ? >> >> The funny thing was that only the socket returned by accept_nonblock >> had this behaviour, that is > > I think it is a Mac OS X problem. > > Your example works without problem on GNU/Linux and FreeBSD > box but not on Mac OS X. > > % cat z1.rb > require 'socket' > require 'fcntl' > a = TCPServer.new(5000) > socket = TCPSocket.new("localhost", 5000) > other = a.accept > p other.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK > puts "Start Write" > p other.write_nonblock("X" * 300000) > puts "End Write" > % ./ruby -v z1.rb > ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.3.0] > 0 > Start Write > 81560 > End Write > > The IO returned from accept is not nonblocking mode. > So write_nonblock makes it nonblocking mode at first. > > % cat z2.rb > require 'socket' > require 'fcntl' > a = TCPServer.new(5000) > socket = TCPSocket.new("localhost", 5000) > other = a.accept_nonblock > p other.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK > puts "Start Write" > p other.write_nonblock("X" * 300000) > puts "End Write" > % ./ruby -v z2.rb > ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.3.0] > 4 > Start Write > ^Cz2.rb:8: Interrupt > > The IO returned from accept_nonblock seems nonblocking mode. > So write_nonblock doesn't make it nonblocking mode. > But actually it is not nonblocking mode, I think. Yeah, that seems to be it. This workaround in the ruby code gets around the problem: require 'socket' require 'fcntl' a = TCPServer.new(5000) socket = TCPSocket.new("localhost", 5000) other = a.accept_nonblock other.fcntl(Fcntl::F_SETFL, other.fcntl(Fcntl::F_GETFL) & ~ (Fcntl::O_NONBLOCK)) puts "Start Write" other.write_nonblock("X" * 300000) puts "End Write" /C