Skip to content

Commit 21c12dd

Browse files
committed
fix(device-handler): make _maybe_retry_device_resync thread-safe
1 parent b8146d4 commit 21c12dd

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

changelog.d/18391.fix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent race-condition in `_maybe_retry_device_resync` entrance.

synapse/handlers/device.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121
#
2222
import logging
23+
from threading import Lock
2324
from typing import (
2425
TYPE_CHECKING,
2526
AbstractSet,
@@ -1237,7 +1238,7 @@ def __init__(self, hs: "HomeServer", device_handler: DeviceHandler):
12371238
)
12381239

12391240
# Attempt to resync out of sync device lists every 30s.
1240-
self._resync_retry_in_progress = False
1241+
self._resync_retry_lock = Lock()
12411242
self.clock.looping_call(
12421243
run_as_background_process,
12431244
30 * 1000,
@@ -1419,13 +1420,9 @@ async def _maybe_retry_device_resync(self) -> None:
14191420
"""Retry to resync device lists that are out of sync, except if another retry is
14201421
in progress.
14211422
"""
1422-
if self._resync_retry_in_progress:
1423+
if self._resync_retry_lock.locked():
14231424
return
1424-
1425-
try:
1426-
# Prevent another call of this function to retry resyncing device lists so
1427-
# we don't send too many requests.
1428-
self._resync_retry_in_progress = True
1425+
with self._resync_retry_lock:
14291426
# Get all of the users that need resyncing.
14301427
need_resync = await self.store.get_user_ids_requiring_device_list_resync()
14311428

@@ -1465,9 +1462,6 @@ async def _maybe_retry_device_resync(self) -> None:
14651462
user_id,
14661463
e,
14671464
)
1468-
finally:
1469-
# Allow future calls to retry resyncinc out of sync device lists.
1470-
self._resync_retry_in_progress = False
14711465

14721466
async def multi_user_device_resync(
14731467
self, user_ids: List[str], mark_failed_as_stale: bool = True

0 commit comments

Comments
 (0)