Skip to content

Commit

Permalink
Greedy ioloop working (#1870)
Browse files Browse the repository at this point in the history
Enable the SYN_GREEDY_CORO environment variable
Set the ioloop into debug mode when this is enabled
This should be considered for use in development/debugging, not for general production use.
  • Loading branch information
vEpiphyte authored Sep 2, 2020
1 parent a021f23 commit 05bdf27
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions synapse/glob.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -47,21 +50,26 @@ 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)
_glob_thrd.start()

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
Expand Down

0 comments on commit 05bdf27

Please sign in to comment.