From c30cda9ff23df8afe1a2fcfcef319141d08aa191 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:07:31 -0600 Subject: [PATCH] write migrated config atomically Config migration rewrote config.yml in place with open("w") after each step, so nginx's get_nginx_settings.py, which starts alongside frigate, could read an empty file, get None from yaml.load, and crash on config.get. Each step now writes to a temp file with the original mode and os.replace's it into place. --- frigate/util/config.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/frigate/util/config.py b/frigate/util/config.py index 634016a331..1555a903b4 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -212,6 +212,14 @@ def find_config_file() -> str: return config_path +def _write_config(config_file: str, yaml: YAML, config: dict[str, Any]) -> None: + tmp_file = f"{config_file}.tmp" + with open(tmp_file, "w") as f: + yaml.dump(config, f) + shutil.copymode(config_file, tmp_file) + os.replace(tmp_file, config_file) + + def migrate_frigate_config(config_file: str): """handle migrating the frigate config.""" logger.info("Checking if frigate config needs migration...") @@ -259,8 +267,7 @@ def migrate_frigate_config(config_file: str): if previous_version < "0.14": logger.info(f"Migrating frigate config from {previous_version} to 0.14...") new_config = migrate_014(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.14" logger.info("Migrating export file names...") @@ -277,56 +284,48 @@ def migrate_frigate_config(config_file: str): if previous_version < "0.15-0": logger.info(f"Migrating frigate config from {previous_version} to 0.15-0...") new_config = migrate_015_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.15-0" if previous_version < "0.15-1": logger.info(f"Migrating frigate config from {previous_version} to 0.15-1...") new_config = migrate_015_1(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.15-1" if previous_version < "0.16-0": logger.info(f"Migrating frigate config from {previous_version} to 0.16-0...") new_config = migrate_016_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.16-0" if previous_version < "0.17-0": logger.info(f"Migrating frigate config from {previous_version} to 0.17-0...") new_config = migrate_017_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.17-0" if previous_version < "0.18-0": logger.info(f"Migrating frigate config from {previous_version} to 0.18-0...") new_config = migrate_018_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.18-0" if previous_version < "0.19-0": logger.info(f"Migrating frigate config from {previous_version} to 0.19-0...") new_config = migrate_019_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.19-0" if needs_models: logger.info("Migrating frigate detectors and model to models...") new_config = migrate_models(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) if needs_detector_rename: logger.info("Migrating renamed frigate detectors...") new_config = rename_hailo_detector(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) logger.info("Finished frigate config migration...")