diff --git a/license_manager/apps/subscriptions/admin.py b/license_manager/apps/subscriptions/admin.py index cb88294a..c80f0546 100644 --- a/license_manager/apps/subscriptions/admin.py +++ b/license_manager/apps/subscriptions/admin.py @@ -407,7 +407,12 @@ def save_model(self, request, obj, form, change): if not change: obj.desired_num_licenses = form.cleaned_data.get('num_licenses', 0) - super().save_model(request, obj, form, change) + # If the desired number of licenses is large enough, we trigger an async celery task + # after the creation of this record. We wrap that creation in a transaction here + # to force a commit, so that the async task does not encounter a race condition + # where the plan it expects to read from the DB does not yet exist. + with transaction.atomic(): + super().save_model(request, obj, form, change) # Finally, if we're creating the model instance, go ahead and create the related license records. if not change: