Skip to content

Commit 489936f

Browse files
auto_create_context: make engagement creation atomic (#13444)
1 parent dec5a63 commit 489936f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

dojo/importers/auto_create_context.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,16 @@ def get_or_create_engagement(
318318
target_end = (timezone.now() + timedelta(days=365)).date()
319319
# Create the engagement
320320
with transaction.atomic():
321-
return Engagement.objects.select_for_update().create(
321+
# Lock the parent product row to serialize engagement creation per product
322+
locked_product = Product.objects.select_for_update().get(pk=product.pk)
323+
# Re-check for an existing engagement now that we hold the lock
324+
existing = get_last_object_or_none(Engagement, product=locked_product, name=engagement_name)
325+
if existing:
326+
return existing
327+
return Engagement.objects.create(
322328
engagement_type="CI/CD",
323329
name=engagement_name,
324-
product=product,
330+
product=locked_product,
325331
lead=get_current_user(),
326332
target_start=target_start,
327333
target_end=target_end,

0 commit comments

Comments
 (0)