Note that when unlocking, you need to provide the CAS retrieved when getAndLocking. That’s how it knows the unlock comes from the same actor. You might double check that you’re doing that.
My understanding is that every time a document is modified, it gets a new CAS value. Therefore, to unlock, you would use the CAS from the sub-document upsert. Is that what you’re currently doing?
I saw your code and I think I know where the issue is.
So it looks like you first lock the document, then you do an update on the document by providing the CAS.
I believe this update with CAS operation would have already unlocked the document. So when you try to unlock the document again, you might be getting this error.
If you try locking the document, and then immediately unlocking, and let us know if this behaves correctly.