# DRb rescue: Shouldn't this be changed?

**URL:** <https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843>\
**Category:** Ruby\
**Created:** [February 12, 2008, 3:06am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843 "2008-02-12T03:06:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 3:06am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/1 "2008-02-12T03:06:54Z")

</div>

I’m a ruby newbie so I wanted to ask a forum about this before I emailed  
the author. The current drb/observer.rb file consists of this code:

require ‘observer’

module DRb  
module DRbObservable  
include Observable

```
def notify_observers(*arg)
  if defined? @observer_state and @observer_state

```

if defined? @observer\_peers  
for i in @observer\_peers.dup  
begin  
i.update(\*arg)  
rescue  
delete\_observer(i)  
end  
end  
end  
@observer\_state = false  
end  
end  
end  
end

My issue is with the rescue. I after a bunch of time trying to track  
down a bug I realized that I had a typo in an observer method which was  
causing the observer to be deleted.

My suggestion is to change the line to:

rescue RangeError

Which only rescues the error that the author was intending (I believe).

Am I missing anything here?

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 3:27am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/2 "2008-02-12T03:27:47Z")

</div>

On 11-Feb-08, at 6:06 PM, Mitch M. wrote:

> My suggestion is to change the line to:
> 
> rescue RangeError
> 
> Which only rescues the error that the author was intending (I  
> believe).
> 
> Am I missing anything here?

RangeError is generally used only to catch errors with a numerical  
range. I think what you’re intending here is probably IndexError  
however even that isn’t correct since

> for i in @observer\_peers.dup  
> begin  
> i.update(\*arg)  
> rescue  
> delete\_observer(i)  
> end  
> end

since I don’t believe a for…in loop can exceed the index in this  
case unless something else is modifying @observer\_peers at the same  
time…?

I confess the “for…in” looks mighty strange to me, as I find  
“@observer\_peers.each do |i|” much easier to read these days.

I’d hazard a guess, from that little chunk of code, that the author’s  
intention was not to catch bounding errors with the loop but rather to  
destroy any observers that were causing critical errors so as to not  
have to deal with them. From the sounds of what you describe, it was  
working as such as well, though silently destroying someone’s stuff  
seems just plain rude to me 🙂

These are all just guesses of course, as I’ve never used DRb and only  
seen the chunk of code you posted. Hope that help.

Regards  
Chris

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 4:38am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/3 "2008-02-12T04:38:22Z")

</div>

> RangeError is generally used only to catch errors with a numerical  
> range. I think what you’re intending here is probably IndexError  
> however even that isn’t correct since
> 
> > for i in @observer\_peers.dup  
> > begin  
> > i.update(\*arg)  
> > rescue  
> > delete\_observer(i)  
> > end  
> > end
> 
> since I don’t believe a for…in loop can exceed the index in this  
> case unless something else is modifying @observer\_peers at the same  
> time…?

I’ve spent some quality time with DRb this last week. Before I  
discovered drb/observer.rb I was trying to use the regular old  
observer.rb. What would happen is an object would build up listeners  
and, when a client would disconnect, I’d get a recycled object error.

I was trying to figure out how to get rid of those orphaned listeners.  
I found drb/observer and all it did was add a “rescue” and  
“delete\_observer” in the notify\_observers method, so I assumed this was  
the reasoning behind it.

I believe that the RangeError is the correct exception for an object  
recycled error (I found it in some random post). Please correct me if  
I’m wrong.

> I confess the “for…in” looks mighty strange to me, as I find  
> “@observer\_peers.each do |i|” much easier to read these days.

I like a little “for…in” every now in then. It’s more intuitive to me  
than those crazy blocks. Still, I use “each” for Ruby solidarity 🙂

> I’d hazard a guess, from that little chunk of code, that the author’s  
> intention was not to catch bounding errors with the loop but rather to  
> destroy any observers that were causing critical errors so as to not  
> have to deal with them.

I’d say that’s right. So, if I’m getting the author’s intent right, a  
little more specificity could save some headaches. Just hoping I could  
add a contribution after struggling with this.

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 5:52am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/4 "2008-02-12T05:52:50Z")

</div>

On 11-Feb-08, at 7:38 PM, Mitch M. wrote:

> > > end  
> > > I was trying to figure out how to get rid of those orphaned listeners.  
> > > I found drb/observer and all it did was add a “rescue” and  
> > > “delete\_observer” in the notify\_observers method, so I assumed this  
> > > was  
> > > the reasoning behind it.
> 
> I believe that the RangeError is the correct exception for an object  
> recycled error (I found it in some random post). Please correct me if  
> I’m wrong.

Ah I think I see now. Incidentally my knowledge of RangeError comes  
largely from this post by Matz:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/6576](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/6576)

however more googling indicates that your problem, and the use of  
RangeError does seem to be correct. I now think I get why you were  
doing:

```
  rescue
    delete_observer(i)
  end

```

in there. That seems as reasonable patch as any to me to accomplish  
what you’re trying to do.

My gut tells me that ending up at that point with a RangeError due to  
garbage collection issues per:

[http://www.megasolutions.net/ruby/drb-recycled-object-problem-42669.aspx](http://www.megasolutions.net/ruby/drb-recycled-object-problem-42669.aspx)

might indicate a problem further up the program that might need more  
detailing?

Hopefully someone with more knowledge of DRb and Ruby’s GC will weigh  
in and enlighten us both!

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 6:05am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/5 "2008-02-12T06:05:29Z")

</div>

> however more googling indicates that your problem, and the use of  
> RangeError does seem to be correct. I now think I get why you were  
> doing:
> 
> ```
> rescue
> delete_observer(i)
> end
> 
> ```
> 
> in there. That seems as reasonable patch as any to me to accomplish  
> what you’re trying to do.

Thanks for the additional information.

I just want to make sure that I was clear. The code:  
begin  
…  
rescue  
delete\_observer(i)  
end

is not my patch, but the code in the current release of DRb  
(drb/observer.rb). The patch I added in my code is:

```
   begin
   ...
   rescue RangeError
     delete_observer(i)
   end
```

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 22, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/6 "2008-02-22T21:02:24Z")

</div>

On Feb 11, 2008, at 18:06 PM, Mitch M. wrote:

> Which only rescues the error that the author was intending (I  
> believe).
> 
> Am I missing anything here?

What about when the remote DRb service crashes?

---

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [February 12, 2008, 5:59am UTC](https://www.ruby-forum.com/t/drb-rescue-shouldnt-this-be-changed/128843/7 "2008-02-12T05:59:16Z")

</div>

On 11-Feb-08, at 8:51 PM, Chris C. wrote:

> ```
> rescue
> delete_observer(i)
> end
> 
> ```
> 
> in there. That seems as reasonable patch as any to me to accomplish  
> what you’re trying to do.

That obviously should have been:
