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

Commit

Permalink
Fix bug in wait for stream position (#14872)
Browse files Browse the repository at this point in the history
This caused some requests to fail.

This caused some requests to fail.

This really only started causing issues due to #14856
  • Loading branch information
erikjohnston authored Jan 19, 2023
1 parent a7b54ca commit cdf2707
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.d/14872.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `wait_for_stream_position` to correctly wait for the right instance to advance its token.
29 changes: 19 additions & 10 deletions synapse/replication/tcp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def __init__(self, hs: "HomeServer"):
if hs.should_send_federation():
self.send_handler = FederationSenderHandler(hs)

# Map from stream to list of deferreds waiting for the stream to
# Map from stream and instance to list of deferreds waiting for the stream to
# arrive at a particular position. The lists are sorted by stream position.
self._streams_to_waiters: Dict[str, List[Tuple[int, Deferred]]] = {}
self._streams_to_waiters: Dict[Tuple[str, str], List[Tuple[int, Deferred]]] = {}

async def on_rdata(
self, stream_name: str, instance_name: str, token: int, rows: list
Expand Down Expand Up @@ -270,7 +270,7 @@ async def on_rdata(
# Notify any waiting deferreds. The list is ordered by position so we
# just iterate through the list until we reach a position that is
# greater than the received row position.
waiting_list = self._streams_to_waiters.get(stream_name, [])
waiting_list = self._streams_to_waiters.get((stream_name, instance_name), [])

# Index of first item with a position after the current token, i.e we
# have called all deferreds before this index. If not overwritten by
Expand All @@ -279,14 +279,13 @@ async def on_rdata(
# `len(list)` works for both cases.
index_of_first_deferred_not_called = len(waiting_list)

# We don't fire the deferreds until after we finish iterating over the
# list, to avoid the list changing when we fire the deferreds.
deferreds_to_callback = []

for idx, (position, deferred) in enumerate(waiting_list):
if position <= token:
try:
with PreserveLoggingContext():
deferred.callback(None)
except Exception:
# The deferred has been cancelled or timed out.
pass
deferreds_to_callback.append(deferred)
else:
# The list is sorted by position so we don't need to continue
# checking any further entries in the list.
Expand All @@ -297,6 +296,14 @@ async def on_rdata(
# loop. (This maintains the order so no need to resort)
waiting_list[:] = waiting_list[index_of_first_deferred_not_called:]

for deferred in deferreds_to_callback:
try:
with PreserveLoggingContext():
deferred.callback(None)
except Exception:
# The deferred has been cancelled or timed out.
pass

async def on_position(
self, stream_name: str, instance_name: str, token: int
) -> None:
Expand Down Expand Up @@ -349,7 +356,9 @@ async def wait_for_stream_position(
deferred, _WAIT_FOR_REPLICATION_TIMEOUT_SECONDS, self._reactor
)

waiting_list = self._streams_to_waiters.setdefault(stream_name, [])
waiting_list = self._streams_to_waiters.setdefault(
(stream_name, instance_name), []
)

waiting_list.append((position, deferred))
waiting_list.sort(key=lambda t: t[0])
Expand Down

0 comments on commit cdf2707

Please sign in to comment.