Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix a race in transaction queue #1930

Merged
merged 1 commit into from
Feb 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions synapse/federation/transaction_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,10 @@ def _attempt_new_transaction(self, destination):
try:
self.pending_transactions[destination] = 1

# XXX: what's this for?
yield run_on_reactor()

while True:
pending_pdus = self.pending_pdus_by_dest.pop(destination, [])
pending_edus = self.pending_edus_by_dest.pop(destination, [])
pending_presence = self.pending_presence_by_dest.pop(destination, {})
pending_failures = self.pending_failures_by_dest.pop(destination, [])

pending_edus.extend(
self.pending_edus_keyed_by_dest.pop(destination, {}).values()
)

limiter = yield get_retry_limiter(
destination,
self.clock,
Expand All @@ -326,6 +318,24 @@ def _attempt_new_transaction(self, destination):
yield self._get_new_device_messages(destination)
)

# BEGIN CRITICAL SECTION
#
# In order to avoid a race condition, we need to make sure that
# the following code (from popping the queues up to the point
# where we decide if we actually have any pending messages) is
# atomic - otherwise new PDUs or EDUs might arrive in the
# meantime, but not get sent because we hold the
# pending_transactions flag.

pending_pdus = self.pending_pdus_by_dest.pop(destination, [])
pending_edus = self.pending_edus_by_dest.pop(destination, [])
pending_presence = self.pending_presence_by_dest.pop(destination, {})
pending_failures = self.pending_failures_by_dest.pop(destination, [])

pending_edus.extend(
self.pending_edus_keyed_by_dest.pop(destination, {}).values()
)

pending_edus.extend(device_message_edus)
if pending_presence:
pending_edus.append(
Expand Down Expand Up @@ -355,6 +365,8 @@ def _attempt_new_transaction(self, destination):
)
return

# END CRITICAL SECTION

success = yield self._send_new_transaction(
destination, pending_pdus, pending_edus, pending_failures,
limiter=limiter,
Expand Down