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

Joel Reymont <>
Sun Oct 23 20:00:51 CEST 2011


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};

Dialyzer reports that 

stats.erl:150: The call ets:insert('stats',[{_,{non_neg_integer(),non_neg_integer(),non_neg_integer()},'count',_},...]) might have an unintended effect due to a possible race condition caused by its combination with the ets:lookup('stats',Key::any()) call in stats.erl on line 149

What is the proper way to fix this?

	Thanks, Joel

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------




More information about the erlang-questions mailing list