Couldn't lock exception even with LOCKMODE_WAIT passed?

Hi, I’m a bit stuck and I’d like some help. I have get an error when executing a query at a close interval:

  File "/Users/myUser/project/api/venv/lib/python3.8/site-packages/couchbase_core/n1ql.py", line 415, in _submit_query
    return self._parent._n1ql_query(self._params.encoded,
couchbase.exceptions.ObjectThreadException: <Couldn't lock. If LOCKMODE_WAIT was passed, then this means that something has gone wrong internally. Otherwise, this means you are using the Connection object from multiple threads. This is not allowed (without an explicit lockmode=LOCKMODE_WAIT constructor argument, C Source=(src/oputil.c,668)>

I’m connecting with the following code:

cluster = Cluster('couchbase://my_server_ip',
                  ClusterOptions(PasswordAuthenticator('Administrator', 'pw'), lockmode=2))
cb = cluster.bucket('mybucket')
cb_coll = cb.default_collection()

I’ve added LOCKMODE_WAIT to the cluster options but I get the error anyway, I don’t really know what I should do, could you help me please ?

Edit: I’m using the Python 3.0.2 SDK

Seems like the lock mode isn’t passed…

print(cluster.lockmode)
returns “1”, which is LOCKMODE_EXC, the default value

I tried using
from couchbase.options import LockMode
lockmode=LockMode.WAIT
but it also returns “1”.

I’m a Junior so maybe this issue is simple to solve since I couldn’t find anything on it. If you could just point me in the right direction as to how I should fix this issue it would be great.

Fixed it by passing lockmode=2 to “Cluster()” instead of “ClusterOptions()”. Weird.

cluster = Cluster(‘couchbase://my-ip’, ClusterOptions(PasswordAuthenticator(‘user’, ‘pw’)), lockmode=2)

Hi, it should be lock_mode in ClusterOptions. Interesting that lockmode makes it through (the options and **kwargs are merged, using the same keywords). Will inspect that piece of code.

Thanks,

Ellis

1 Like

https://issues.couchbase.com/browse/PYCBC-1022 - tracking JIRA

I have tried setting it with multiple versions of python couch sdk (3.0.2, 3.0.3, 3.0.4, 3.0.5) by passing lockmode=2 in either Cluster or ClusterOptions. But none of those working. I see there is an open issue to resolve this, would like to know if any work arounds f0r our flask based application until we get new sdk with this fix available. Thanks in advance.

This actually is an issue with the “bucket” not having the correct lockmode…

When you use the cluster object to get a bucket cluster.bucket what happens is that it does not pass down the LockMode set on the cluster object. So the only work around is calling the underlying cluster object in the cluster and calling the open_bucket method and passing in the bucket lockmode.

IE

bucket = cluster._cluster.open_bucket(bucket_name, lockmode=2)
The bucket object will now have lockmode set.

Couchbase needs to fix in the SDK when using the abstraction layer of the cluster when getting a bucket ie .Bucket to pass back the cluster settings to the bucket settings, or at least allow the user to pass down some settings when creating the bucket.

cluster._cluster.open_bucket is not available any more in Python SDK 3.x, there seems no workaround for this, the lockmode cannot be carried over from Cluster.

self._cluster = Cluster(‘couchbase://{}’.format(self._db_hosts),
ClusterOptions(
PasswordAuthenticator(self._username, self._my_password),
lockmode=LOCKMODE_WAIT
),
lockmode=LOCKMODE_WAIT
)

            self._bucket = self._cluster.bucket(self._bucket_name)
            print("cluster: ", self._cluster.lockmode)
            print("bucket: ", self._bucket.lockmode)
            self._collection = self._bucket.default_collection()

cluster: 2
bucket: LockMode.EXC

I have to take a look at my code but I believe that the reason why my lock mode worked is because my app only used N1QL queries, so I only used cluster.query(A_Query). Sorry for my misleading answers, LockMode for the bucket probably doesn’t work with the “solution” I mentioned above.

Removing the “Solution” tag.

@ellis.breen,

The defect in Python SDK 3.0.x seems very critical when using couchbase buckets from multiple threads, is there any ETA when PYCBC-1022 will be fixed?

@Joey_Wang

thats not true… Its still available in couchbase 3.X… its a sub cluster object which is why its

cluster._cluster.open_bucket

Based on the code you showed it will be
self._cluster._cluster.open_bucket()

thank you @mplachter, you’re correct.
the open_bucket does exist and it’s working!!!
self._bucket = self._cluster._cluster.open_bucket(self._bucket_name, lockmode=LOCKMODE_WAIT) #work around to set lockmode
self._collection = self._bucket.default_collection()