# Trouble with timeout

**URL:** <https://www.ruby-forum.com/t/trouble-with-timeout/189384>\
**Category:** Ruby\
**Created:** [May 31, 2010, 12:27am UTC](https://www.ruby-forum.com/t/trouble-with-timeout/189384 "2010-05-31T00:27:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/898d66/32.png) [@lukich](https://www.ruby-forum.com/u/lukich)\
**Post date:** [May 31, 2010, 12:27am UTC](https://www.ruby-forum.com/t/trouble-with-timeout/189384/1 "2010-05-31T00:27:43Z")

</div>

Hi. I have a small script that I use to download archives from the web.  
The code looks like this:

require ‘rubygems’  
require ‘mechanize’

def downloader()  
agent = WWW::Mechanize.new

# create agent object

page = agent.get(‘[http://www.myurl.com](http://www.myurl.com)’)

#authenticate  
form = page.forms.first  
form.username = ‘####’  
form.password = ‘####’

#submit form  
page = agent.submit form

#grab all links that have zip in them  
mylinks = page.links\_with(:href =\> /zip//)

#for each link download and rename  
mylinks.each do |archive|

```
name = archive.href.split('zip/')[1].sub('/','').strip

puts "Archive #{name} is saving..."

myfile = agent.click(archive)

output = File.open("/Users/luka/Desktop/#{name}.zip", 'w') {|file|

```

file \<\< myfile.body }

```
puts "Done!"

```

end  
end

begin  
downloader  
rescue Timeout::Error  
puts ‘Timeout was detected. Trying again…’  
downloader  
end

However, I’ve been having problems with timeouts from time to time, so I  
ended up making this code into a function and upon timeout invoke this  
function once again, as you can see at the bottom of the code. However,  
I still get an error, which looks like this on timeout:

Archive 914188 is saving…  
Timeout was detected. Trying again…  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:60:in  
`rbuf\_fill’: execution expired (Timeout::Error)

So it tries to rescue the timeout but obviously calling the same  
function again is a bad idea. I was wondering why is it, and what would  
be a proper solution.  
Thanks!

Luka

---

<div class="post-metadata">

**Author:** ![lukich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/898d66/32.png) [@lukich](https://www.ruby-forum.com/u/lukich)\
**Post date:** [June 1, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/trouble-with-timeout/189384/2 "2010-06-01T16:50:51Z")

</div>

begin  
downloader  
rescue Timeout::Error  
puts ‘Timeout was detected. Trying again…’  
retry  
end

---

<div class="post-metadata">

**Author:** ![lukich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/898d66/32.png) [@lukich](https://www.ruby-forum.com/u/lukich)\
**Post date:** [June 1, 2010, 6:25pm UTC](https://www.ruby-forum.com/t/trouble-with-timeout/189384/3 "2010-06-01T18:25:02Z")

</div>

I’ll try it out, thanks!

Walle W. wrote:

> begin  
> downloader  
> rescue Timeout::Error  
> puts ‘Timeout was detected. Trying again…’  
> retry  
> end
