MongoEngine / MongoEngine/mongoengine
switch_collection method is not thread safe
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Hi guys,
We just run into an issue when archiving data from one collection into another using switch_collection in multi threaded environment (sample code we used below):
def archive(self):
self.switch_collection(Item._meta['archive_collection'])
self.save()
self.switch_collection(Item._meta['collection'])
self.delete()
The scenario is a following:
1st thread runs archive, executes self.switch_collection(Item._meta['archive_collection'])
2nd thread wants to query different document and doesn't find it, because 1st thread has switched collection.
It would be a good idea to make switch_collection (and preferably switch_db) method thread safe.
What do you think?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the implementations of switch_collection and switch_db, then review the concurrent archive example in the issue. The work is done when concurrent operations retain the correct collection and database without interfering with one another, with tests demonstrating that behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100