From a9af1c0ce24e8d973ec07223101834374110a8c1 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Mon, 19 Aug 2024 14:45:07 +0530 Subject: [PATCH] streams: Update do_change_stream_description to send event on commit. Earlier, we were using 'send_event' in do_change_stream_description which can lead to a situation where we enqueue events but the 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/streams.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/zerver/actions/streams.py b/zerver/actions/streams.py index 066f01da34..1d8bb6fcd2 100644 --- a/zerver/actions/streams.py +++ b/zerver/actions/streams.py @@ -1504,27 +1504,26 @@ def send_change_stream_description_notification( ) +@transaction.atomic(durable=True) def do_change_stream_description( stream: Stream, new_description: str, *, acting_user: UserProfile ) -> None: old_description = stream.description - - with transaction.atomic(): - stream.description = new_description - stream.rendered_description = render_stream_description(new_description, stream.realm) - stream.save(update_fields=["description", "rendered_description"]) - RealmAuditLog.objects.create( - realm=stream.realm, - acting_user=acting_user, - modified_stream=stream, - event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED, - event_time=timezone_now(), - extra_data={ - RealmAuditLog.OLD_VALUE: old_description, - RealmAuditLog.NEW_VALUE: new_description, - "property": "description", - }, - ) + stream.description = new_description + stream.rendered_description = render_stream_description(new_description, stream.realm) + stream.save(update_fields=["description", "rendered_description"]) + RealmAuditLog.objects.create( + realm=stream.realm, + acting_user=acting_user, + modified_stream=stream, + event_type=RealmAuditLog.STREAM_PROPERTY_CHANGED, + event_time=timezone_now(), + extra_data={ + RealmAuditLog.OLD_VALUE: old_description, + RealmAuditLog.NEW_VALUE: new_description, + "property": "description", + }, + ) event = dict( type="stream", @@ -1535,7 +1534,7 @@ def do_change_stream_description( value=new_description, rendered_description=stream.rendered_description, ) - send_event(stream.realm, event, can_access_stream_user_ids(stream)) + send_event_on_commit(stream.realm, event, can_access_stream_user_ids(stream)) send_change_stream_description_notification( stream,