From a700368b2cb021bb1cd007697db4a3bae0099f2d Mon Sep 17 00:00:00 2001 From: James Fish Date: Mon, 1 Apr 2013 23:15:45 +0100 Subject: Fix ranch_server:set_connections_sup/2 race conditions A ranch_conns_sup could be (re)started and call ranch_server:set_connections_sup/2 before ranch_server has handled the predecessor's exit. This would cause the ranch_server to crash because ets:insert_new/2 would return false. This change allows ranch_server to handle this case by crashing the calling process instead of itself. --- test/acceptor_SUITE.erl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/acceptor_SUITE.erl b/test/acceptor_SUITE.erl index ca8c856..861ea33 100644 --- a/test/acceptor_SUITE.erl +++ b/test/acceptor_SUITE.erl @@ -49,6 +49,7 @@ -export([supervisor_clean_child_restart/1]). -export([supervisor_conns_alive/1]). -export([supervisor_server_recover_state/1]). +-export([supervisor_clean_conns_sup_restart/1]). %% ct. @@ -77,7 +78,8 @@ groups() -> supervisor_clean_restart, supervisor_clean_child_restart, supervisor_conns_alive, - supervisor_server_recover_state + supervisor_server_recover_state, + supervisor_clean_conns_sup_restart ]}]. init_per_suite(Config) -> @@ -499,6 +501,26 @@ supervisor_server_recover_state(_) -> _ = erlang:trace(all, false, [all]), ok = clean_traces(). +supervisor_clean_conns_sup_restart(_) -> + %% Verify that a conns_sup can not register with the same Name as an already + %% registered conns_sup that is still alive. Make sure this does not crash + %% the ranch_server. + Name = supervisor_clean_conns_sup_restart, + {ok, _} = ranch:start_listener(Name, + 1, ranch_tcp, [{port, 0}], echo_protocol, []), + Server = erlang:whereis(ranch_server), + ServerMonRef = erlang:monitor(process, Server), + %% Exit because Name already registered and is alive. + {'EXIT', _} = (catch ranch_server:set_connections_sup(Name, self())), + receive + {'DOWN', ServerMonRef, process, Server, _} -> + error(ranch_server_down) + after + 1000 -> + ok + end, + ranch:stop_listener(Name). + %% Utility functions. connect_loop(_, 0, _) -> -- cgit v1.2.3