Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add missing type hints to synapse.app. #11287

Merged
merged 13 commits into from
Nov 10, 2021
4 changes: 3 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ exclude = (?x)
|synapse/app/generic_worker.py
|synapse/app/homeserver.py
|synapse/app/media_repository.py
|synapse/app/phone_stats_home.py
|synapse/app/pusher.py
|synapse/app/synchrotron.py
|synapse/app/user_dir.py
Expand Down Expand Up @@ -181,6 +180,9 @@ exclude = (?x)
[mypy-synapse.api.*]
disallow_untyped_defs = True

[mypy-synapse.app.*]
disallow_untyped_defs = True

[mypy-synapse.crypto.*]
disallow_untyped_defs = True

Expand Down
24 changes: 0 additions & 24 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,6 @@ def format_config_error(e: ConfigError) -> Iterator[str]:


def run(hs: HomeServer):
PROFILE_SYNAPSE = False
if PROFILE_SYNAPSE:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this broken, sorry?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't work:

type object 'ThreadPool' has no attribute '_worker'

It also doesn't have anyway to enable it without modifying code, so I figure this seems less than useful. I believe that cProfile has too much overhead for actually running synapse, but 🤷 I can attempt to get it to work if you'd like.


def profile(func):
from cProfile import Profile
from threading import current_thread

def profiled(*args, **kargs):
profile = Profile()
profile.enable()
func(*args, **kargs)
profile.disable()
ident = current_thread().ident
profile.dump_stats(
"/tmp/%s.%s.%i.pstat" % (hs.hostname, func.__name__, ident)
)

return profiled

from twisted.python.threadpool import ThreadPool

ThreadPool._worker = profile(ThreadPool._worker)
reactor.run = profile(reactor.run)

_base.start_reactor(
"synapse-homeserver",
soft_file_limit=hs.config.server.soft_file_limit,
Expand Down
23 changes: 15 additions & 8 deletions synapse/app/phone_stats_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import math
import resource
import sys
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List, Sized, Tuple

from prometheus_client import Gauge

from synapse.metrics.background_process_metrics import wrap_as_background_process
from synapse.types import JsonDict

if TYPE_CHECKING:
from synapse.server import HomeServer
Expand All @@ -28,7 +29,7 @@

# Contains the list of processes we will be monitoring
# currently either 0 or 1
_stats_process = []
_stats_process: List[Tuple[int, "resource.struct_rusage"]] = []

# Gauges to expose monthly active user control metrics
current_mau_gauge = Gauge("synapse_admin_mau:current", "Current MAU")
Expand All @@ -45,9 +46,15 @@


@wrap_as_background_process("phone_stats_home")
async def phone_stats_home(hs: "HomeServer", stats, stats_process=_stats_process):
async def phone_stats_home(
hs: "HomeServer",
stats: JsonDict,
stats_process: List[Tuple[int, "resource.struct_rusage"]] = _stats_process,
) -> None:
logger.info("Gathering stats for reporting")
now = int(hs.get_clock().time())
# Ensure the homeserver has started.
assert hs.start_time is not None
uptime = int(now - hs.start_time)
if uptime < 0:
uptime = 0
Expand Down Expand Up @@ -146,15 +153,15 @@ async def phone_stats_home(hs: "HomeServer", stats, stats_process=_stats_process
logger.warning("Error reporting stats: %s", e)


def start_phone_stats_home(hs: "HomeServer"):
def start_phone_stats_home(hs: "HomeServer") -> None:
"""
Start the background tasks which report phone home stats.
"""
clock = hs.get_clock()

stats = {}
stats: JsonDict = {}

def performance_stats_init():
def performance_stats_init() -> None:
_stats_process.clear()
_stats_process.append(
(int(hs.get_clock().time()), resource.getrusage(resource.RUSAGE_SELF))
Expand All @@ -170,10 +177,10 @@ def performance_stats_init():
hs.get_datastore().reap_monthly_active_users()

@wrap_as_background_process("generate_monthly_active_users")
async def generate_monthly_active_users():
async def generate_monthly_active_users() -> None:
current_mau_count = 0
current_mau_count_by_service = {}
reserved_users = ()
reserved_users: Sized = ()
store = hs.get_datastore()
if hs.config.server.limit_usage_by_mau or hs.config.server.mau_stats_only:
current_mau_count = await store.get_monthly_active_count()
Expand Down