Interesting problem with memcache-client and sockets

Hi all,

Been writing unit tests and found an interesting problem.

We’re using memcache on a web app and wanted to test that the proper
exceptions would be thrown if the memcache server went down.

If memcache server is started, then killed, then a set or a get is
called, the MemCacheError exception is always rescued.

However, if memcache server is started, a set and get is called, the
server is killed, then about half the time if a set is called, no
exception. If a get is called, always an exception.

I tracked it down to the set method in memcache.rb

def set(key, value, expiry = 0, raw = false)
raise MemCacheError, “Update of readonly cache” if @readonly
server, cache_key = request_setup key
socket = server.socket

value = Marshal.dump value unless raw
command = "set #{cache_key} 0 #{expiry} #{value.size}\r\n#{value}\r


  @mutex.lock if @multithread
  socket.write command
  result = socket.gets
  raise MemCacheError, $1.strip if result =~ /^SERVER_ERROR (.*)/
rescue SocketError, SystemCallError, IOError => err
  raise MemCacheError, err.message
  @mutex.unlock if @multithread


I found that in some cases socket.write command would throw the
appropriate IOError, but that sometimes it would return OK and result
would be ‘nil’

I added this code

  if result.nil?
    raise MemCacheError, "No MemCache Server"

and that throws my exception, but that seems pretty hackish and I’d
like to get a better understanding of the problem.

What’s interesting to me is that it only happens if a set was called
while the memcache server was up, then another called after the server
was stopped. It never happens if the memcache object has never
successfully done a set. Meanwhile, a get will always throw an
exception, though different ones ( connection reset by peer and lost
connection to localhost:11211 ) about equally.

memcache-client 1.5
ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-darwin8.11.1]

running on Mac OS 10.4

memcached 1.2.4

Any thoughts are appreciated.