#13366 closed (fixed)
Model importing race condition created in [12977] for subclasses of abstract classes that have fields.
Reported by: | Brett Hoerner | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Keywords: | ||
Cc: | adulic@…, safariman@…, davisd.davisd@…, rokclimb15@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
We ran into a weird edge case race condition that seems to be caused by [12977]. FWIW I haven't taken all the time needed to understand exactly why or what [12977] does, I'm just reporting the error based on a traceback and example code set. :)
Basic things you need:
- An abstract base class with a field, and a model A that uses it, so that
ModelBase.__new__
will drop into the "new_class.add_to_class(field.name, copy.deepcopy(field))
" branch intended for abstract only. - Another model (in a different module) that imports model A.
- A management command that imports model A (this won't happen in the shell, I'm not sure what the difference is yet or where else it would occur).
Basically it seems that the new "self.default = self.model._meta.get_field_by_name(self.name)[0].default
" results eventually in a "get_models()
", but this is run *while model A is being loaded* which means that all models are fetched, including the models file which contains model B and thus imports model A, and then blammo, circular dependency problem.
I've attached a simple project with two apps and a sample management command.
Attachments (2)
Change History (13)
by , 15 years ago
Attachment: | 13366-example.tar.gz added |
---|
comment:1 by , 15 years ago
#13368 and a post in django-dev (http://groups.google.com/group/django-developers/browse_frm/thread/be2085f11e9f95e?hl=en) report a manifestation of this when trying to import django.contrib.comments.models.Comment
from an app models module.
comment:2 by , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 15 years ago
Component: | Uncategorized → Core framework |
---|
comment:4 by , 15 years ago
I think the solution here is to force the population of the application cache before management commands are used. The simple approach of putting "get_apps()" early in the management command execute cycle fixes this particular problem, but it causes other regressions in the test suite (mostly dealing with error handling of applications that can't be imported, etc).
comment:5 by , 15 years ago
Cc: | added |
---|
comment:6 by , 15 years ago
Cc: | added |
---|
comment:7 by , 15 years ago
It would be better to guard the self.model access with a hasattr
check. copy()
should be safe to call at *all* times. Postponing the call is a workaround, not a fix.
by , 15 years ago
Attachment: | 13366.symptomaticpatch added |
---|
Attached symptomatic patch for using the comments app (worksforme).
comment:8 by , 15 years ago
Cc: | added |
---|
comment:9 by , 15 years ago
Cc: | added |
---|
comment:10 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Simple example project, run
python manage.py fail