enyim/EnyimMemcached — Issue #94: Wrong magic value exception
Author: vchekan
Created: 2012-01-11 00:45:59+00:00
State: open

Sometimes we see an error: "Expected magic value 129, received:". I have to open a new ticket instead of reopening the old one: https://github.com/enyim/EnyimMemcached/issues/66.

The cause of this error is the following scenario:
- Large data is receiving from memcached server (~1Mb)
- While data reading is not complete yet, an exception happen (for examlple, ThreadAbortException is normal thing in Web based applications)
- As long as exception is not IOException, PooledSocket is not marked as IsAlive=false and is returned into pool with dirty buffer
- When this socket is reused, it is Reset and as many as Avialibe bytes are read and discard from the socket buffer. But if socket is reused quickly enough, due to network latencies and large data size (1Mb value), data may still be coming into even after Reset call. So there is no way to guarantee that Reset will really clean up buffer because we do not know when data will stop coming in
- Data which keeps coming from previous request are read as a header and magic value corruption is detected.

The fix is here:
https://github.com/vchekan/EnyimMemcached/commit/cb44bdf61066f812bc2257d1aaf6d2c879fa9ec2
and it introduces a new state for PooledSoket: recoverable error. It means that socket itself is messed up and should be disposed, but server is fine and there is no need to mark server as dead.
Another change is that now any exception will lead to server "dead" state, not just IOException.