[erlang-questions] ets behavior

Alin Popa alin.popa@REDACTED
Thu Jun 11 07:39:26 CEST 2009


Thanks Steve,

Now's more clear.

On Thu, Jun 11, 2009 at 8:30 AM, Steve Vinoski <vinoski@REDACTED> wrote:

> On 6/11/09, Alin Popa <alin.popa@REDACTED> wrote:
> > Hi,
> >
> >  I have the following behavior when using ets:
> >
> >  *1> ets:new(test, [named_table, public]).
> >  test
> >  2> ets:insert(test, {self(), 0}).
> >  true
> >  3> ets:update_counter(test, self(), 1).
> >  1
> >  4> ets:update_counter(test, self(), 2).
> >  3
> >  5> ets:update_counter(test, self(), 3).
> >  6
> >  6> ets:lookup(test, self()).
> >  [{<0.33.0>,6}]
> >  7> ets:info(test, self()).
> >  ** exception error: bad argument
> >      in function  ets:info/2
> >         called as ets:info(test,<0.33.0>)
>
> This is an illegal call. See the documentation for ets:info to see
> what arguments are allowed. Because of the badarg exception, you've
> just killed your shell, which was the owner of the ets table, so the
> ets table is now non-existent. If you were to call ets:info(test,
> owner) at this point, for example, you'd get undefined as the result,
> indicating that the table is gone.
>
> >  8> ets:update_counter(test, self(), 1).
> >  ** exception error: bad argument
> >      in function  ets:update_counter/3
> >         called as ets:update_counter(test,<0.41.0>,1)
> >  9> ets:update_counter(test, self(), 3).
> >  ** exception error: bad argument
> >      in function  ets:update_counter/3
> >         called as ets:update_counter(test,<0.43.0>,3)
> >  10> ets:update_counter(test, self(), 4).
> >  ** exception error: bad argument
> >      in function  ets:update_counter/3
> >         called as ets:update_counter(test,<0.45.0>,4)
> >  11>
> >  *
> >  What is happening here ? Why my counter (called test) is crashed ? A
> repair
> >  function should be called or something ?
>
> These all fail because the table is gone at this point. This is a new
> shell process, as you can see by the new value of self(). In fact, as
> you can see a new shell is created after each exception above.
>
> If you replace your call to ets:info at prompt 7 in this example with
> the following, then all is well:
>
> 7> (catch ets:info(test, self())).
> {'EXIT',{badarg,[{ets,info,[test,<0.33.0>]},
>                 {erl_eval,do_apply,5},
>                 {erl_eval,expr,5},
>                 {shell,exprs,6},
>                 {shell,eval_exprs,6},
>                 {shell,eval_loop,3}]}}
> 8> ets:update_counter(test, self(), 1).
> 7
>
> Here we catch the badarg to prevent it from killing the shell, so the
> table is still valid for the subsequent update_counter call.
>
> --steve
>



-- 
Regards,

Alin


More information about the erlang-questions mailing list