Giter Site home page Giter Site logo

“AttributeError: ‘_thread._local’ object has no attribute ‘no_dereferencing_class’ in Multithreaded Environment” about mongoengine HOT 11 CLOSED

AdithyanJothir avatar AdithyanJothir commented on June 10, 2024 7
“AttributeError: ‘_thread._local’ object has no attribute ‘no_dereferencing_class’ in Multithreaded Environment”

from mongoengine.

Comments (11)

bagerard avatar bagerard commented on June 10, 2024 11

merged and 0.28.1 will be published in a few minutes

from mongoengine.

bagerard avatar bagerard commented on June 10, 2024 3

Thanks for the reproducible snippet, working on a fix right now

from mongoengine.

bagerard avatar bagerard commented on June 10, 2024

Argh this change was brought in the last release.
Could you provide a reproducible snippet containing minimal amount of code?

from mongoengine.

kelvin-j-li avatar kelvin-j-li commented on June 10, 2024

I have the same error with eventlet-0.35.2 when running mongoengine-0.28.0 and flask_track_usage-2.0.0.

Fallback to mongoengine-0.27.0 help remove the no_dereferencing_class error:

  File "/home/pyuser/app/dist-packages/flask_track_usage/summarization/__init__.py", line 53, in _caller
    method(**kwargs)
  File "/home/pyuser/app/dist-packages/flask_track_usage/summarization/mongoenginestorage.py", line 139, in sumUrl
    increment(sumUrlClasses, src, "url", ["url"])
  File "/home/pyuser/app/dist-packages/flask_track_usage/summarization/mongoenginestorage.py", line 38, in increment
    doc = class_dict[period].objects(date=times[period], **db_args).first()
  File "/home/pyuser/app/dist-packages/mongoengine/queryset/base.py", line 301, in first
    result = queryset[0]
  File "/home/pyuser/app/dist-packages/mongoengine/queryset/base.py", line 206, in __getitem__
    _auto_dereference=self._auto_dereference,
  File "/home/pyuser/app/dist-packages/mongoengine/queryset/base.py", line 1768, in _auto_dereference
    should_deref = not no_dereferencing_active_for_class(self._document)
  File "/home/pyuser/app/dist-packages/mongoengine/context_managers.py", line 28, in no_dereferencing_active_for_class
    return cls in thread_locals.no_dereferencing_class
  File "/home/pyuser/app/dist-packages/eventlet/corolocal.py", line 45, in __getattribute__
    return object.__getattribute__(self, attr)
AttributeError: 'local' object has no attribute 'no_dereferencing_class'

Thanks!

from mongoengine.

kelvin-j-li avatar kelvin-j-li commented on June 10, 2024
# activate eventlet
import os
import eventlet
eventlet.monkey_patch()

from flask_track_usage.storage.mongo import MongoEngineStorage
from mongoengine import connect

from mongoengine.

saipraveenkondapalli avatar saipraveenkondapalli commented on June 10, 2024

I am facing the same issue after updating to the latest version 0.28.0. The issue occurs only in the latest version, downgrading to version 0.27.0 seems to solve the issue.

sample code from my project

@auth_bp.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    email = data.get('email')
    password = data.get('password')
    user = User.objects(email=email).first()
    if user and check_password_hash(user.password, password):
        return make_response('Logged in', 200)
    return make_response('Invalid email or password', 401)

The issue occurs where ever there is a query in the code.

from mongoengine.

AdithyanJothir avatar AdithyanJothir commented on June 10, 2024

Here is a simple code snippet to reproduce the said issue using asgiref sync to async with ThreadPoolExecuter

import asyncio
from concurrent.futures import ThreadPoolExecutor
from functools import partial
import mongoengine
from asgiref.sync import sync_to_async
from mongoengine import Document, StringField

mongoengine.connect(
    host="mongodb://localhost:27017/",
    db="test_db",
)


class UserModel(Document):
    meta = {
        "collection": "users",
    }
    name = StringField()


user_a = UserModel(name="JothirAdithyan")
UserModel.objects.insert(user_a)


async def get_docs():
    function = partial(UserModel.objects.first)
    user1 = await sync_to_async(func=function, thread_sensitive=False, executor=ThreadPoolExecutor())()
    print(user1.to_json())

if __name__ == "__main__":
    asyncio.run(get_docs())

from mongoengine.

c4talyst avatar c4talyst commented on June 10, 2024

Here is an example using just Flask (no async).

tl;dr: Downgrade to mongoengine 0.27.0 and it works

requirements.txt

Flask==3.0.2
mongoengine==0.28.0

app.py


app = Flask(__name__)

from flask import Flask
from mongoengine import connect
from mongoengine import Document, StringField

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {
    'db': 'my_database',
    'host': 'mongodb://localhost:20000/my_database'
}
connect(db='my_database', host='mongodb://localhost:20000/my_database')

class User(Document):
    user_id = StringField(required=True)
    name = StringField()
    email = StringField(required=True)

@app.route('/')
def index():
    # Create a new user
    user = User(user_id='john_doe', email='[email protected]')
    user.save()
    return 'User created successfully!'

@app.route('/users')
def get_users():
    # Retrieve all users
    users = User.objects.all()
    user_list = ', '.join([f"{user.user_id}: {user.email}" for user in users])
    return f'Users: {user_list}'


if __name__ == "__main__":
    app.run(debug=True, port=6001)

Exception

Traceback (most recent call last):
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 1488, in __call__
    return self.wsgi_app(environ, start_response)
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 1466, in wsgi_app
    response = self.handle_exception(e)
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 1463, in wsgi_app
    response = self.full_dispatch_request()
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 870, in full_dispatch_request
    rv = self.dispatch_request()
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/flask/app.py", line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
  File "/tmp/temp-flask-mongo/app.py", line 32, in get_users
    user_list = ', '.join([f"{user.user_id}: {user.email}" for user in users])
  File "/tmp/temp-flask-mongo/app.py", line 32, in <listcomp>
    user_list = ', '.join([f"{user.user_id}: {user.email}" for user in users])
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/mongoengine/queryset/queryset.py", line 109, in _iter_results
    self._populate_cache()
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/mongoengine/queryset/queryset.py", line 128, in _populate_cache
    self._result_cache.append(next(self))
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/mongoengine/queryset/base.py", line 1636, in __next__
    _auto_dereference=self._auto_dereference,
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/mongoengine/queryset/base.py", line 1768, in _auto_dereference
    should_deref = not no_dereferencing_active_for_class(self._document)
  File "/tmp/temp-flask-mongo/venv/lib/python3.10/site-packages/mongoengine/context_managers.py", line 28, in no_dereferencing_active_for_class
    return cls in thread_locals.no_dereferencing_class
AttributeError: '_thread._local' object has no attribute 'no_dereferencing_class'

Fix

Downgrade mongoengine to 0.27.0

from mongoengine.

HK-Mattew avatar HK-Mattew commented on June 10, 2024

Recently I'm getting the same error

from mongoengine.

jan-stillfried-toteat avatar jan-stillfried-toteat commented on June 10, 2024

We're facing the same issue. Downgrading fixed it for now.

from mongoengine.

voglster avatar voglster commented on June 10, 2024

Just hit this myself. Looks like the patch is on the way

from mongoengine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.