Mnesia deadlock detector

Dan Gudmundsson <>
Mon Aug 9 14:09:41 CEST 1999


Hi Luke

You're right it is the deadlock avoidance algo. 
The lock will usally be queued but if that's not allowed 
(due to wrong order in transaction identifier)
the transaction will sleep for awhile and then be restarted.

The sleeping time has been decreased in mnesia 3.7 

/Dan

Luke Gorrie writes:
 > Hi all, me again,
 > 
 > I've been poking around with mnesia this time, and I've found some
 > strange behaviour apparently relating to deadlock detection.  I have a 
 > function which I'm using for transactions:
 > 
 > fun() -> mnesia:write(#test_rec{}) end
 > 
 > With: -record(test_rec, {id=1, date=now()}).
 > 
 > Running that function in mnesia:transaction/1 seems to take about
 > 0.7ms on my machine.  However, when I run that function several times
 > concurrently the performance drops hugely.  It looks like Mnesia is
 > thinking there are deadlocks, because it cancels the transactions that 
 > miss out on the write lock, puts them to sleep for a random length of
 > time (I've experienced over 900ms), and then wakes them again (at
 > which point they seem to sleep/restart again if the lock isn't free).
 > 
 > Thought I'd report it incase it's a bug.  It sounds like
 > deadlock-avoidance behaviour, but all that's actually occuring here is 
 > contention for the write lock as far as I can tell.  Or maybe I'm
 > doing something silly. :-)
 > 
 > Code and output attached.  I'm using Mnesia 3.6.
 > 
 > Cheers,
 > Luke
 > 
 > %%%----------------------------------------------------------------------
 > %%% File    : mnesia_deadlock.erl
 > %%% Author  : Luke Gorrie <>
 > %%% Purpose : Try to induce a lot of deadlocks into Mnesia to see if it'd
 > %%%           be useful to add "upgrade" locks.
 > %%% Created :  9 Aug 1999 by Luke Gorrie <>
 > %%%----------------------------------------------------------------------
 > 
 > -module(mnesia_deadlock).
 > -author('').
 > 
 > -record(test_rec, {id=1, date=now()}).
 > 
 > -export([init/0,run_test/1,cleanup/0, transaction/2]).
 > 
 > init() ->
 >     mnesia:create_table(test_rec,
 > 			[{attributes, record_info(fields, test_rec)}]),
 >     mnesia:transaction(fun() -> mnesia:write(#test_rec{}) end).
 > 
 > cleanup() ->
 >     mnesia:delete_table(test_rec).
 > 
 > run_test(N) ->
 >     Deadlocker =
 > 	fun() ->
 > 		mnesia:write(#test_rec{})
 > 	end,
 >     spawn_worker(N, Deadlocker),
 >     wait_responses(N).
 > 
 > spawn_worker(0, _F) ->
 >     ok;
 > spawn_worker(N, F) ->
 >     spawn_link(?MODULE, transaction, [self(), F]),
 >     %transaction(self(), F),
 >     spawn_worker(N-1, F).
 > 
 > wait_responses(0) ->
 >     ok;
 > wait_responses(N) ->
 >     receive
 > 	finished ->
 > 	    wait_responses(N-1)
 >     end.
 > 
 > transaction(Pid, F) ->
 >     {atomic, _} = mnesia:transaction(F),
 >     Pid ! finished.
 > 
 > 
 > ------------------------------------------------------------
 > 
 > Erlang (JAM) emulator version 47.4.1
 > 
 > Eshell V47.4.1  (abort with ^G)
 > ()1> c("/home/luke/devel/erlang/mnesia_deadlock", [{outdir, "/home/luke/devel/erlang/"}]).
 > {ok,mnesia_deadlock}
 > ()2> mnesia:start().
 > ok
 > ()3> mnesia_deadlock:init().
 > {atomic,ok}
 > ()4> mnesia_lib:set(debug, trace).
 > true
 > ()5> mnesia_deadlock:run_test(3).
 > Mnesia(''): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.92.0>,
 >                                                                                                  normal}
 > Mnesia(''): Restarting transaction {tid,16,<0.93.0>}: in 358ms {cyclic,'',{test_rec,1},write,write,{tid,15,<0.92.0>}}
 > Mnesia(''): Restarting transaction {tid,17,<0.94.0>}: in 359ms {cyclic,'',{test_rec,1},write,write,{tid,15,<0.92.0>}}
 > Mnesia(''): Restarting transaction {tid,17,<0.94.0>}: in 263ms {cyclic,'',{test_rec,1},write,write,{tid,16,<0.93.0>}}
 > Mnesia(''): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.93.0>,
 >                                                                                                  normal}
 > ok
 > Mnesia(''): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.94.0>,
 >                                                                                                  normal}
 > ()6> 

-- 
Dan Gudmundsson               Project:    Mnesia, Erlang/OTP
Ericsson Utvecklings AB       Phone:      +46  8 727 5762 
UAB/F/P                       Mobile:     +46 70 519 9469
S-125 25 Stockholm            Visit addr: Armborstv 1 




More information about the erlang-questions mailing list