From 04347bb3156690d0ea0cc75820a0ca99c7ae02f0 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Wed, 31 Jul 2024 19:59:55 +0530 Subject: [PATCH] schedule_messages: Update do_schedule_messages to send event on commit. Earlier, we were using 'send_event' in 'do_schedule_messages' which can lead to a situation, if any db operation is added after the 'send_event' in future, where we enqueue events but the action function fails at a later stage. Events should not be sent until we know we're not rolling back. Fixes part of #30489. --- zerver/actions/scheduled_messages.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/zerver/actions/scheduled_messages.py b/zerver/actions/scheduled_messages.py index ab9239d90a..748fce7286 100644 --- a/zerver/actions/scheduled_messages.py +++ b/zerver/actions/scheduled_messages.py @@ -24,7 +24,7 @@ from zerver.lib.scheduled_messages import access_scheduled_message from zerver.lib.string_validation import check_stream_topic from zerver.models import Client, Realm, ScheduledMessage, Subscription, UserProfile from zerver.models.users import get_system_bot -from zerver.tornado.django_api import send_event +from zerver.tornado.django_api import send_event, send_event_on_commit SCHEDULED_MESSAGE_LATE_CUTOFF_MINUTES = 10 @@ -104,14 +104,15 @@ def do_schedule_messages( scheduled_message.has_attachment = True scheduled_message.save(update_fields=["has_attachment"]) - event = { - "type": "scheduled_messages", - "op": "add", - "scheduled_messages": [ - scheduled_message.to_dict() for scheduled_message, ignored in scheduled_messages - ], - } - send_event(sender.realm, event, [sender.id]) + event = { + "type": "scheduled_messages", + "op": "add", + "scheduled_messages": [ + scheduled_message.to_dict() for scheduled_message, ignored in scheduled_messages + ], + } + send_event_on_commit(sender.realm, event, [sender.id]) + return [scheduled_message.id for scheduled_message, ignored in scheduled_messages]