# Net::HTTP timeouts not respected depending on how it's used?

**URL:** <https://www.ruby-forum.com/t/net-http-timeouts-not-respected-depending-on-how-its-used/134209>\
**Category:** Ruby\
**Created:** [April 8, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/net-http-timeouts-not-respected-depending-on-how-its-used/134209 "2008-04-08T21:42:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@xavi](https://www.ruby-forum.com/u/xavi)\
**Post date:** [April 8, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/net-http-timeouts-not-respected-depending-on-how-its-used/134209/1 "2008-04-08T21:42:28Z")

</div>

In my Mac with Leopard, this doesn’t respect the 2 seconds timeout  
that’s set in the code…

require ‘net/http’  
require ‘uri’

url = URI.parse(“[http://222.222.3.234](http://222.222.3.234)”)  
req = Net::HTTP::Get.new(url.request\_uri)  
res = Net::HTTP.start(url.host, url.port) { |http|  
http.open\_timeout = 2  
http.read\_timeout = 2  
http.request(req)  
}  
puts res.body

It times out, but after 1 MINUTE, with this message…  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:564:in  
`initialize’: Operation timed out - connect(2) (Errno::ETIMEDOUT)

However, the following code works (i.e. it times out after the 2  
seconds)…

require ‘net/http’

site = Net::HTTP.new(“[http://222.222.3.234](http://222.222.3.234)”)  
site.open\_timeout = 2  
site.read\_timeout = 2  
res = site.get2(URI.escape(“/”))  
puts res.body

After the 2 seconds I get this message  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:54:in  
`open’: execution expired (Timeout::Error)

Do you know why the timeout is not respected in the first case? (I don’t  
understand)

Thanks in advance.

Xavi

---

<div class="post-metadata">

**Author:** ![xavi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@xavi](https://www.ruby-forum.com/u/xavi)\
**Post date:** [April 8, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/net-http-timeouts-not-respected-depending-on-how-its-used/134209/2 "2008-04-08T23:00:28Z")

</div>

On Apr 8, 2008, at 21:42 , Xavi C. wrote:

> http.read\_timeout = 2  
> http.request(req)  
> }  
> puts res.body
> 
> It times out, but after 1 MINUTE, with this message…  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/  
> 1.8/net/http.rb:564:in  
> `initialize’: Operation timed out - connect(2) (Errno::ETIMEDOUT)

Ei Xavi!

I’ve traced the code:

Class method start instantiates a new http object  
and invokes start on it

Instance method start calls do\_start, which in turn  
invokes connect, and _then_ yields

So by the time the block is executed the connection has already been  
attempted with default (nil) open timeout value.

With the current implementation you can’t tweak the open timeout and  
use the block form, you need to go with the alternative you already  
know it works.

– fxn
