Skip to content

Commit

Permalink
Fix 500 error on parallel same-object creates
Browse files Browse the repository at this point in the history
fixes: pulp#4183
  • Loading branch information
gerrod3 committed Aug 2, 2023
1 parent be1400f commit e5a98b6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/4183.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a 500 server error when two concurrent requests try to create an object with the same unique fields.
12 changes: 11 additions & 1 deletion pulpcore/app/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from django.conf import settings
from django.core.validators import URLValidator
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.urls.exceptions import NoReverseMatch
from django.db import IntegrityError
from django.db.models import Model
Expand Down Expand Up @@ -439,6 +439,16 @@ def _validate_relative_path(self, path):

return path

def save(self, **kwargs):
try:
super().save(**kwargs)
except IntegrityError as e:
# Concurrent request got by the unique validator on the serializer
if "unique" in str(e):
# Run validation again to properly raise an unique-ValidationError
self.run_validation(self.initial_data)
raise e

def __init_subclass__(cls, **kwargs):
"""Set default attributes in subclasses.
Expand Down

0 comments on commit e5a98b6

Please sign in to comment.