Skip to content

Commit

Permalink
Fix a race condition in distribution filtering
Browse files Browse the repository at this point in the history
This change adds a `detail_model` property to the master-detail concept
and removes a race condition by replacing calls to `cast` with it in
`DistributionWithContentFilter`.

fixes #4766
  • Loading branch information
mdellweg authored and ggainey committed Dec 1, 2023
1 parent de247ba commit ad0f105
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/4766.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed a race condition leading to 500 error in distribution filtering.
1 change: 1 addition & 0 deletions CHANGES/plugin_api/4766.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the async safe `detail_model` property to master detail models.
8 changes: 6 additions & 2 deletions pulpcore/app/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,20 @@ def get_pulp_type(cls):
def get_model_for_pulp_type(cls, pulp_type):
return cls._pulp_model_map[pulp_type]

def save(self, *args, **kwargs):
@property
def detail_model(self):
return self._pulp_model_map[self.pulp_type]

def __init__(self, *args, **kwargs):
# instances of "detail" models that subclass MasterModel are exposed
# on instances of MasterModel by the string stored in that model's TYPE attr.
# Storing this pulp_type in a column on the MasterModel next to makes it trivial
# to filter for specific detail model types across master's relations.
# Prepend the TYPE defined on a detail model with a django app label.
# If a plugin sets the type field themselves, it's used as-is.
super().__init__(*args, **kwargs)
if not self.pulp_type:
self.pulp_type = self.get_pulp_type()
return super().save(*args, **kwargs)

def cast(self):
"""Return the "Detail" model instance of this master-detail object.
Expand Down
6 changes: 3 additions & 3 deletions pulpcore/app/viewsets/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ def filter(self, qs, value):
for dist in qs.exclude(publication=None).values("publication__repository_version", "pk"):
versions_distributions[dist["publication__repository_version"]].append(dist["pk"])

for dist in qs.exclude(repository_version=None).values("repository_version", "pk"):
if not dist.cast().SERVE_FROM_PUBLICATION:
for dist in qs.exclude(repository_version=None).values("repository_version", "pulp_type"):
if not dist.detail_model.SERVE_FROM_PUBLICATION:
versions_distributions[dist["repository_version"]].append(dist["pk"])

for dist in qs.exclude(repository=None).prefetch_related("repository__versions"):
if dist.cast().SERVE_FROM_PUBLICATION:
if dist.detail_model.SERVE_FROM_PUBLICATION:
versions = dist.repository.versions.values_list("pk", flat=True)
publications = Publication.objects.filter(
repository_version__in=versions, complete=True
Expand Down

0 comments on commit ad0f105

Please sign in to comment.