Skip to content

Instantly share code, notes, and snippets.

@zerowidth
Created July 19, 2012 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerowidth/3144130 to your computer and use it in GitHub Desktop.
Save zerowidth/3144130 to your computer and use it in GitHub Desktop.
child socket port re-binding test
# only way to get the child process to re-bind is to set SO_REUSEPORT on both
# the parent socket and the child socket before binding.
require "socket"
puts "in parent"
server = TCPServer.new("127.0.0.1", 4000)
server.setsockopt(:SOCKET, :REUSEPORT, true)
puts server.inspect
if fork
Process.wait
else
puts "in child"
child_server = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
child_server.setsockopt(:SOCKET, :REUSEPORT, true)
child_server.bind(Socket.pack_sockaddr_in(4000, '127.0.0.1'))
puts child_server.inspect
exit
end
@ahoward
Copy link

ahoward commented Jul 19, 2012

yeah that's the option i was thinking of. so to be very durable the parent would need to un-bind to allow bad children to work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment