简体   繁体   中英

issue when supervisor is creating more than one child

I have a supervisor. I get child_specs from the child which is a gen_server. I first wrote this for a single supervisor - child relation. Later I wanted that supervisor to start many childs. But I have some ets tables as state in the child gen_server. So when the supervisor is trying to create the second child it is throwing an exception like:

     exception exit: {shutdown,
                   {failed_to_start_child,bench_client2,
                       {badarg,
                           [{ets,new,[config,[set,named_table]],[]},
                            {bench_client,init,1,
                                [{file,"bench_client.erl"},{line,59}]},
                            {gen_server,init_it,6,
                                [{file,"gen_server.erl"},{line,306}]},
                            {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}}}

I am guessing that since ets is shared, when the supervisor goes to the init function of the second child it already sees a ets table and so the exception but not sure how to get around. only a guess, though.

This is how I am getting the child specs from the child gen_server

child_specs() ->
[begin
 Name = list_to_atom(?MODULE_STRING ++ integer_to_list(Index)),
 {Name, {?MODULE, start_link, [Name]},
   transient, 2000, worker, [bench_client]}
 end || Index <- lists:seq(1, 20)].

init() function of the gen_server is

init([]) ->
Config = ets:new(config, [set, named_table]),
Destinations = ets:new(destinations, [set, named_table]),

I am a complete beginner in erlang and thus having a hard time.

Thank you !

Do you really need named ets tables? When you name a table, only one table by that name can exist within an Erlang node. Get rid of the named_table option in your ets:new/2 call and you won't get the badarg exception anymore.

private

Only the owner process can read or write to the table.

Could you try ets:new(config, [set, named_table, private]) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM