MongoEngine / MongoEngine/mongoengine

Closing connections (disconnect) is not thread-safe

Open
#2,546 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Calling disconnect or disconnect_all in a multi-threaded environment is unsafe. Consider the following code:

import threading


import time
from mongoengine import *

connect(host="mongodb://127.0.0.1:9800/test_db_1")

class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

def thread_1_task():
    for user in User.objects:
        print(user.email)
    disconnect()

def thread_2_task():
    time.sleep(5)
    for user in User.objects:   # <----- This query raises a mongoengine.connection.ConnectionFailure
        print(user.email)
    disconnect()


def main():
    t1 = threading.Thread(target=thread_1_task)
    t2 = threading.Thread(target=thread_2_task)

    t1.start()
    t2.start()

    t1.join()
    t2.join()

if __name__ == "__main__":
    main()

After taking a look at connection.py, I found that the problem was that connections/db references were being stored in a process level dict. The solution is to probably use either threading.local or contextvars to store these references so that each thread / co-routine would get its own connection object thereby making it safe to execute disconnect in concurrent environments.

If agreed upon, I can submit a PR which I've been working on to fix this.

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 by reading connection.py and reproduce the disconnect/disconnect_all race using the threaded example in the issue. Trace how connection and database references are stored and determine the concurrency-safe ownership needed for threads or coroutines. Done means concurrent queries remain usable when another worker disconnects, with the existing connection behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
backend, database
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.