Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stack overflow triggered by casefolding script #405

Merged
merged 10 commits into from
Sep 28, 2021
1 change: 1 addition & 0 deletions changelog.d/405.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix stack overflow caused by settign up logging multiple times in casefolding script.
Azrenbeth marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 26 additions & 14 deletions sydent/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def parse_from_config_parser(self, cfg: ConfigParser) -> bool:
def parse_config_file(self, config_file: str) -> None:
"""
Parse the given config from a filepath, populating missing items and
sections. NOTE: this method also sets up logging.
sections.

:param config_file: the file to be parsed
"""
Expand All @@ -236,13 +236,8 @@ def parse_config_file(self, config_file: str) -> None:

cfg.read(config_file)

# Logging is configured in cfg, but these options must be parsed first
# so that we can log while parsing the rest
setup_logging(cfg)

# TODO: Don't alter config file when starting Sydent so that
# it can be set to read-only

needs_saving = self.parse_from_config_parser(cfg)

if needs_saving:
Expand Down Expand Up @@ -271,13 +266,31 @@ def parse_config_dict(self, config_dict: Dict) -> None:
for option, value in section_dict.items():
cfg.set(section, option, value)

# This is only ever called by tests so don't configure logging
# as tests do this themselves

self.parse_from_config_parser(cfg)


def setup_logging(cfg: ConfigParser) -> None:
def setup_logging_from_file(config_file: str) -> None:
"""
Parse the given config from a filepath, populating missing items and
sections. NOTE: this method also sets up logging.
Azrenbeth marked this conversation as resolved.
Show resolved Hide resolved

:param config_file: the file to be parsed
"""
cfg = ConfigParser()
if os.path.exists(config_file):
cfg.read(config_file)

path = cfg.get(
"general", "log.path", fallback=CONFIG_DEFAULTS.get("general").get("log.path")
)
level = cfg.get(
"general", "log.level", fallback=CONFIG_DEFAULTS.get("general").get("log.level")
)

_setup_logging(path, level)


def _setup_logging(log_path: str, log_level: str) -> None:
"""
Setup logging using the options selected in the config

Expand All @@ -286,10 +299,9 @@ def setup_logging(cfg: ConfigParser) -> None:
log_format = "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s" " - %(message)s"
formatter = logging.Formatter(log_format)

logPath = cfg.get("general", "log.path")
if logPath != "":
if log_path != "":
handler: logging.StreamHandler = logging.handlers.TimedRotatingFileHandler(
logPath, when="midnight", backupCount=365
log_path, when="midnight", backupCount=365
)
handler.setFormatter(formatter)

Expand All @@ -303,7 +315,7 @@ def sighup(signum, stack):

handler.setFormatter(formatter)
rootLogger = logging.getLogger("")
rootLogger.setLevel(cfg.get("general", "log.level"))
rootLogger.setLevel(log_level)
rootLogger.addHandler(handler)

observer = log.PythonLoggingObserver()
Expand Down
7 changes: 5 additions & 2 deletions sydent/sydent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import twisted.internet.reactor
from twisted.internet import address, task

from sydent.config import SydentConfig
from sydent.config import SydentConfig, setup_logging_from_file
from sydent.db.hashing_metadata import HashingMetadataStore
from sydent.db.sqlitedb import SqliteDatabase
from sydent.db.valsession import ThreePidValSessionStore
Expand Down Expand Up @@ -307,8 +307,11 @@ def run_gc():


if __name__ == "__main__":
config_file = get_config_file_path()
setup_logging_from_file(config_file)

sydent_config = SydentConfig()
sydent_config.parse_config_file(get_config_file_path())
sydent_config.parse_config_file(config_file)

syd = Sydent(sydent_config)
syd.run()