# Interesting problem with memcache-client and sockets

**URL:** <https://www.ruby-forum.com/t/interesting-problem-with-memcache-client-and-sockets/128536>\
**Category:** Ruby\
**Created:** [February 8, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/interesting-problem-with-memcache-client-and-sockets/128536 "2008-02-08T15:00:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakanapes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@Jakanapes](https://www.ruby-forum.com/u/Jakanapes)\
**Post date:** [February 8, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/interesting-problem-with-memcache-client-and-sockets/128536/1 "2008-02-08T15:00:52Z")

</div>

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

```

\n"

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

```

end

* * *

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?
    server.close
    raise MemCacheError, "No MemCache Server"
  end

```

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.
