kgio RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <>
Subject: Re: handling Timeout.timeout with sockets
Date: Thu, 3 May 2012 07:10:01 +0000	[thread overview]
Message-ID: <> (raw)

Mike Perham <> wrote:
> I've noticed that my code cannot rescue the Timeout::Error that is
> raised and I expect that is by design.

Yes, it's by design.  Reading timeout.rb (and its
"git log -p --full-diff" history) is enlightening, btw.   You'll also
get better performance if you explicitly specify a class for timeout
to raise (ref:

> However I'm wondering how my network client can recover from that
> timeout?  Presumably there is now data to be read on the socket which,
> due to the timeout, has not been read, meaning my protocol state
> machine is now out of sync, thus leading to the crash.
> Should I drain the socket somehow before each operation?  What is the
> best way to do this?

The easiest way is to open a new socket.

The other way is to _completely_ parse the old response (that timed out)
before reusing the socket.

Do _not_ rely on IO#nread in the io/wait module for this, it's
inherently racy: bytes may arrive after you call IO#nread.  Unless the
protocol changed recently, for memcached (and nearly everything
implemented on top of TCP), you must completely parse the response the
server gives you.

      reply	other threads:[~2012-05-03  7:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <>
2012-05-03  4:55 ` handling Timeout.timeout with sockets Mike Perham
2012-05-03  7:10   ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).