From ef21dd9b9900ba33a21c1823d413158758c0cbec Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 17 Sep 2024 14:32:45 +0000 Subject: [PATCH] thumbnail: Set a stable ordering on ImageAttachment rows for locking. Failure to have a stable ordering can lead to deadlocks. --- zerver/lib/thumbnail.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zerver/lib/thumbnail.py b/zerver/lib/thumbnail.py index f15303fdf4..0acdf9aaf6 100644 --- a/zerver/lib/thumbnail.py +++ b/zerver/lib/thumbnail.py @@ -347,7 +347,9 @@ def get_user_upload_previews( upload_preview_data: dict[str, MarkdownImageMetadata] = {} - image_attachments = ImageAttachment.objects.filter(realm_id=realm_id, path_id__in=path_ids) + image_attachments = ImageAttachment.objects.filter( + realm_id=realm_id, path_id__in=path_ids + ).order_by("id") if lock: image_attachments = image_attachments.select_for_update() for image_attachment in image_attachments: