Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
reactions: Select Message FOR UPDATE when adding/removing.
Abhijeet Prasad Bodas90b6fa7881d5
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex 1cdf0cecfa7..8682f2d78f1 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -2215,7 +2215,7 @@ def notify_reaction_update(stream = Stream.objects.get(id=stream_id)user_ids |= subscriber_ids_with_stream_history_access(stream)- send_event(user_profile.realm, event, list(user_ids))+ transaction.on_commit(lambda: send_event(user_profile.realm, event, list(user_ids)))def do_add_reaction(@@ -2225,6 +2225,11 @@ def do_add_reaction(emoji_code: str,reaction_type: 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.+ """+reaction = Reaction(user_profile=user_profile,message=message,@@ -2232,13 +2237,8 @@ def do_add_reaction(emoji_code=emoji_code,reaction_type=reaction_type,)- try:- reaction.save()- except django.db.utils.IntegrityError: # nocoverage- # This can happen when a race results in the check in views- # code not catching an attempt to double-add a reaction, or- # perhaps if the emoji_name/emoji_code mapping is busted.- raise JsonableError(_("Reaction already exists."))++ reaction.save()notify_reaction_update(user_profile, message, reaction, "add")@@ -2250,7 +2250,7 @@ def check_add_reaction(emoji_code: Optional[str],reaction_type: Optional[str],) -> None:- message, user_message = access_message(user_profile, message_id)+ message, user_message = access_message(user_profile, message_id, lock_message=True)if emoji_code is None:# The emoji_code argument is only required for rare corner@@ -2316,6 +2316,10 @@ def check_add_reaction(def do_remove_reaction(user_profile: UserProfile, message: Message, emoji_code: str, reaction_type: 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.+ """reaction = Reaction.objects.filter(user_profile=user_profile,message=message,@@ -2323,6 +2327,7 @@ def do_remove_reaction(reaction_type=reaction_type,).get()reaction.delete()+notify_reaction_update(user_profile, message, reaction, "remove")diff --git a/zerver/tests/test_reactions.py b/zerver/tests/test_reactions.pyindex 517ce72b88d..a5b0a9bc560 100644--- a/zerver/tests/test_reactions.py+++ b/zerver/tests/test_reactions.py@@ -4,7 +4,11 @@ from unittest import mockimport orjsonfrom django.http import HttpResponse-from zerver.lib.actions import do_change_stream_invite_only, do_make_stream_web_public+from zerver.lib.actions import (+ do_change_stream_invite_only,+ do_make_stream_web_public,+ notify_reaction_update,+)from zerver.lib.cache import cache_get, to_dict_cache_key_idfrom zerver.lib.emoji import emoji_name_to_emoji_codefrom zerver.lib.message import extract_message_dict@@ -1023,6 +1027,10 @@ class ReactionAPIEventTest(EmojiReactionBase):}events: List[Mapping[str, Any]] = []with self.tornado_redirected_to_list(events, expected_num_events=1):+ with mock.patch("zerver.lib.actions.send_event") as m:+ m.side_effect = AssertionError(+ "Events should be sent only after the transaction commits!"+ )self.api_post(reaction_sender, f"/api/v1/messages/{pm_id}/reactions", reaction_info)event = events[0]["event"]@@ -1084,3 +1092,27 @@ class ReactionAPIEventTest(EmojiReactionBase):self.assertEqual(event["emoji_name"], reaction_info["emoji_name"])self.assertEqual(event["emoji_code"], reaction_info["emoji_code"])self.assertEqual(event["reaction_type"], reaction_info["reaction_type"])++ def test_events_sent_after_transaction_commits(self) -> None:+ """+ Tests that `send_event` is hooked to `transaction.on_commit`. This is important, because+ we don't want to end up holding locks on message rows for too long if the event queue runs+ into a problem.+ """+ hamlet = self.example_user("hamlet")+ self.send_stream_message(hamlet, "Scotland")+ message = self.get_last_message()+ reaction = Reaction(+ user_profile=hamlet,+ message=message,+ emoji_name="whatever",+ emoji_code="whatever",+ reaction_type="whatever",+ )++ with self.tornado_redirected_to_list([], expected_num_events=1):+ with mock.patch("zerver.lib.actions.send_event") as m:+ m.side_effect = AssertionError(+ "Events should be sent only after the transaction commits."+ )+ notify_reaction_update(hamlet, message, reaction, "stuff")diff --git a/zerver/views/reactions.py b/zerver/views/reactions.pyindex b50b74c85e9..fa0666d588d 100644--- a/zerver/views/reactions.py+++ b/zerver/views/reactions.py@@ -1,5 +1,6 @@from typing import Optional+from django.db import transactionfrom django.http import HttpRequest, HttpResponsefrom django.utils.translation import gettext as _@@ -12,6 +13,8 @@ from zerver.lib.response import json_successfrom zerver.models import Reaction, UserProfile+# transaction.atomic is required since we use FOR UPDATE queries in access_message@has_request_variablesdef add_reaction(request: HttpRequest,@@ -26,6 +29,8 @@ def add_reaction(return json_success()+# transaction.atomic is required since we use FOR UPDATE queries in access_message@has_request_variablesdef remove_reaction(request: HttpRequest,@@ -35,7 +40,7 @@ def remove_reaction(emoji_code: Optional[str] = REQ(default=None),reaction_type: str = REQ(default="unicode_emoji"),) -> HttpResponse:- message, user_message = access_message(user_profile, message_id)+ message, user_message = access_message(user_profile, message_id, lock_message=True)if emoji_code is None:if emoji_name is None: