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

I have a non-sharded mongo replication cluster with maxIncomingConnections: 500. There were no application code changes but recently app is facing the below error. What is the reason for this error and how to resolve the issue.

ERROR:
pymongo.errors.OperationFailure: cannot add session into the cache
When checked db.serverStatus().logicalSessionRecordCache
"activeSessionsCount" is around 300 which is still less than max incoming connections. Also current connections count is 28, not sure why application is unable to add new session.  

 db.serverStatus().connections;
 {
"current" : 28,
"available" : 472,
"totalCreated" : 315382,
"active" : 2
 }

As a quick turn around when maxIncomingConnections was increased to 1000, the error no longer occurred. But what are the other options I can work on to resolve this situation.

Based on the error and the code, it sounds like you are maxing out your connections. You might have a session leak where you are starting a large amount of connections.

Can you check

db.system.sessions.count()

db.system.sessions.count() value is 2840, I don’t see anywhere near 500 connections present in netstat on any of the machines.

Is there a way I find the session leaks or the culprit sessions/processes?
What are the remediation steps to handle such scenario from database end as well as application end ?

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