-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
switch_collection method is not thread safe #788
Comments
Hi I did a little investigation. |
@matino , why not wrap with lock your archive and query methods? |
@mmarksnippety , I think it is because otherwise how would you be able to do other actions on the second collection? |
@DavidBord - I would have to lock all queries in a non archived collection, remember about locking new queries - not really an elegant solution for me. |
I think that read locking the collection while in the context of switch_collection would also lock the collection for reading from the collection switch_collection switched to |
I need to be 100% sure ;) |
But its not only about you, right? :) Regardless of mongoengine, I would have separated the archive and query stuff into 2 separate processes because I wouldn't want a problem with one of them to affect the other |
@DavidBord - fine by me, thanks for your 2prompt replies anyway! Maybe there should be a warning at least in the docs about this behavior? What do you think? |
+1 for including a warning in the docs. We've been seeing a lot of strange bugs that all turned out to be caused by the non-thread-safety of the |
Why not completely review this or maybe even remove this feature? I know that this sound bald but beyond the thread issue the concept of a "switch_db" is already dangerous, doesn't looks like an streamlined code. Ideally one should use simply use connA and connB to reference different collections, because in fact they are different connections |
Ran into the same issue today, also in a multithreaded environment. Hours spend catching an inconsistent bug because of such behaviour. Ended up in mongoengine's sources (leaky abstraction, yeah). Docs, unfortunately, still have no info about Thread 1:
Thread 2:
Thread 2 is almost the same as the usage shown in
Summarizing: it's quite an unexpected move to switch the whole Document's database affecting all queries and instances when using +1 for feature implementation reviewing, or at least a sad warning in docs. |
My approach to resolve this problem was to stop using I think something similar can be done to |
6 years passed and the bug which is "just a little bit" critical unless you do a lot of concurrency testing is still open :( |
rather than switch, what about using |
I took a look at the code and it all uses get_db() under-the-hood in a non-thread-safe way. I've proposed a change to support switching db's in a safe way, which could be extended to collections. See here: #2686 |
Hi guys,
We just run into an issue when archiving data from one collection into another using
switch_collection
in multi threaded environment (sample code we used below):The scenario is a following:
1st thread runs
archive
, executesself.switch_collection(Item._meta['archive_collection'])
2nd thread wants to query different document and doesn't find it, because 1st thread has switched collection.
It would be a good idea to make
switch_collection
(and preferablyswitch_db
) method thread safe.What do you think?
The text was updated successfully, but these errors were encountered: