Issue creating named ets table

I am writing my first erlang module for ejabberd 14.07 and am having a strange issue using ets. If I try to create an ets table using the named_table option I get a "badarg" exception. If I remove the named_table option it works fine. Also I can run the same code through a standalone erlang module on the system and it works fine so it appears to be something to do with it running within ejabberd. Any suggestions would be greatly appreciated as I am out of ideas.

Executing this line of code:
ets:new(foo, [named_table, protected, set, {keypos, 1}])

results in this exception:
2016-02-03 05:52:13.472 [critical] <0.38.0>@gen_mod:start_module:90 Problem starting the module mod_roster_http for host <<"realtalk.test">>
options: []
error: badarg
[{ets,new,[foo,[named_table,protected,set,{keypos,1}]],[]},
{settings,init_settings,1,[{file,"settings.erl"},{line,18}]},
{gen_mod,start_module,3,[{file,"src/gen_mod.erl"},{line,82}]},
{lists,foreach,2,[{file,"lists.erl"},{line,1336}]},
{ejabberd_app,start,2,[{file,"src/ejabberd_app.erl"},{line,67}]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},{line,272}]}]
2016-02-03 05:52:13.472 [critical] <0.38.0>@gen_mod:start_module:95 ejabberd initialization was aborted because a module start failed.

That "bad argument" error

That "bad argument" error probably means that the table already exists.

Example:

ets:new(foo,[named_table,protected,set,{keypos,1}]).  
foo
ets:new(foo,[named_table,protected,set,{keypos,1}]).
** exception error: bad argument

In your case, if you make that call in the start of an ejabberd module, and you have enabled the module in the ejabberd config, and you have several virtual hosts defined in ejabberd, you must understand this: ejabberd starts your module one time for each virtual host. This means that the table is created for the first host, and in the other ones, it complains with a "bad arg" error.

Syndicate content