Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

user_message: Sort by message_id to avoid deadlocks.

Bedo Khaledb4c495229eba
diff --git a/zerver/lib/user_message.py b/zerver/lib/user_message.py
index c6fa35ec993..5708679b2fb 100644
--- a/zerver/lib/user_message.py
+++ b/zerver/lib/user_message.py
@@ -59,7 +59,17 @@ def bulk_insert_ums(ums: list[UserMessageLite]) -> None:
if not ums:
return
- vals = [(um.user_profile_id, um.message_id, um.flags) for um in ums]
+ # PostgreSQL automatically acquires a FOR KEY SHARE row-lock
+ # on the referenced tables: zerver_userprofile and zerver_message.
+ # We sort the rows by message_id so zerver_message rows
+ # are locked in a consistent order; this prevents potential
+ # deadlock with codepaths that take FOR UPDATE lock,
+ # in that same order (ascending), on those same rows.
+ vals = [
+ (um.user_profile_id, um.message_id, um.flags)
+ for um in sorted(ums, key=lambda um: um.message_id)
+ ]
+
query = SQL(
"""
INSERT into
@@ -79,6 +89,11 @@ def bulk_insert_all_ums(
if not user_ids or not message_ids:
return
+ # As in bulk_insert_ums: sort message_ids so
+ # zerver_message rows are locked in a consistent order,
+ # avoiding deadlocks.
+ message_ids = sorted(message_ids)
+
query = SQL(
"""
INSERT INTO zerver_usermessage (user_profile_id, message_id, flags)