Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
deferred_work: Move all queries into the transaction.
Alex Vandiver75e9903be537
diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.pyindex 395b2022bdc..aa022608841 100644--- a/zerver/worker/queue_processors.py+++ b/zerver/worker/queue_processors.py@@ -1063,18 +1063,29 @@ class DeferredWorker(QueueProcessingWorker):batch_size = 100offset = 0while True:- messages = Message.objects.filter(- # Uses index: zerver_message_realm_recipient_id- realm_id=stream.realm_id,- recipient_id=event["stream_recipient_id"],- ).order_by("id")[offset : offset + batch_size]-with transaction.atomic(savepoint=False):+ # Note that this algorithm is susceptible to+ # leaving some messages unread if messages were to+ # be moved out of the stream while it was running,+ # causing the "offset" to potentially skip ahead,+ # over messages which have not been handled.+ # Since it is currently only run after a stream is+ # deactivated, this is not _currently_ possible.+ messages = (+ Message.objects.filter(+ # Uses index: zerver_message_realm_recipient_id+ realm_id=stream.realm_id,+ recipient_id=event["stream_recipient_id"],+ )+ .order_by("id")[offset : offset + batch_size]+ .values_list("id", flat=True)+ )+ message_count = messages.count()UserMessage.select_for_update_query().filter(message__in=messages).extra(where=[UserMessage.where_unread()]).update(flags=F("flags").bitor(UserMessage.flags.read))- offset += len(messages)- if len(messages) < batch_size:+ offset += message_count+ if message_count < batch_size:breaklogger.info("Marked %s messages as read for all users, stream_recipient_id %s",