[erlang-questions] dialyzer: possible race condition with insert after lookup

Jachym Holecek <>
Sun Oct 23 20:18:34 CEST 2011


# Joel Reymont 2011-10-23:
> Consider this piece of code
> 
> handle_cast({bump, Key, Value}, Data) ->
>     %% cannot update counter on a non-existing record,
>     %% so a lookup cannot be avoided
>     _ = case ets:lookup(stats, Key) of
>             [] -> ets:insert(stats, [{Key, now(), count, Value}]);
>             _  -> ets:update_counter(stats, Key, {4, Value})
>         end,
>     {noreply, Data};

[Funny, this looks like the general area I've been working in most of
today's afternoon. :-)]

> Dialyzer reports that 
> 
> stats.erl:150: [... dialyzer being unexpectedly smart ...]
> 
> What is the proper way to fix this?

Check out ets:insert_new/2.

BR,
	-- Jachym



More information about the erlang-questions mailing list