Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent switch_db failure #1487

Open
goldbaum opened this issue Feb 23, 2017 · 3 comments
Open

Concurrent switch_db failure #1487

goldbaum opened this issue Feb 23, 2017 · 3 comments

Comments

@goldbaum
Copy link

goldbaum commented Feb 23, 2017

I have multiple green threads that try to with switch_db(SomeDocument, alias):... on the same document in order to query multiple aliases.
Once in a while I get an exception:

File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py", line 104, in count
return super(QuerySet, self).count(with_limit_and_skip)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 347, in count
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1481, in _cursor
self._cursor_obj = self._collection.find(self._query,
AttributeError: 'NoneType' object has no attribute 'find'
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py", line 104, in count
return super(QuerySet, self).count(with_limit_and_skip)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 347, in count
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1481, in _cursor
self._cursor_obj = self._collection.find(self._query,
AttributeError: 'NoneType' object has no attribute 'find'

It happens possibly due to self.cls._collection = None in switch_db.enter function.
I'm using mongoengine 0.10.6

@wojcikstefan
Copy link
Member

Hi @goldbaum, I'm afraid switch_db has many flaws in its current form. Your issue sounds very similar to #788. There's already some progress on this stuff via @anih's #1457 PR.

@bagerard
Copy link
Collaborator

Relates to #788

@josepato
Copy link

We ran into this issue when using multiple treads hitting different databases.

We realize that the db_alias is changing on all the class instances, so if I have

  • instance_1 of the some class with db_alias='user_1'
  • instance_2 of the same class with db_alias='user_2'

The instance_1, db_alias attribute will be changes to 'user_2'. So in that fraction of a second the instance_1 will hit the wrong database.

We try to init() the class but we had a lot of problems with that. So we added a property to the document (the actual document that is being created or updated) that is called 'db_alias'. So when the _save_create and _save_update are called, we check if the document has the attribute and if so, we call directly the get_db and return the proper db and collection. Still making some adjustments but so far so good.

https://github.com/linkaform/mongoengine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants