Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_edit: Defer cache invalidation to after transaction commit.

Alex Vandiver0a86e5e45108
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py
index 147e07cedf7..0144d15580e 100644
--- a/zerver/actions/message_edit.py
+++ b/zerver/actions/message_edit.py
@@ -951,11 +951,12 @@ def do_update_message(
save_changes_for_propagation_mode()
# Invalidate the message cache for all changed messages. They'll
- # be lazily rebuilt from the database on next access. The DB has
- # up-to-date content for all of them: the target message was saved
- # above, and propagated messages were updated by
- # save_changes_for_propagation_mode().
- cache_delete_many(to_dict_cache_key_id(msg_id) for msg_id in changed_message_ids)
+ # be lazily rebuilt from the database on next access. We defer
+ # this to after the transaction commits so that concurrent readers
+ # don't repopulate the cache with stale pre-commit data.
+ transaction.on_commit(
+ lambda: cache_delete_many(to_dict_cache_key_id(msg_id) for msg_id in changed_message_ids)
+ )
event["message_ids"] = sorted(changed_message_ids)
# The following blocks arranges that users who are subscribed to a
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index 4cf2b8d100d..84bb395d34a 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -199,21 +199,23 @@ class EditMessageTest(ZulipTestCase):
msg_id = self.send_stream_message(
self.example_user("hamlet"), "Denmark", topic_name="editing", content="before edit"
)
- result = self.client_patch(
- f"/json/messages/{msg_id}",
- {
- "content": "after edit",
- },
- )
+ with self.captureOnCommitCallbacks(execute=True):
+ result = self.client_patch(
+ f"/json/messages/{msg_id}",
+ {
+ "content": "after edit",
+ },
+ )
self.assert_json_success(result)
self.check_message(msg_id, topic_name="editing", content="after edit")
- result = self.client_patch(
- f"/json/messages/{msg_id}",
- {
- "topic": "edited",
- },
- )
+ with self.captureOnCommitCallbacks(execute=True):
+ result = self.client_patch(
+ f"/json/messages/{msg_id}",
+ {
+ "topic": "edited",
+ },
+ )
self.assert_json_success(result)
self.assertEqual(Message.objects.get(id=msg_id).topic_name(), "edited")