diff --git a/synapse/glob.py b/synapse/glob.py index 9bfa0c40c4..9d803359f4 100644 --- a/synapse/glob.py +++ b/synapse/glob.py @@ -1,9 +1,12 @@ import os import signal import asyncio +import logging import threading import faulthandler +logger = logging.getLogger(__name__) + _glob_loop = None _glob_thrd = None @@ -47,14 +50,12 @@ def initloop(): _glob_loop = asyncio.get_running_loop() # if we get here, it's us! _glob_thrd = threading.currentThread() + # Enable debug and greedy coro collection + setGreedCoro(_glob_loop) except RuntimeError: - - # otherwise, lets fire one... _glob_loop = asyncio.new_event_loop() - greedy_threshold = os.environ.get('SYN_GREEDY_CORO') - if greedy_threshold is not None: - _glob_loop.slow_callback_duration = float(greedy_threshold) + setGreedCoro(_glob_loop) _glob_thrd = threading.Thread(target=_glob_loop.run_forever, name='SynLoop') _glob_thrd.setDaemon(True) @@ -62,6 +63,13 @@ def initloop(): return _glob_loop +def setGreedCoro(loop: asyncio.AbstractEventLoop): + greedy_threshold = os.environ.get('SYN_GREEDY_CORO') + if greedy_threshold is not None: # pragma: no cover + logger.info(f'Setting ioloop.slow_callback_duration to {greedy_threshold}') + loop.set_debug(True) + loop.slow_callback_duration = float(greedy_threshold) + def iAmLoop(): initloop() return threading.currentThread() == _glob_thrd