# Monitor Error when Joining Threads

**URL:** <https://rubytalk.org/t/monitor-error-when-joining-threads/47932>\
**Category:** ruby-talk\
**Created:** [25 July 2008 15:44 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932 "2008-07-25T15:44:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)\
**Post date:** [25 July 2008 15:44 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932/1 "2008-07-25T15:44:09Z")

</div>

Hello all,

\*Note: this may get published twice... I posted via the email list but  
it never went through...\*

I'm getting the following error when I I try to join some threads:

c:/ruby/lib/ruby/1.8/monitor.rb:215:in `mon_enter': undefined method `+'  
for nil:NilClass (NoMethodError)  
&nbsp;&nbsp;&nbsp;&nbsp;from simulator2.rb:48:in `join'  
&nbsp;&nbsp;&nbsp;&nbsp;from simulator2.rb:48:in `init'  
&nbsp;&nbsp;&nbsp;&nbsp;from simulator2.rb:48:in `each'  
&nbsp;&nbsp;&nbsp;&nbsp;from simulator2.rb:48:in `init'  
&nbsp;&nbsp;&nbsp;&nbsp;from simulator2.rb:163

For some reason @mon\_count within monitor.rb is nil, but I'm not sure  
why any type of synchronization is being done at this point. Does  
Thread do some synchronization when join is called?

Below is the code from simulator2.rb on line 48 (along with other  
potentially informative code):

threads = Array.new  
outages.each do |range|  
&nbsp;&nbsp;@logger.debug "New thread -- Start: #{range.first}, Finish:  
#{range.last}"  
&nbsp;&nbsp;threads \<\< Thread.new do  
&nbsp;&nbsp;&nbsp;&nbsp;Simulator2.new(@file, @interface,  
"c:\log-#{threads.length}.txt").run(range.first, range.last)  
&nbsp;&nbsp;end  
end  
threads.each { |thread| thread.join }

I am doing some synchronization of methods and variables within the  
Simulator2#run method... but would this show up as coming from the  
Thread#join method?

Just for clarification, yes I am creating a new instance of Simulator2  
(which is the class which this code resides in). The code above resides  
in Simulator2#init.

> **···**
>
> --  
> Thanks!  
> Bryan  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)\
**Post date:** [25 July 2008 16:22 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932/2 "2008-07-25T16:22:53Z")

</div>

All,

So this doesn't have anything to do with Thread.join and such... it  
instead has to do with some synchronization I'm doing in another class.  
Here's what it looks like:

class StatsTracker \< Monitor  
&nbsp;&nbsp;include Singleton  
&nbsp;&nbsp;...  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;...  
end

I get an error at the 'synchronize do' line similar to what I describe  
in my first post, essentially saying @mon\_count is nil. I tried  
including MonitorMixin vs extending Monitor, but that didn't fix the  
problem. I then tried doing the following and it worked:

class StatsTracker  
&nbsp;&nbsp;include Singleton  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@semaphore = Mutex.new  
&nbsp;&nbsp;end  
&nbsp;&nbsp;...  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;@semaphore.synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;...  
end

Does anyone know what I'm doing wrong to make the Monitor approach not  
work?

> **···**
>
> --  
> Thanks!  
> Bryan  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeff\_Shantz](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@Jeff\_Shantz](https://rubytalk.org/u/Jeff_Shantz)\
**Post date:** [14 May 2009 09:58 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932/3 "2009-05-14T09:58:06Z")

</div>

I realize this is an old question, but I'm replying in case anyone else  
encounters this error, since I just found the fix to it.

You need to make a call to super() in your constructor.

So, the following:

> class StatsTracker \< Monitor  
> &nbsp;&nbsp;include Singleton  
> &nbsp;&nbsp;...  
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;...  
> end

should be changed to:

class StatsTracker \< Monitor  
include Singleton

&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;...  
end

The same is true if you're using MonitorMixin. For instance,

class StatsTracker  
include Singleton  
include MonitorMixin

&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super()  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;synchronize do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;...  
end

Hope that helps someone.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [14 May 2009 11:37 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932/4 "2009-05-14T11:37:49Z")

</div>

Just an additional note: the reason why the exception appears to come  
out of Thread.join is this:

irb(main):004:0\> t = Thread.new { raise ArgumentError, "test" }  
=\> #\<Thread:0x100ea960 run\>  
irb(main):005:0\> t.join  
ArgumentError: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `block in irb\_binding'  
irb(main):006:0\>

In other words: since the exception is not caught in the thread,  
thread exits but the exception survives. When you do the join, it  
will come to haunt you. This does make sense when you consider  
Thread.value:

irb(main):006:0\> t = Thread.new { "result" }  
=\> #\<Thread:0x100e0b54 run\>  
irb(main):007:0\> t.value  
=\> "result"  
irb(main):008:0\> t = Thread.new { raise ArgumentError, "test" }  
=\> #\<Thread:0x10016aac run\>  
irb(main):009:0\> t.value  
ArgumentError: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):8:in `block in irb\_binding'  
irb(main):010:0\>

Kind regards

robert

> **···**
>
> 2009/5/14 Jeff Shantz \<rubyforum@jeffshantz.com\>:
> 
> > I realize this is an old question, but I'm replying in case anyone else  
> > encounters this error, since I just found the fix to it.
> > 
> > You need to make a call to super() in your constructor.
> > 
> > So, the following:
> > 
> > > class StatsTracker \< Monitor  
> > > include Singleton  
> > > ...  
> > > def foo  
> > > synchronize do  
> > > # do something  
> > > end  
> > > end  
> > > ...  
> > > end
> > 
> > should be changed to:
> > 
> > class StatsTracker \< Monitor  
> > include Singleton
> > 
> > def initialize  
> > super()  
> > end
> > 
> > def foo  
> > synchronize do  
> > # do something  
> > end  
> > end  
> > ...  
> > end
> > 
> > The same is true if you're using MonitorMixin. For instance,
> > 
> > class StatsTracker  
> > include Singleton  
> > include MonitorMixin
> > 
> > def initialize  
> > super()  
> > end
> > 
> > def foo  
> > synchronize do  
> > # do something  
> > end  
> > end  
> > ...  
> > end
> > 
> > Hope that helps someone.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Michael\_G](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Michael\_G](https://rubytalk.org/u/Michael_G)\
**Post date:** [12 October 2010 17:36 UTC](https://rubytalk.org/t/monitor-error-when-joining-threads/47932/5 "2010-10-12T17:36:29Z")

</div>

Jeff Shantz wrote in post #816096:

> I realize this is an old question, but I'm replying in case anyone else  
> encounters this error, since I just found the fix to it.
> 
> You need to make a call to super() in your constructor.

Jeff, Thanks for taking the time to record the solution. I really  
appreciated it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
