How do I reproduce CursorNotFound error due to 10 minutes of cursor inactivity and due to session idle timeout in 30 minutes using PyMongo tools?
I am trying something like this :
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)
# even tried sleeping for 35 minutes but didn't help
# sleeping for 45 mins worked (don't know why)
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.
# sleep is to emulate slow query
cursor = collection.find(batch_size=1).where("sleep(59000) || true")
for document in cursor:
print(f'sleeping and processing document: {document}')
sleep_for_minutes(3)
print(f'sleeping done')
This works without erroring out as such.
What exactly is the meaning of “cursor inactivity” or “cursor being idle”?
I guess I might have got it wrong.
So there is no more 10 minute cursor timeout, only the 30 minute session timeout. When the session is expired the cursor(s) it created are closed as well.