MongoEngine / MongoEngine/mongoengine

save() may break database consistency (concurrency issue)

Open
#564 3 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug High Priority
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

save() only saves changed fields in existing documents, This is pretty fine, but it saves with upsert=True! So, if someone deleted the document in the meanwhile, we end-up with a document with only updated fields (and missing required fields). (We encountered to these mal-structured rows in our database.)

save() should not use upsert=True when updating only changed fields. It may check for results and raise an exception if no document is updated (sqlalchemy has a StaleDataError for this porpose)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at save() and trace the existing-document path that updates only changed fields. Verify that a concurrent deletion cannot recreate a partial document through upsert, and that a no-update result produces the intended stale-data exception; add regression coverage for both outcomes.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.