Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: Unable to acquire lock

I've been getting this error lately when running my tests. I tried it with a local MongoDB server (4.0.5) and I've tried it on Mongo Atlas but experiencing the same problem.

I tried increasing the lock timeout but that has no effect.

I'm not sure what the problem is.

{ MongoError: Unable to acquire lock '{8576955153473224393: Database, 1659426125832142537}' within a max lock request timeout of '5ms' milliseconds.
    at queryCallback (/home/user/workspace/my-project/node_modules/mongodb-core/lib/cursor.js:248:25)
    at /home/user/workspace/my-project/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  errorLabels: [ 'TransientTransactionError' ],
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 29, high_: 1548245676 },
  ok: 0,
  errmsg: 'Unable to acquire lock \'{8576955153473224393: Database, 1659426125832142537}\' within a max lock request timeout of \'5ms\' milliseconds.',
  code: 24,
  codeName: 'LockTimeout',
  '$clusterTime': 
   { clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 29, high_: 1548245676 },
     signature: { hash: [Object], keyId: 0 } },
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }
like image 470
Christiaan Maks Avatar asked Jun 09 '26 23:06

Christiaan Maks


2 Answers

Not really a fix but a workaround. Since it only happens on my local machine during automated testing and it doesn't happen in production I can get away with this.

The Unable to acquire lock error can be prevented by setting the maxTransactionLockRequestTimeoutMillis to something higher but since I also had this error Unable to read from a snapshot due to pending collection catalog changes; please retry the operation. at the same time, I figured out the following to fix that error which in turn also fixed the first error:

My test runner (AVA) creates a new database for every test file but it seems it needs some time to settle now and adding await timeout(1000) after the mongoose.connect the problem went away. Not super happy with this solution but for testing it is good enough.

like image 147
Christiaan Maks Avatar answered Jun 12 '26 11:06

Christiaan Maks


It would be beneficial to know the query you are using that is causing the timeout as well as what indexes you have in place and how heavy the workload is. You can increase the timeout with the following command:

db.adminCommand( { setParameter: 1, maxTransactionLockRequestTimeoutMillis: 5000 })

But I would first try and run a profiler to see what is causing the contention:

https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/

like image 40
Jeff Blanchard Avatar answered Jun 12 '26 13:06

Jeff Blanchard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!