浏览代码

fix(authn): create authn chain tab in start_link

Ilya Averyanov 4 年之前
父节点
当前提交
3d449b5acf
共有 1 个文件被更改,包括 10 次插入16 次删除
  1. 10 16
      apps/emqx/src/emqx_authentication.erl

+ 10 - 16
apps/emqx/src/emqx_authentication.erl

@@ -277,6 +277,9 @@ initialize_authentication(ChainName, AuthenticatorsConfig) ->
 
 
 -spec start_link() -> {ok, pid()} | ignore | {error, term()}.
 -spec start_link() -> {ok, pid()} | ignore | {error, term()}.
 start_link() ->
 start_link() ->
+    %% Create chains ETS table here so that it belongs to the supervisor
+    %% and survives `emqx_authentication` crashes.
+    ok = create_chain_table(),
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
 
 -spec stop() -> ok.
 -spec stop() -> ok.
@@ -401,7 +404,6 @@ list_users(ChainName, AuthenticatorID, Params) ->
 %%--------------------------------------------------------------------
 %%--------------------------------------------------------------------
 
 
 init(_Opts) ->
 init(_Opts) ->
-    ok = create_chain_table(),
     ok = emqx_config_handler:add_handler([authentication], ?MODULE),
     ok = emqx_config_handler:add_handler([authentication], ?MODULE),
     ok = emqx_config_handler:add_handler([listeners, '?', '?', authentication], ?MODULE),
     ok = emqx_config_handler:add_handler([listeners, '?', '?', authentication], ?MODULE),
     {ok, #{hooked => false, providers => #{}}}.
     {ok, #{hooked => false, providers => #{}}}.
@@ -581,21 +583,13 @@ reply(Reply, State) ->
 %%--------------------------------------------------------------------
 %%--------------------------------------------------------------------
 
 
 create_chain_table() ->
 create_chain_table() ->
-    Status = try
-                 _ = ets:new(?CHAINS_TAB, [named_table, set, public,
-                                           {keypos, #chain.name},
-                                           {read_concurrency, true}]),
-                 created
-             catch
-                 error:badarg -> already_exists
-             end,
-
-    case Status of
-        created ->
-            ets:give_away(?CHAINS_TAB, whereis(emqx_authentication_sup), undefined),
-            ok;
-        already_exists ->
-            ok
+    try
+        _ = ets:new(?CHAINS_TAB, [named_table, set, public,
+                                  {keypos, #chain.name},
+                                  {read_concurrency, true}]),
+        ok
+    catch
+        error:badarg -> ok
     end.
     end.
 
 
 global_chain(mqtt) ->
 global_chain(mqtt) ->