# Bug involving condition variables

**URL:** <https://www.ruby-forum.com/t/bug-involving-condition-variables/142984>\
**Category:** Ruby\
**Created:** [July 14, 2008, 8:19am UTC](https://www.ruby-forum.com/t/bug-involving-condition-variables/142984 "2008-07-14T08:19:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 14, 2008, 8:19am UTC](https://www.ruby-forum.com/t/bug-involving-condition-variables/142984/1 "2008-07-14T08:19:39Z")

</div>

The following code generates a ThreadError (“not owner”, raised in  
unlock\_mutex\_inner()). However, the ThreadError never gets reported at  
the top level (even though my code is re-raising it and  
Thread.abort\_on\_exception = true). A ThreadError that I raise directly  
gets reported normally.

For 1.8.6-p114 (Linux), the output is:

#\<ThreadError: not owner\>

For 1.8.4, there is no output (so it looks like this difference has  
something to do with the thread rewrite in 1.8.6).

If it doesn’t look like there’s some obvious stupidity, I’ll file a bug.

* * *

require ‘thread’

Thread.abort\_on\_exception = true

class MyQueue  
def initialize  
@q = []  
@mutex = Mutex.new  
@cond = ConditionVariable.new  
end

def push(obj)  
@mutex.synchronize do  
@q \<\< obj  
@cond.signal  
end  
end

def pop  
@mutex.synchronize do  
if (last=@q.pop)  
return last  
end

```
   loop do
     @cond.wait(@mutex)
     if (last=@q.pop)
       return last
     end
   end
 end

```

end  
end

queue = MyQueue.new

def wait(queue)  
#raise ThreadError, “foo bar”  
# why does this behave differently than  
# the “not owner” ThreadError ?  
queue.pop  
queue.pop

# without the rescue clause the program has no exceptions–why?

rescue ThreadError =\> ex  
p ex  
raise ex # Why no exception reported at top level?  
end

Thread.new {wait(queue)}

sleep 0.01

Thread.new do  
queue.push “foobar”  
end

sleep 0.01

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 14, 2008, 8:31am UTC](https://www.ruby-forum.com/t/bug-involving-condition-variables/142984/2 "2008-07-14T08:31:21Z")

</div>

Same problem on OCI windows:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 14, 2008, 11:18am UTC](https://www.ruby-forum.com/t/bug-involving-condition-variables/142984/3 "2008-07-14T11:18:57Z")

</div>

Hi,

At Mon, 14 Jul 2008 15:12:16 +0900,  
Joel VanderWerf wrote in [ruby-talk:308103]:

> The following code generates a ThreadError (“not owner”, raised in  
> unlock\_mutex\_inner()). However, the ThreadError never gets reported at  
> the top level (even though my code is re-raising it and  
> Thread.abort\_on\_exception = true). A ThreadError that I raise directly  
> gets reported normally.
> 
> For 1.8.6-p114 (Linux), the output is:
> 
> #\<ThreadError: not owner\>

It seems like a bug of ext/thread in 1.8.6, and 1.8.7 doesn’t  
have it.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [July 14, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/bug-involving-condition-variables/142984/4 "2008-07-14T19:44:01Z")

</div>

Nobuyoshi N. wrote:

> > For 1.8.6-p114 (Linux), the output is:
> > 
> > #\<ThreadError: not owner\>
> 
> It seems like a bug of ext/thread in 1.8.6, and 1.8.7 doesn’t  
> have it.

I filed a bug report (#275), in case this can be fixed in 1.8.6 for  
those of us who are timid about 1.8.7.
