-
Notifications
You must be signed in to change notification settings - Fork 10
Change MerginProject constructor to force one instance per project #283
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
base: master
Are you sure you want to change the base?
Conversation
mergin/merginproject.py
Outdated
| # directory paths to instances. | ||
| # The dictionary is a WeakValueDictionary so we don't cause memory leaks | ||
| # (when the instance is GC'd, the entry is deleted). | ||
| project_cache: weakref.WeakValueDictionary[str, "MerginProject"] = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I understand... the type annotation is WeakValueDictionary - but doesn't project_cache become a normal dict if set it to {} ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's just me being an idiot, sorry. Though I wonder why my IDE didn't complain...
I've force-pushed a fix, since it's just one line.
0596d56 to
9ab77de
Compare
MarcelGeo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @dvdkon , could you run tests with your version of code? You probably do not need to fork repo, instead of this -> use clone.
This is good idea to do it in this way. I'm wondering if this is not like 🩹 for buggy behavior in db-sync itself. In plugin are flying the MerginProject instances and no issues with that. Yeah, we could probably solve initialisation of Geodiff. @wonder-sk
mergin/merginproject.py
Outdated
| return instance | ||
|
|
||
| instance = super().__new__(cls) | ||
| cls.project_cache[directory] = instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to pass project_cache to class directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's already there since it's defined as a class property. cls.project_cache here is equivalent to MerginProject.project_cache.
Sadly I don't have the right (non-trial) account to run tests on the dev instance.
We could probably work around the desync issues by just being careful in |
Currently there can be multiple instances of
MerginProjectin memory that represent a single project (directory). This is an issue, because doing an operation on one instance will desync metadata in the others, which caused syncing to fail indb-sync.This PR changes the MerginProject constructor to return the existing instance for a given directory, if one exists. It also bumps the library version, so I can make subsequent changes in
db-sync.