Strange authsession error

I’ve been seeing periodic errors in my application that I’m not sure how to resolve. It seems to be an issue retrieving info from the Redis cache:

Zero length response, LastCommand:'GET urn:iauthsession:o8WnvFIFCIN80xjzJfyt', srcPort:51179

Unfortunately it’s causing error pages to be returned. I’m assuming the system is trying to retrieve session data from the cache using that session ID, the session info doesn’t exist, and Redis is returning nothing. I would expect that ServiceStack would simply create a new session at that point. Am I misunderstanding the scenario? Is there a way to catch/resolve this gracefully?

Errors where Redis is returning an unknown response is almost always the result of re-using a non-thread safe Redis Client instance across multiple threads. You should only be sharing any one of the Thread-Safe Client Managers across threads and each thread should resolve and return (aka Dispose) their own Redis Client instance, preferably in a using{} statement.

I’d suggest to look where you might be sharing the Redis Client instance e.g. in singleton or static instances or not disposing it properly i.e. not in a using{} statement.

If the issue persists we’ll need a stand-alone repro we can run locally to reproduce the issue.

These errors occur on first use, so no pre-existing session. Here’s how I’ve set up the cache in apphost.cs:

container.Register<IRedisClientsManager>(c => new RedisManagerPool(AppSettings.GetString("RedisConnectionString")));

container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());

The only other times I use anything cache related are either to set/get something from SessionBag, or in a service interface that saves items to the cache using Cache.Get<>. I don’t believe the latter is the issue as the keys there are generated using the UrnId.Create() method and don’t match the authsession keys that show up in the error.

If it is indeed something with the auth process or SessionBag, I’m not sure where I’d use using{}. Where can I look?

The Reids and Cache Registration is ok, but I wont be able to identify the issue without being able to reproduce it. The LastCommand in the Redis error is often not what’s the cause of the issue as by the time the error occurred the redis connection can already be corrupted. The only recommendation is to check all the places where you’re using non-thread safe instances to make sure you’re not saving them in a singleton or static variable so they’re not accessed by multiple threads.

Unfortunately that’s all I can say from here without being able to reproduce the error locally.

I guess the challenge is that I’m not explicitly using the Redis Client anywhere other than through the SessionBag and Cache objects exposed by the ServiceStackController and Service objects, neither of which should require a using{} wrapper, correct? The specific endpoint used here doesn’t even authenticate – it’s a publicly-accessible URL.

Are there any other auth-related processes that would use the Redis Client that I should check?

The first step to resolving any issue, is to isolate and reproduce it. If you can put together the steps to reproduce the issue than that’s something that can be investigated.

Other than that, it’s just trial and error, comment out code to try isolate which code is causing it.