Bugs item #2047, was opened at 2005-06-20 12:24 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=2047&group_id=426 Category: Standard Library Group: None Status: Open Resolution: None Priority: 3 Submitted By: Brian Candler (bcandler) Assigned to: Nobody (None) Summary: TCPSocket fails to bind first time on certain FreeBSD configs Initial Comment: I have some very strange reproducible behaviour with TCPSocket. It differs between two machines which are almost identical: one is FreeBSD-5.4-RELEASE, the other is FreeBSD-5.4-STABLE dating from only a week or so after 5.4-RELEASE. Both report Ruby version as "ruby 1.8.2 (2004-12-25) [i386-freebsd5]" The 5.4-RELEASE machine is running a GENERIC kernel with IPv6 enabled. The 5.4-STABLE machine has a slightly customised kernel, with IPv6 turned off. This is the only thing which I can imagine is significant. Now, it's the 5.4-STABLE machine (no IPv6) which is being strange: $ irb irb(main):001:0> require 'socket' => true irb(main):002:0> a = TCPServer.new(1234) => #<TCPServer:0x813aed0> irb(main):003:0> At this point, you'd expect the server to be listening on port 1234. However, it isn't: in another window I can try $ telnet localhost 1234 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host $ sockstat -4l USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS mailnull exim-4.50- 432 4 tcp4 *:25 *:* root sshd 393 4 tcp4 *:22 *:* root lpd 363 7 tcp4 *:515 *:* root syslogd 266 6 udp4 *:514 *:* However, as soon as I create a *second* TCPServer object, it works. irb(main):003:0> b = TCPServer.new(1234) => #<TCPServer:0x81387c0> irb(main):004:0> and in another window: $ sockstat -4l USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS brian ruby18 1186 4 tcp4 *:1234 *:* mailnull exim-4.50- 432 4 tcp4 *:25 *:* root sshd 393 4 tcp4 *:22 *:* root lpd 363 7 tcp4 *:515 *:* root syslogd 266 6 udp4 *:514 *:* $ telnet localhost 1234 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. This is completely repeatable. Now: if I give an IP address explicitly to bind to, it's OK: $ irb irb(main):001:0> require 'socket' => true irb(main):002:0> a = TCPServer.new("0.0.0.0",1234) => #<TCPServer:0x813a69c> irb(main):003:0> $ sockstat -4l USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS brian ruby18 1196 3 tcp4 *:1234 *:* mailnull exim-4.50- 432 4 tcp4 *:25 *:* root sshd 393 4 tcp4 *:22 *:* root lpd 363 7 tcp4 *:515 *:* root syslogd 266 6 udp4 *:514 *:* So I have a workaround which solves my problem - but it looks like there's something strange going on under the surface. I have tried building the latest snapshot: -rw-r--r-- 1 snapshot nogroup 4124201 Jun 19 19:00 stable-snapshot.tar.gz but the problem seems to be the same: brian:/u/build/ruby$ ruby -I ext/socket -r socket -e 'a = TCPServer.new(1234); sleep 50' & [1] 9335 brian:/u/build/ruby$ telnet localhost 1234 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=2047&group_id=426