Skip to content

Commit

Permalink
Using select_for_update to grab a lock when calculating inner_id
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanu committed Dec 12, 2024
1 parent a900be2 commit d53ac20
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions label_studio/tasks/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,13 @@ def add_tasks(self, task_annotations, task_predictions, validated_tasks):
db_tasks = []
max_overlap = self.project.maximum_annotations

# identify max inner id
tasks = Task.objects.filter(project=self.project)
prev_inner_id = tasks.order_by('-inner_id')[0].inner_id if tasks else 0
# Acquire a lock on the project to ensure atomicity when calculating inner_id
project = Project.objects.select_for_update().get(id=self.project.id)

last_task = Task.objects.filter(project=project).order_by('-inner_id').first()
prev_inner_id = last_task.inner_id if last_task else 0
max_inner_id = (prev_inner_id + 1) if prev_inner_id else 1

for i, task in enumerate(validated_tasks):
cancelled_annotations = len([ann for ann in task_annotations[i] if ann.get('was_cancelled', False)])
total_annotations = len(task_annotations[i]) - cancelled_annotations
Expand Down

0 comments on commit d53ac20

Please sign in to comment.