# IO select. Reconnect procedure

**URL:** <https://rubytalk.org/t/io-select-reconnect-procedure/58467>\
**Category:** ruby-talk\
**Created:** [10 May 2010 13:44 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467 "2010-05-10T13:44:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walle\_Wallen](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@Walle\_Wallen](https://rubytalk.org/u/Walle_Wallen)\
**Post date:** [10 May 2010 13:44 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/1 "2010-05-10T13:44:22Z")

</div>

Hey, I have a minor problem with my IRC bot. It disconnects every while  
and then, often with a few days in between. My problem lies in the  
reconnecting procedure. The bot does not notice when it has been  
disconnected. I have tried Socket.closed?, time-out timer for the select  
IO. Nothing seems to work.

Any idea why?

//Walle

[/code]  
def loop()  
&nbsp;&nbsp;while !@connection.closed?  
&nbsp;&nbsp;&nbsp;&nbsp;ready = select([@connection, $stdin], nil, nil, 10)  
&nbsp;&nbsp;&nbsp;&nbsp;next if !ready  
&nbsp;&nbsp;&nbsp;&nbsp;ready[0].each do |message|  
&nbsp;&nbsp;&nbsp;&nbsp;if(message == $stdin)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return if $stdin.eof  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message = $stdin.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send(message)  
&nbsp;&nbsp;&nbsp;&nbsp;elsif(message == @connection)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return if @connection.eof  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message = @connection.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data\_response(message.strip)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;raise Errno::EPIPE  
&nbsp;&nbsp;end  
[/code]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dominic\_Rose](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@Dominic\_Rose](https://rubytalk.org/u/Dominic_Rose)\
**Post date:** [10 May 2010 15:35 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/2 "2010-05-10T15:35:47Z")

</div>

Maybe this will help.

class TCPSocket  
&nbsp;&nbsp;def off?  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eof  
&nbsp;&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kernel.puts "#{\_\_FILE\_\_}:#{\_\_LINE\_\_} #{$!.inspect}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

def loop  
&nbsp;&nbsp;until connection.off?  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [10 May 2010 17:20 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/3 "2010-05-10T17:20:36Z")

</div>

Walle Wallen wrote:

> Hey, I have a minor problem with my IRC bot. It disconnects every while  
> and then, often with a few days in between. My problem lies in the  
> reconnecting procedure. The bot does not notice when it has been  
> disconnected. I have tried Socket.closed?, time-out timer for the select  
> IO. Nothing seems to work.
> 
> Any idea why?

Maybe you have to detect when it sends you an empty string? (that  
usually means the socket has closed). If it's a bug report it though.  
-rp

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Walle\_Wallen](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@Walle\_Wallen](https://rubytalk.org/u/Walle_Wallen)\
**Post date:** [24 June 2010 16:14 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/4 "2010-06-24T16:14:08Z")

</div>

It still happens, but very rarely... The bot kicks in again if I write  
anything in the window($stdin).  
Does anyone have any thoughts on this?

//Walle

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [24 June 2010 17:57 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/5 "2010-06-24T17:57:42Z")

</div>

Walle Wallen wrote:

> It still happens, but very rarely... The bot kicks in again if I write  
> anything in the window($stdin).  
> Does anyone have any thoughts on this?

Yeah I think it's a generic "problem" of TCP. If the other side has  
closed the socket (or died, or been rebooted), and you never write  
anything to the socket, you will never learn it is closed until the next  
time you write something.

[http://en.wikibooks.org/wiki/Ruby\_Programming/Reference/Objects/Socket#keepalive](http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Socket#keepalive)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Walle\_Wallen](https://avatars.discourse-cdn.com/v4/letter/w/c89c15/32.png) [@Walle\_Wallen](https://rubytalk.org/u/Walle_Wallen)\
**Post date:** [25 June 2010 09:10 UTC](https://rubytalk.org/t/io-select-reconnect-procedure/58467/6 "2010-06-25T09:10:00Z")

</div>

Roger Pack wrote:

> Walle Wallen wrote:
> 
> > It still happens, but very rarely... The bot kicks in again if I write  
> > anything in the window($stdin).  
> > Does anyone have any thoughts on this?
> 
> Yeah I think it's a generic "problem" of TCP. If the other side has  
> closed the socket (or died, or been rebooted), and you never write  
> anything to the socket, you will never learn it is closed until the next  
> time you write something.
> 
> [Ruby Programming/Reference/Objects/Socket - Wikibooks, open books for an open world](http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Socket#keepalive)

Oh, okey.  
Thank you for the insight.

//Walle

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
