# How to control timeout exceptions

**URL:** <https://www.ruby-forum.com/t/how-to-control-timeout-exceptions/172566>\
**Category:** Ruby\
**Created:** [July 23, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-control-timeout-exceptions/172566 "2009-07-23T12:55:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![junaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@junaid](https://www.ruby-forum.com/u/junaid)\
**Post date:** [July 23, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-control-timeout-exceptions/172566/1 "2009-07-23T12:55:23Z")

</div>

This is code that i am using for fetching updates from twitter. I used  
SystemTimer gem from  
thoughtwork([http://ph7spot.com/articles/system\_timer](http://ph7spot.com/articles/system_timer)) to ensure that we  
don’t get anymore timeout exception and crashed page.As article  
([http://ph7spot.com/articles/system\_timer](http://ph7spot.com/articles/system_timer)) points out weaknesses of  
timeout.rb implemented in ruby 1.8. We are using ruby 1.8 in production.  
After system\_timer we also handle “rescue Twitter::Error” but they also  
didn’t work. I know we can also option to configure timeout in the http  
client library but i think these both should work fine.

We are using rails 2.2.2 and ruby 1.8.6. Here is my code.

TWITTER\_SERVICE\_TIMEOUT = 25s

begin  
SystemTimer.timeout(TWITTER\_SERVICE\_TIMEOUT) do  
c = Twitter::Client.new(:login =\>  
profile\_twitter.email,:password =\> profile\_twitter.password)  
my\_timeline = c.timeline\_for(:me, :since =\> 30.minutes.ago)  
end  
rescue Twitter::RESTError =\> re

```
rescue Timeout::Error
  #my_timeline = nil

```

end

Exception i got mostly

/usr/lib/ruby/1.8/net/http.rb:560:in `initialize' /usr/lib/ruby/1.8/net/http.rb:560:in `open’  
/usr/lib/ruby/1.8/net/http.rb:560:in `connect' /usr/lib/ruby/1.8/timeout.rb:53:in `timeout’  
/usr/lib/ruby/1.8/timeout.rb:93:in `timeout' /usr/lib/ruby/1.8/net/http.rb:560:in `connect’  
/usr/lib/ruby/1.8/net/http.rb:553:in `do_start' /usr/lib/ruby/1.8/net/http.rb:542:in `start’  
/usr/lib/ruby/gems/1.8/gems/twitter4r-0.3.0/lib/twitter/client/base.rb:9:in  
`http_connect' /usr/lib/ruby/gems/1.8/gems/twitter4r-0.3.0/lib/twitter/client/timeline.rb:66:in `timeline\_for’

---

<div class="post-metadata">

**Author:** ![junaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a9adbd/32.png) [@junaid](https://www.ruby-forum.com/u/junaid)\
**Post date:** [July 23, 2009, 1:49pm UTC](https://www.ruby-forum.com/t/how-to-control-timeout-exceptions/172566/2 "2009-07-23T13:49:50Z")

</div>

Maybe you want to check out Terminator? http://rubyforge.org/frs/?group\_id=1024&release\_id=26322 or http://codeforpeople.com/lib/ruby/terminator/

I cannot make promises but Ara is probably _the_ guy to come up with a  
solution for that kind of problem.

## HTH Robert

Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exupéry]
