# Net::HTTP Timeout

**URL:** <https://www.ruby-forum.com/t/net-http-timeout/124721>\
**Category:** Ruby\
**Created:** [January 1, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/net-http-timeout/124721 "2008-01-01T19:50:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thefed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@thefed](https://www.ruby-forum.com/u/thefed)\
**Post date:** [January 1, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/net-http-timeout/124721/1 "2008-01-01T19:50:29Z")

</div>

Yes, I have read the previous thread ([http://www.ruby-forum.com/topic/](http://www.ruby-forum.com/topic/)  
105212), but I have one small question:

Would it be so terribly bad to change it from:

> > def rbuf\_fill  
> > timeout(@read\_timeout) {  
> > @rbuf \<\< @io.sysread(1024)  
> > }  
> > end

to:

> > def rbuf\_fill  
> > Thread.new {  
> > @rbuf \<\< @io.sysread(1024)  
> > }  
> > end

? Would the uncapturable thread cause me a problem? Timeout runs it  
in another thread anyways, so this is just removing the timeout aspect.

-------------------------------------------------------|  
~ Ari  
seydar: it’s like a crazy love triangle of Kernel commands and C code

---

<div class="post-metadata">

**Author:** ![thefed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@thefed](https://www.ruby-forum.com/u/thefed)\
**Post date:** [January 2, 2008, 4:34am UTC](https://www.ruby-forum.com/t/net-http-timeout/124721/2 "2008-01-02T04:34:49Z")

</div>

This is a terrible idea. Starting in another thread causes rbuf\_fill  
to return immediately instead of waiting to fill the buffer. The use  
of timeout causes it to wait a while trying, and then abort if it  
fails. Using a thread without a timeout will completely monkey with  
things.

Is there some reason as to why you want to do this? What are you  
trying to accomplish?

-Bryan

---

<div class="post-metadata">

**Author:** ![thefed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@thefed](https://www.ruby-forum.com/u/thefed)\
**Post date:** [January 2, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/net-http-timeout/124721/3 "2008-01-02T22:55:54Z")

</div>

On Jan 1, 2008, at 10:34 PM, Bryan D. wrote:

> This is a terrible idea. Starting in another thread causes  
> rbuf\_fill to return immediately instead of waiting to fill the  
> buffer. The use of timeout causes it to wait a while trying, and  
> then abort if it fails. Using a thread without a timeout will  
> completely monkey with things.
> 
> Is there some reason as to why you want to do this? What are you  
> trying to accomplish?

When I’m using ruby to download large files, everynow and then the  
server doesn’t respond for over 60 seconds, and my program crashes  
with a timeout error.

---

<div class="post-metadata">

**Author:** ![thefed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@thefed](https://www.ruby-forum.com/u/thefed)\
**Post date:** [January 3, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/net-http-timeout/124721/4 "2008-01-03T19:06:37Z")

</div>

On Wednesday 02 January 2008, thefed wrote:

> > Is there some reason as to why you want to do this? What are you  
> > trying to accomplish?
> 
> When I’m using ruby to download large files, everynow and then the  
> server doesn’t respond for over 60 seconds, and my program crashes  
> with a timeout error.

It seems to me that your program, or whatever is using Net::HTTP should  
handle the timeout better, rather than crashing. You can always wrap  
the call in something that rescues the timeout error and retries  
(perhaps after printing out a warning), or you could ask the user what  
to do. Not timing out at all isn’t the best approach.

Ben

---

<div class="post-metadata">

**Author:** ![thefed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@thefed](https://www.ruby-forum.com/u/thefed)\
**Post date:** [January 11, 2008, 4:22am UTC](https://www.ruby-forum.com/t/net-http-timeout/124721/5 "2008-01-11T04:22:13Z")

</div>

Ben G. wrote:

> On Wednesday 02 January 2008, thefed wrote:
> 
> > > Is there some reason as to why you want to do this? What are you  
> > > trying to accomplish?
> > 
> > When I’m using ruby to download large files, everynow and then the  
> > server doesn’t respond for over 60 seconds, and my program crashes  
> > with a timeout error.
> 
> It seems to me that your program, or whatever is using Net::HTTP should  
> handle the timeout better, rather than crashing. You can always wrap  
> the call in something that rescues the timeout error and retries  
> (perhaps after printing out a warning), or you could ask the user what  
> to do. Not timing out at all isn’t the best approach.
> 
> Ben

The crash is caused by the default behavior of timeouts (which is what  
Net::HTTP uses). By default timeouts throw an InterruptException which  
kills the current thread. You can get a simple fix for this with the  
http\_configuration gem/plugin  
([http://agilewebdevelopment.com/plugins/http\_configuration](http://agilewebdevelopment.com/plugins/http_configuration)).
