How do I reproduce CursorNotFound error due to 10 minutes of cursor inactivity using PyMongo tools?
def sleep_for_minutes(minutes_to_sleep):
for i in range(minutes_to_sleep):
print(f'{i} sleeping for 1 minute')
time.sleep(60 * 1)
# Iterate over all documents in the collection
for document in collection.find():
print(f'{document} before sleeping')
sleep_for_minutes(15)
print(f'{document} after sleeping')
Context
Am iterating a very large Mongo collection. Each document of a collection is quite large as well. Tech stack : MongoEngine, Django
My production system is timing out due to CursorNotFound error.
Error goes like this : pymongo.errors.CursorNotFound: cursor id <something> not found, full error: {'ok': 0.0, 'errmsg': 'cursor id <something> not found', 'code': 43, 'codeName': 'CursorNotFound'}
As per my understanding, there can 2 possible reasons for the same:
- Session Idle Timeout which occurs at 30 minutes
- Cursor inactivity timeout after 10 minutes of inactivity
To fix and verify the fixes, I am trying to reproduce there errors on a local setup to fix the issue. I do this by using sleep methods.
While I am able to reproduce the 1st situation, I am unable to reproduce the situation when cursor times out after 10 minutes of activity.