From 6e349db4da08b96231b1c6785a8d30b160e9af18 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 7 Oct 2025 16:50:59 +0100 Subject: [PATCH] Allow Synapse modules to specify their own threadpool when calling `defer_to_thread` --- synapse/module_api/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 12a31dd2ab..2484661ab5 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -43,6 +43,7 @@ from typing_extensions import Concatenate, ParamSpec from twisted.internet import defer from twisted.internet.interfaces import IDelayedCall +from twisted.python.threadpool import ThreadPool from twisted.web.resource import Resource from synapse.api import errors @@ -78,7 +79,7 @@ from synapse.http.server import ( from synapse.http.servlet import parse_json_object_from_request from synapse.http.site import SynapseRequest from synapse.logging.context import ( - defer_to_thread, + defer_to_threadpool, make_deferred_yieldable, run_in_background, ) @@ -1716,6 +1717,7 @@ class ModuleApi: async def defer_to_thread( self, f: Callable[P, T], + threadpool: Optional[ThreadPool] = None, *args: P.args, **kwargs: P.kwargs, ) -> T: @@ -1731,7 +1733,14 @@ class ModuleApi: Returns: The return value of the function once ran in a thread. """ - return await defer_to_thread(self._hs.get_reactor(), f, *args, **kwargs) + # If a threadpool is not provided by the module, then use the default + # reactor threadpool of the homeserver. + if threadpool is None: + threadpool = self._hs.get_reactor().getThreadPool() + + return await defer_to_threadpool( + self._hs.get_reactor(), threadpool, f, *args, **kwargs + ) async def check_username(self, username: str) -> None: """Checks if the provided username uses the grammar defined in the Matrix