extend/ranch — pull request #43 by fishcakez (2013-03-31 15:01:40+00:00)
Title: Fix ranch_server race conditions

This replaces #32. It adds tests cases (and a fix) to the ranch server race conditions:

1) A cast from ranch_server:add_acceptor/2 could arrive at ranch_server
after a listener had died and its 'DOWN' message had been handled.
This would cause the ranch_server to crash because it assumed the
listener would be present. This is solved by removing the monitoring
acceptors by ranch_server. As ranch_conns_sup now passes protocol
options to protocols there is no need to message acceptors. Two new
functions, ranch_server:set_acceptors_sup/2 and
ranch_server:lookup_acceptors_sup/1 have been added. These allow the
acceptor pids to be looked up using supervisor:which_children/1.

2) If a ranch_server crashed with messages in its queue any registration
casts in its message queue would not be read but the listeners (and
previously acceptors) would have assumed they had been. Also when
ranch_server was restarted it would stop monitoring previously
registered processes. ranch_server now recovers its state by reading
from the public ets table, so all listener pid can be recovered and
remonitored.

3) A listener could be restarted and call ranch_server:insert_listener/2
before ranch_server had handled its predecessor's 'DOWN' message.
This would crash the listener because the ets:insert_new/2 would
return false. The opportunistic registration approach has been
replaced by a call.