From 56ffcebdcfe0b1b39dde75673268aea1638ecabb Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 21 Mar 2026 21:12:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=8F=BA=20The=20remaining=20~79=20defer.*=20u?= =?UTF-8?q?sages=20are=20all=20in=20code=20that=20depends=20on=20the=20Twi?= =?UTF-8?q?sted=20reactor=20to=20schedule=20and=20resolve=20Deferreds.=20T?= =?UTF-8?q?hey'll=20be=20eliminated=20when=20the=20entry=20point=20switche?= =?UTF-8?q?s=20from=20reactor.run()=20to=20asyncio.run().=20At=20that=20po?= =?UTF-8?q?int:=20=20=20-=20defer.Deferred()=20=E2=86=92=20asyncio.Future(?= =?UTF-8?q?)=20or=20asyncio.Event()=20=20=20-=20defer.gatherResults()=20?= =?UTF-8?q?=E2=86=92=20asyncio.gather()=20=20=20-=20defer.succeed/fail=20?= =?UTF-8?q?=E2=86=92=20direct=20returns=20/=20asyncio.Future.set=5Fresult/?= =?UTF-8?q?set=5Fexception=20=20=20-=20defer.TimeoutError=20=E2=86=92=20as?= =?UTF-8?q?yncio.TimeoutError=20=20=20-=20defer.ensureDeferred=20=E2=86=92?= =?UTF-8?q?=20asyncio.ensure=5Ffuture=20or=20direct=20await=20=20=20-=20Li?= =?UTF-8?q?nearizer,=20ReadWriteLock,=20etc.=20=E2=86=92=20NativeLinearize?= =?UTF-8?q?r,=20NativeReadWriteLock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- synapse/handlers/auth.py | 2 +- synapse/logging/_remote.py | 2 +- synapse/types/__init__.py | 2 +- synapse/util/async_helpers.py | 18 +++++++++++++++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/synapse/handlers/auth.py b/synapse/handlers/auth.py index 8f5df65e4e..89163b615d 100644 --- a/synapse/handlers/auth.py +++ b/synapse/handlers/auth.py @@ -43,7 +43,7 @@ import unpaddedbase64 from prometheus_client import Counter try: - from twisted.internet.defer import CancelledError + from asyncio import CancelledError from twisted.web.server import Request except ImportError: pass diff --git a/synapse/logging/_remote.py b/synapse/logging/_remote.py index 0ac1f584b4..300d1e10a4 100644 --- a/synapse/logging/_remote.py +++ b/synapse/logging/_remote.py @@ -36,7 +36,7 @@ except ImportError: try: from twisted.application.internet import ClientService - from twisted.internet.defer import CancelledError + from asyncio import CancelledError from twisted.internet.defer import Deferred from twisted.internet.endpoints import ( HostnameEndpoint, diff --git a/synapse/types/__init__.py b/synapse/types/__init__.py index 09c71dfab0..400b5144aa 100644 --- a/synapse/types/__init__.py +++ b/synapse/types/__init__.py @@ -59,7 +59,7 @@ except ImportError: pass try: - from twisted.internet.defer import CancelledError + from asyncio import CancelledError from twisted.internet.interfaces import ( IReactorCore, IReactorPluggableNameResolver, diff --git a/synapse/util/async_helpers.py b/synapse/util/async_helpers.py index 80e1d5a944..97c6197252 100644 --- a/synapse/util/async_helpers.py +++ b/synapse/util/async_helpers.py @@ -1146,7 +1146,8 @@ class NativeLinearizer: # do work """ - def __init__(self, name: str, max_count: int = 1) -> None: + def __init__(self, name: str, max_count: int = 1, clock: Any = None) -> None: + # clock parameter accepted for backward compatibility with Linearizer(clock=...) self.name = name self.max_count = max_count self._key_to_entry: dict[Hashable, _NativeLinearizerEntry] = {} @@ -1350,7 +1351,8 @@ class NativeAwakenableSleeper: Allows explicit waking of sleeping coroutines by name. """ - def __init__(self) -> None: + def __init__(self, clock: Any = None) -> None: + # clock parameter accepted for backward compatibility self._streams: dict[str, set[asyncio.Event]] = {} def wake(self, name: str) -> None: @@ -1383,7 +1385,8 @@ class NativeEvent: Like threading.Event but for asyncio code. """ - def __init__(self) -> None: + def __init__(self, clock: Any = None) -> None: + # clock parameter accepted for backward compatibility self._event = asyncio.Event() def set(self) -> None: @@ -1490,3 +1493,12 @@ class DeferredEvent: pass return self.is_set() + + +# =========================================================================== +# The native asyncio versions (NativeLinearizer, NativeReadWriteLock, +# NativeAwakenableSleeper, NativeEvent) are available above and will replace +# the Deferred-based versions once the entry point switches from reactor.run() +# to asyncio.run(). Until then, the old classes remain as they need the +# Twisted reactor to drive their Deferred-based internals. +# ===========================================================================