# BUG: "Mutual join" deadlock detection faulty in 1.8.6 and 1.8.7

**URL:** <https://www.ruby-forum.com/t/bug-mutual-join-deadlock-detection-faulty-in-1-8-6-and-1-8-7/168029>\
**Category:** Ruby\
**Created:** [May 15, 2009, 5:11am UTC](https://www.ruby-forum.com/t/bug-mutual-join-deadlock-detection-faulty-in-1-8-6-and-1-8-7/168029 "2009-05-15T05:11:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 15, 2009, 5:11am UTC](https://www.ruby-forum.com/t/bug-mutual-join-deadlock-detection-faulty-in-1-8-6-and-1-8-7/168029/1 "2009-05-15T05:11:52Z")

</div>

I tried submitting this at  
[Ruby Issue Tracking System](http://redmine.ruby-lang.org/projects/ruby-186/issues/new)  
but it said “Â« Mailing list Â» can’t be blank” but wouldn’t let me fill  
in anything in that field.

If a third thread is involved, the code in eval.c in function  
rb\_thread\_join0 to detect a mutual join dead lock is faulty.

This effects ruby 1.8.6 p368 and ruby 1.8.7 p 160 but not ruby 1.9.1 p  
129

What seems to be happening is a sequence like this…

main thread grabs mutex

thread two blocks waiting for mutex

thread one blocks waiting for mutex and th-\>join is set to the main  
thread.

the main thread unlocks, which wakes thread two. Thread two now has  
mutex, thread one is still blocked.

the main thread attempts to join thread one, rb\_thread\_join0 notes  
that thread one is in a WAIT\_JOIN waitfor state AND th-\>join still  
points at the main thread and (erroneously) reports deadlock.

ruby-1.8.7-p160/eval.c================================================  
rb\_thread\_join0(th, limit)  
rb\_thread\_t th;  
double limit;  
{  
enum rb\_thread\_status last\_status = THREAD\_RUNNABLE;

```
 if (rb_thread_critical) rb_thread_deadlock();
 if (!rb_thread_dead(th)) {

```

# if (th == curr\_thread) rb\_raise(rb\_eThreadError, “thread 0x%lx tried to join itself”, th-\>thread); if ((th-\>wait\_for & WAIT\_JOIN) && th-\>join == curr\_thread) rb\_raise(rb\_eThreadError, “Thread#join: deadlock 0x%lx - mutual join(0x%lx)”, curr\_thread-\>thread, th-\>thread);

Affected versions.

/opt/ruby/ruby-1.8.6-p368/bin/ruby -v  
ruby 1.8.6 (2009-03-31 patchlevel 368) [i686-linux]

/opt/ruby/ruby-1.8.7-p160/bin/ruby -v  
ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-linux]

Here is a test script to trigger it…  
=mutual\_join\_bug.rb============================================================  
require ‘thread’

m = Mutex.new  
m.lock  
wt2 = Thread.new do  
m.lock  
sleep 2  
m.unlock  
end

# Ensure wt2 is waiting on m

sleep 0.1

wt1 = Thread.new do  
m.lock  
m.unlock  
end

# Ensure wt1 is waiting on m

sleep 0.1

# Give it to wt2

m.unlock

# wt1.join

Output…  
/opt/ruby/ruby-1.8.7-p160/bin/ruby -w mutual\_join\_bug.rb  
mutual\_join\_bug.rb:24:in `join’: Thread#join: deadlock 0xb7daa1bc -  
mutual join(0xb7d9bc48) (ThreadError)  
from mutual\_join\_bug.rb:24

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 15, 2009, 10:49am UTC](https://www.ruby-forum.com/t/bug-mutual-join-deadlock-detection-faulty-in-1-8-6-and-1-8-7/168029/2 "2009-05-15T10:49:21Z")

</div>

Thanks, I’ve submitted this fault at the url you recommend.

Alas I get the same fault on the “new issue” page for 1.9.1

On Fri, 15 May 2009, Nobuyoshi N. wrote:

> [Ruby Issue Tracking System](http://redmine.ruby-lang.org/projects/ruby-18/issues/new) work.
> 
> –  
> Nobu Nakada

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [May 15, 2009, 7:46am UTC](https://www.ruby-forum.com/t/bug-mutual-join-deadlock-detection-faulty-in-1-8-6-and-1-8-7/168029/3 "2009-05-15T07:46:59Z")

</div>

Hi,

At Fri, 15 May 2009 12:10:53 +0900,  
John C. wrote in [ruby-talk:336622]:

> I tried submitting this at  
> [Ruby Issue Tracking System](http://redmine.ruby-lang.org/projects/ruby-186/issues/new)  
> but it said “« Mailing list » can’t be blank” but wouldn’t let me fill  
> in anything in that field.

[Ruby Issue Tracking System](http://redmine.ruby-lang.org/projects/ruby-187/issues/new) and  
[Ruby Issue Tracking System](http://redmine.ruby-lang.org/projects/ruby-18/issues/new) work.
