From 3ea6ba97c06ba72a4f524bc75646222353c6a920 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Fri, 23 Aug 2024 14:02:27 +0530 Subject: [PATCH] users: Send event on commit in do_change_is_billing_admin. Earlier, we were using 'send_event' in 'do_change_is_billing_admin' 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/users.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zerver/actions/users.py b/zerver/actions/users.py index ddd887d017..dac7086624 100644 --- a/zerver/actions/users.py +++ b/zerver/actions/users.py @@ -50,7 +50,7 @@ from zerver.models.users import ( get_bot_dicts_in_realm, get_user_profile_by_id, ) -from zerver.tornado.django_api import send_event, send_event_on_commit +from zerver.tornado.django_api import send_event_on_commit if settings.BILLING_ENABLED: from corporate.lib.stripe import RealmBillingSession @@ -517,13 +517,14 @@ def do_change_user_role( send_stream_events_for_role_update(user_profile, previously_accessible_streams) +@transaction.atomic(savepoint=False) def do_change_is_billing_admin(user_profile: UserProfile, value: bool) -> None: user_profile.is_billing_admin = value user_profile.save(update_fields=["is_billing_admin"]) event = dict( type="realm_user", op="update", person=dict(user_id=user_profile.id, is_billing_admin=value) ) - send_event(user_profile.realm, event, get_user_ids_who_can_access_user(user_profile)) + send_event_on_commit(user_profile.realm, event, get_user_ids_who_can_access_user(user_profile)) def do_change_can_forge_sender(user_profile: UserProfile, value: bool) -> None: