submessage: Select Message FOR UPDATE when creating.

This locks the message while creating a submessage, which
will handle race conditions caused by deleting the message
simultaneously.

We make sure that events work happens outside the transaction,
so that in case there's a problem with the queue processor,
the locks aren't held for too long.
This commit is contained in:
Abhijeet Prasad Bodas
2021-05-18 20:29:17 +05:30
committed by Tim Abbott
parent 5f4113cf60
commit efc2f49e3c
3 changed files with 27 additions and 2 deletions

View File

@@ -2141,6 +2141,10 @@ def do_add_submessage(
msg_type: str,
content: str,
) -> None:
"""Should be called while holding a SELECT FOR UPDATE lock
(e.g. via access_message(..., lock_message=True)) on the
Message row, to prevent race conditions.
"""
submessage = SubMessage(
sender_id=sender_id,
message_id=message_id,
@@ -2160,7 +2164,7 @@ def do_add_submessage(
ums = UserMessage.objects.filter(message_id=message_id)
target_user_ids = [um.user_profile_id for um in ums]
send_event(realm, event, target_user_ids)
transaction.on_commit(lambda: send_event(realm, event, target_user_ids))
def notify_reaction_update(