From: Robert Klemme Date: 2006-03-12T02:58:43+09:00 Subject: Re: Threads and Deadlocks ------=_NextPart_000_0011_01C6453D.16860000 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit Mc Osten wrote: > Eric Hodel ha scritto: > >> There's nowhere to throw an exception to, all threads are blocked >> from running. > > What happens then? Is there any documentation about this (I'd rather > not to read directly the source, but if there is no other way...) I did some experimenting with the attached script. The trace actually seems to show a "raise" but none of the exception blocks catches anything. "ensure" is executed nevertheless. This is strange. Another strange thing is that "raise" does not show up if you uncomment those two lines that print the thread ids. > If I can somehow trap the event that makes the ruby interpreter > terminate I could obviously recover the deadlock, since blocked > resources would have been constructed to be recoverable. It is generally the better approach to avoid deadlocks programmatically. > Probably I am not able to explain what I'm looking for: this is not > real case programming. This are kind of teaching examples. > >> It is part of the interpreter and written in C. > > So I can't assume JRuby (for example) will act the same, can I? For these low level things you should not rely on similar behavior. And you also should not rely on deadlock detection. I guess the interpreter uses some kind of special mechanism. If you want to know what exactly happens here a look into the source code is probably the best. Kind regards robert ------=_NextPart_000_0011_01C6453D.16860000 Content-Type: application/octet-stream; name="th-deadlock.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="th-deadlock.rb" =0A= require 'thread'=0A= =0A= Thread.abort_on_exception =3D true=0A= =0A= $a =3D Mutex.new=0A= $b =3D Mutex.new=0A= =0A= set_trace_func lambda {|*a| p a}=0A= =0A= # puts "main: #{Thread.current.inspect}"=0A= =0A= Thread.new do=0A= # puts "thread: #{Thread.current.inspect}"=0A= begin=0A= $a.synchronize do=0A= Thread.pass=0A= $b.synchronize do=0A= puts "thread"=0A= end=0A= end=0A= rescue Exception =3D> e=0A= puts "caught in thread: #{e} (#{e.class})"=0A= ensure=0A= puts "thread terminating"=0A= end=0A= puts "END"=0A= end=0A= =0A= begin=0A= $b.synchronize do=0A= Thread.pass=0A= $a.synchronize do=0A= puts "main"=0A= end=0A= end=0A= rescue Exception =3D> e=0A= puts "caught in main: #{e} (#{e.class})"=0A= ensure=0A= puts "main terminating"=0A= end=0A= =0A= puts "END"=0A= ------=_NextPart_000_0011_01C6453D.16860000--