pymongo.errors.OperationFailure: cannot add session into the cache

Sessions automatically timeout on the server after 30 minutes of inactivity (see https://www.mongodb.com/docs/v7.0/reference/parameters/#mongodb-parameter-param.localLogicalSessionTimeoutMinutes). It’s possible your application could be unintentionally creating sessions without closing them efficiently if the app’s MongoClient is not closed. Could you try calling MongoClient.close() or use MongoClient in a with block and report back if this fixes the problem?:

with MongoClient(...) as client:
    run_app(client)
# or:
client = MongoClient(...)
try:
    run_app(client)
finally:
    client.close()

Could you also check the maxSessions server parameter? It defaults to 1000000 so 2840 should not be a problem unless the maxSessions param was lowered. For example:

>>> client.admin.command('getParameter', maxSessions=1)['maxSessions']
1000000