# Timeout not obeyed when trying to open bad url

**URL:** <https://www.ruby-forum.com/t/timeout-not-obeyed-when-trying-to-open-bad-url/137866>\
**Category:** Rails\
**Created:** [May 17, 2008, 1:16am UTC](https://www.ruby-forum.com/t/timeout-not-obeyed-when-trying-to-open-bad-url/137866 "2008-05-17T01:16:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![histrionics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@histrionics](https://www.ruby-forum.com/u/histrionics)\
**Post date:** [May 17, 2008, 1:16am UTC](https://www.ruby-forum.com/t/timeout-not-obeyed-when-trying-to-open-bad-url/137866/1 "2008-05-17T01:16:45Z")

</div>

Here is something Ive never seen before:

I have a list of urls fed into mechanize (which uses net/http to grab  
pages)

I have it setup as thus:  
require ‘mechanize’  
agent = WWW::Mechanize.new;  
error\_count = 0  
begin  
Timeout::timeout(2) {  
@tracked\_page = agent.get(“http://#{site\_url}”)  
}  
rescue Timeout::Error =\> timeout\_error  
puts “I TIMED OUT AFTER 2 SECS BUT IM TRYING AGAIN:  
#{timeout\_error}”  
error\_count += 1  
if error\_count \< 5  
puts “ATTEMPT NUMBER #{@error\_count} QUITTING AFTER 4 TRIES”  
retry  
end  
end

This is all well and good, it works fine and catches any timeout  
exceptions, except when its trying to deal with one particular URL  
([www.webdevking.com](http://www.webdevking.com)).

This URL is not currently resolving to any host. it returns “unknown  
host” when you try to connect to it.

For some reason, when I run this code on the url: [www.webdevking.com](http://www.webdevking.com)  
it hangs for upwards of 30 seconds. For some reason, it fails to obey  
the Timeout of two seconds. I’ve tried setting the mechanize timeout:  
agent.open\_timeout and agent.read\_timeout, but these aren’t obeyed  
either. The timeout is obeyed for every URL but that one.

I would like to get to the root of this problem, because a 30 second  
slowdown when process a batch of urls is unacceptable.

Any ideas?!?!

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![histrionics](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@histrionics](https://www.ruby-forum.com/u/histrionics)\
**Post date:** [May 17, 2008, 2:33am UTC](https://www.ruby-forum.com/t/timeout-not-obeyed-when-trying-to-open-bad-url/137866/2 "2008-05-17T02:33:59Z")

</div>

On 17 May 2008, at 00:16, histrionics wrote:

> Here is something Ive never seen before:

I’ve seen this before. Bottom line is, ruby’s threading sucks, and  
when running c code (extensions or bits of the stdlib that call  
through to c code) you can block the entire ruby interpreter. Things  
that can do this include mysql queries, some parts of name resolving,  
and it would appear some other bits of the networking libraries.

Fred
