]> granicus.if.org Git - ejabberd/commitdiff
Avoid a possible race condition
authorBadlop <badlop@process-one.net>
Sat, 26 Nov 2011 16:03:24 +0000 (17:03 +0100)
committerBadlop <badlop@process-one.net>
Sat, 26 Nov 2011 16:03:24 +0000 (17:03 +0100)
src/mod_ip_blacklist.erl

index eac61a76699f5fdf514e6a2fa7d510418ff40e09..718a25b1e29f476344f0f5bbbe1b327453f717e1 100644 (file)
@@ -33,6 +33,7 @@
 
 %% API:
 -export([start/2,
+         preinit/2,
          init/1,
          stop/1]).
 -export([update_bl_c2s/0]).
 -record(bl_c2s, {ip}).
 
 %% Start once for all vhost
-start(Host, Opts) ->
-    case whereis(?PROCNAME) of
-        undefined ->
-            ?DEBUG("Starting mod_ip_blacklist ~p  ~p~n", [Host, Opts]),
-              register(?PROCNAME,
-                     spawn(?MODULE, init, [#state{}]));
-        _ ->
-            ok
+start(_Host, _Opts) ->
+   Pid = spawn(?MODULE, preinit, [self(), #state{}]),
+   receive {ok, Pid, PreinitResult} ->
+       PreinitResult
+   end.
+
+preinit(Parent, State) ->
+    Pid = self(),
+    try register(?PROCNAME, Pid) of
+        true ->
+            Parent ! {ok, Pid, true},
+            init(State)
+    catch error:_ ->
+        Parent ! {ok, Pid, true}
     end.
 
 %% TODO: