# Socket timeout

**URL:** <https://www.ruby-forum.com/t/socket-timeout/119896>\
**Category:** Ruby\
**Created:** [November 12, 2007, 1:13am UTC](https://www.ruby-forum.com/t/socket-timeout/119896 "2007-11-12T01:13:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [November 12, 2007, 1:13am UTC](https://www.ruby-forum.com/t/socket-timeout/119896/1 "2007-11-12T01:13:04Z")

</div>

I am using a TCPSocket and I want the script to restart if the  
connection times out. I have tried doing it myself and searched around  
everywhere for a solution but I have had no luck… Any help would be  
great

tia

Lee

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [November 12, 2007, 1:26am UTC](https://www.ruby-forum.com/t/socket-timeout/119896/2 "2007-11-12T01:26:26Z")

</div>

On 11/11/07, Lee J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using a TCPSocket and I want the script to restart if the  
> connection times out. I have tried doing it myself and searched around  
> everywhere for a solution but I have had no luck… Any help would be  
> great

What do you mean by “the connection times out”? Do you mean that the  
connection has no read or write activity for some interval of time?

If you need to do that, then the Ruby/EventMachine library has that  
ability.  
You’ll need to rearrange your code to go this route, so perhaps someone  
else  
will have a less invasive solution. If not, then look at EventMachine.

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [November 12, 2007, 2:17am UTC](https://www.ruby-forum.com/t/socket-timeout/119896/3 "2007-11-12T02:17:52Z")

</div>

Francis C. wrote:

> What do you mean by “the connection times out”? Do you mean that the  
> connection has no read or write activity for some interval of time?  
> Yes, basically

> If you need to do that, then the Ruby/EventMachine library has that  
> ability.  
> You’ll need to rearrange your code to go this route, so perhaps someone  
> else  
> will have a less invasive solution. If not, then look at EventMachine.

Uh, I am editing an old program of mine which is quite large, about  
1.4k+ lines, so I am trying to do it the least invasive way possible… I  
appreciate your input so quickly though. Perhaps this is the only route.

Lee

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [November 12, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/socket-timeout/119896/4 "2007-11-12T15:18:19Z")

</div>

Lee J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

Q\> Francis C. wrote:

> > What do you mean by “the connection times out”? Do you mean that the  
> > connection has no read or write activity for some interval of time?  
> > Yes, basically

You can adjust the SO\_RCVTIMEO and SO\_SNDTIMEO socket options using  
Socket#{get,set}sockopts.

However, they are susceptible to trickle attack. For example, if you  
set the receive timeout to 30 seconds, then the sender can send just  
one packet every 30 seconds, tying up resources on your end.

The proper solution requires application-level enforcement of  
timeout. You can use the timeout library for the least invasive  
mechanism to even changing the core mechanism to Ruby/EventMachine.

YS.

---

<div class="post-metadata">

**Author:** ![jarvo88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a8b319/32.png) [@jarvo88](https://www.ruby-forum.com/u/jarvo88)\
**Post date:** [November 12, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/socket-timeout/119896/5 "2007-11-12T20:20:58Z")

</div>

If you’re not worried about scaling then  
Timeout::timeout(30) {  
do stuff }

or  
Thread.new {  
sleep 30  
if hasnt\_done\_anything  
raise on it # scary!  
end  
}

maybe 🙂

Lee J. wrote:

> I am using a TCPSocket and I want the script to restart if the  
> connection times out. I have tried doing it myself and searched around  
> everywhere for a solution but I have had no luck… Any help would be  
> great
> 
> tia
> 
> Lee
