From fac9acdd5197f4e679c4013719c44e402ab62ecd Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 4 Oct 2026 01:25:02 +0100 Subject: [PATCH] Raise Twisted's TCP SEND_LIMIT to 4MiB on non-Windows platforms Twisted only sends 128KiB per write to a TCP socket on each reactor tick (a workaround for Windows), which throttles large responses on a busy reactor even when the kernel send buffer has room. --- synapse/app/_base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 7efa6d8fe2..5ab35d1e96 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -53,7 +53,7 @@ from twisted.internet.interfaces import ( IReactorUNIX, ) from twisted.internet.protocol import ServerFactory -from twisted.internet.tcp import Port +from twisted.internet.tcp import Connection, Port from twisted.logger import LoggingFile, LogLevel from twisted.protocols.tls import TLSMemoryBIOFactory from twisted.python.threadpool import ThreadPool @@ -685,6 +685,14 @@ async def start(hs: "HomeServer", *, freeze: bool = True) -> None: GAIResolver(reactor, getThreadPool=lambda: resolver_threadpool) ) + # Twisted sends at most `SEND_LIMIT` (128KiB) per write to a TCP socket on + # each reactor tick, to work around Windows mishandling large sends. On a + # busy reactor that throttles large responses to 128KiB per tick, even when + # the kernel send buffer has plenty of room. Raise it to 4MiB (the default + # maximum TCP send buffer on Linux) so each tick can fill the send buffer. + if sys.platform != "win32": + Connection.SEND_LIMIT = 4 * 1024 * 1024 + # Register the threadpools with our metrics. register_threadpool( name="default", server_name=server_name, threadpool=reactor.getThreadPool()