MongoEngine / MongoEngine/mongoengine

mongoengine does not disconnect tcp auto, make client timeout

Open
#2,246 0 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

Today morning, I found I can not connect mongo anymore, and did a log of research for it.

I checked my system [windows], cause there is a blog say windows does not have two key in reg-table. [NOT WORK]

I try to call the disconnect function explicitly. [NOTWORK]

So, I tail the mongodb log under server.

I found that mongoengine establish connection twice when I call connect and doc.objects.filter(...) method.

def test_mongo_to_df():
    try:
        register_connection("default", config['MONGO_DB'], host=config['MONGO_URL'],
                            authentication_source=config['MONGO_AUTHENTICATION_SOURCE'])
        data = Record.objects.filter(dt__gt=datetime.now() - rd(minutes=1)).first()
        print(mongo_to_df(data))
    except KeyboardInterrupt as e:
        disconnect()
    else:
        disconnect()

It blocked.

Cause one of the connect client is never be released.

So, I try to use pymongo.MongoClient

def test_pymongo():
    myclient = pymongo.MongoClient("mongodb://wFGZp2q8hnxFlaXJ:4xlQxtV6jwrZ9ZiF@favormylikes.com:27017/")
    try:
        mydb = myclient["mydb"]
        mycol = mydb["record"]
        start = datetime.now() - rd(minutes=60)
        cursor = mycol.aggregate([{"$match": {"dt": {"$gte": start, "$lte": datetime.now()}}},
                                  {"$project": {"_id": 0,
                                                "src": "$src"}}])
        data = Df(list(cursor))
        print(data)
    except:
        myclient.close()
    else:
        myclient.close()

It work, maybe that is the reason why mongoengine always block.

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 reproducing the reported register_connection, Record.objects.filter(...), and disconnect() sequence, then compare its connection lifecycle with the shown pymongo.MongoClient example. The issue is done when the MongoEngine query no longer blocks because its connection is released or disconnected as expected.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.