NamedCache cannot refresh lock using AcquireLock from a different thread

#1
Hello,

I'm using .NET 4.7.2, soss_svcdotnet 5.0.0.0, and soss_namedchache 6.0.0.0. Per documentation, calling AcquireLock(string) "
on an instance of the NamedCache that already has a lock on the cached object will reset the 90 second lock timeout." However, when I use the same NamedCache instance in a new thread, I get "Soss.Client.ObjectLockedException: 'Unable to acquire lock while retrieving object. Object Key: TEST'". This exception is thrown when the object is locked by another "client". How is the same instance of NamedCache considered a different client?

See sample code below
C#:
const string TEST_KEY = "TEST";
var nc = CacheFactory.GetCache("Scenario2");
nc.Clear();
nc.MaxLockAttempts = 1;
nc.Insert(TEST_KEY, "blah", new CreatePolicy(), false, false);
nc.AcquireLock(TEST_KEY);

new Thread(() =>
{
    nc.AcquireLock(TEST_KEY);
}).Start();
 

markw

Administrator
Staff member
#2
Hi Caleb,

Great question. In the NamedCache library, locks are held on a per-thread basis. (Behind the scenes, the API holds on to a lock ticket for you in thread-local storage.) The idea is that locks can provide consistency across any number of clients, processes, and threads.

FYI, the newer Scaleout.Client library uses a different design to better support async programming (it's possible for an async method to continue on a different thread after awaiting). Locking calls in Scaleout.Client explicitly return a lock token that callers must provide to subsequent operations on the object (GetExclusiveLockAsync, for example). You could conceivably share that token across threads/tasks (but be very, very careful since you could easily undermine the consistency that a lock provides).
 
Last edited:
Top