Skip to content

Commit

Permalink
fix: avoid race conditions when removing multiple instance groups at …
Browse files Browse the repository at this point in the history
…once
  • Loading branch information
pb82 committed Sep 9, 2024
1 parent c4d8fdb commit 591ec95
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion awx/api/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
from awx.api.metadata import SublistAttachDetatchMetadata, Metadata
from awx.conf import settings_registry

from django.db.transaction import get_connection

__all__ = [
'APIView',
'GenericAPIView',
Expand Down Expand Up @@ -743,7 +745,16 @@ def unattach_by_id(self, request, sub_id):
if parent_key:
sub.delete()
else:
relationship.remove(sub)
with transaction.atomic():
cursor = get_connection().cursor()
# lock the table to prevent any race conditions at the database level
# a transaction will automatically release the lock once committed (or rolled back) and
# other transactions will automatically wait for the lock to be released
cursor.execute(f'LOCK TABLE {relationship.through._meta.db_table} IN SHARE ROW EXCLUSIVE MODE')
try:
relationship.remove(sub)
finally:
cursor.close()

return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down

0 comments on commit 591ec95

Please sign in to comment.