Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicast socket triggers NPE when bound before IP_ADD_MEMBERSHIP #2352

Open
headius opened this issue Dec 29, 2014 · 0 comments
Open

Multicast socket triggers NPE when bound before IP_ADD_MEMBERSHIP #2352

headius opened this issue Dec 29, 2014 · 0 comments

Comments

@headius
Copy link
Member

headius commented Dec 29, 2014

From http://jira.codehaus.org/browse/JRUBY-7070.

The problem is triggered in the code below by changing to a multicast socket after the bind has happened. In our logic, we defer creating the JDK-level socket object as long as possible, since it is generally not possible to change socket type after that point. setsockopt with IP_ADD_MEMBERSHIP triggers us to flip to using multicast sockets, but without a subsequent bind the actual multicast socket object is never created. That leads to the NPE.

The sequence used in this code is found (for example) in rinda/ring.rb, which binds and then adds membership (at least in the 2.2 version).

The code below should exit normally and the recvfrom should block without raising NPE.

require 'socket'
require 'ipaddr'

MULTICAST_ADDR = '239.255.255.250'
LOCATOR_PORT = 24067

membership = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new("0.0.0.0").hton
sock = UDPSocket.open
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)

begin
  sock.bind(Socket::INADDR_ANY, LOCATOR_PORT)
rescue Errno::EADDRINUSE
  p :here
  raise Error, "Unable to listen on port #{LOCATOR_PORT},"
end

sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, membership)

Thread.new {
  sleep 5
  puts "Exited normally"
  exit  
}


sock.recvfrom(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant