From 56ac6204b0966d09279099346d28873fa4507cba Mon Sep 17 00:00:00 2001 From: chodeus <190988615+chodeus@users.noreply.github.com> Date: Thu, 15 Oct 2026 14:36:04 +0800 Subject: [PATCH 1/4] fix: keep Docker-set variables when the WebUI saves settings, and name the ones it can't apply --- app/main.py | 33 +++++++++++++-------- app/static/app.js | 7 ++++- app/webui.py | 13 ++++---- docs/configuration.md | 2 +- tests/test_config_save_env.py | 56 +++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 tests/test_config_save_env.py diff --git a/app/main.py b/app/main.py index 06bb6d6..11f4258 100644 --- a/app/main.py +++ b/app/main.py @@ -3403,6 +3403,25 @@ def _load_config(): ) +def _sync_env_from_config_file(config_dir): + """Copy beatscheck.conf into os.environ except Docker-set vars; returns the keys Docker overrides, None if unreadable.""" + path = os.path.join(config_dir, "beatscheck.conf") + try: + file_vals = _parse_config_lines(path) + except OSError: + return None + overridden = [] + for key, value in file_vals.items(): + env_name = _CONFIG_KEY_MAP.get(key) + if not env_name: + continue + if env_name not in _docker_env_vars: + os.environ[env_name] = value + elif os.environ.get(env_name) != value: + overridden.append(key) + return overridden + + def _reload_config(cfg): """Re-read beatscheck.conf and update cfg for the next scan. @@ -3410,21 +3429,9 @@ def _reload_config(cfg): update the os.environ entries that _apply_config_file originally set, then we refresh the cfg namespace from os.environ. """ - path = os.path.join(cfg.log_dir, "beatscheck.conf") - try: - file_vals = {} - for key, value in _parse_config_lines(path).items(): - env_name = _CONFIG_KEY_MAP.get(key) - if env_name: - file_vals[env_name] = value - except OSError: + if _sync_env_from_config_file(cfg.log_dir) is None: return - # Update env vars from config file (skip Docker-set vars) - for env_name, value in file_vals.items(): - if env_name not in _docker_env_vars: - os.environ[env_name] = value - # Refresh cfg attributes from env vars cfg.input_folder = os.environ.get( "MUSIC_DIR", cfg.input_folder).rstrip("/") diff --git a/app/static/app.js b/app/static/app.js index 2c56f1a..aee69fb 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -1520,7 +1520,12 @@ async function saveConfig(e) { if (res && res.ok) { status.textContent = 'Saved!'; status.className = 'form-status'; - showToast('Configuration saved', 'success'); + const dockerSet = Array.isArray(res.docker_set) ? res.docker_set : []; + if (dockerSet.length) { + showToast('Configuration saved. Set by a Docker variable, so not applied: ' + dockerSet.join(', '), 'warning'); + } else { + showToast('Configuration saved', 'success'); + } configSnapshot = buildConfigSnapshot(); updateUnsavedIndicator(); } else { diff --git a/app/webui.py b/app/webui.py index e9bdcae..7c3e3ff 100644 --- a/app/webui.py +++ b/app/webui.py @@ -372,8 +372,8 @@ def _login_record_success(key): # allowed-keys list and parser stay in sync without duplication. from main import ( # noqa: E402 _CONFIG_KEY_MAP, _acquire_scan_lock, _load_corrupt_details, - _parse_config_lines, _release_scan_lock, write_corrupt_list, - write_json_atomic, write_text_atomic, + _parse_config_lines, _release_scan_lock, _sync_env_from_config_file, + write_corrupt_list, write_json_atomic, write_text_atomic, ) import reason_text # noqa: E402 _ALLOWED_CONFIG_KEYS = frozenset(_CONFIG_KEY_MAP) @@ -1223,12 +1223,9 @@ def do_POST(self): del updates['lidarr_api_key'] ok = _write_config_file(config_dir, updates) if ok: - # Sync updated values to os.environ so other - # operations (e.g. delete) see them immediately. - for key, val in updates.items(): - env_name = key.upper() - os.environ[env_name] = str(val) - self._json_response({"ok": True}) + # Deletes read os.environ, so apply the save now; Docker-set vars still win. + overridden = _sync_env_from_config_file(config_dir) or [] + self._json_response({"ok": True, "docker_set": overridden}) else: self._json_response( {"error": "config file not found"}, 500) diff --git a/docs/configuration.md b/docs/configuration.md index 9b9ac79..966996c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -23,7 +23,7 @@ On first run, BeatsCheck creates `/config/beatscheck.conf` with all options and | `webui` | `false` | Enable the built-in [web interface](webui.md). Requires a published port | | `webui_port` | `8484` | Port for the web interface | -Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set. A value saved in the WebUI overrides the environment variable until the container restarts. +Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set, including values saved in the WebUI. A WebUI save that a variable overrides says so, and the saved value applies once the variable is removed. ## Container environment variables diff --git a/tests/test_config_save_env.py b/tests/test_config_save_env.py new file mode 100644 index 0000000..e69bf45 --- /dev/null +++ b/tests/test_config_save_env.py @@ -0,0 +1,56 @@ +"""A WebUI settings save applies at once, but a Docker-set variable still wins and the reply names it.""" + +import json +import os +import sys +import urllib.request + +import pytest + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "app")) + +import main # noqa: E402 +import webui # noqa: E402 + + +@pytest.fixture +def save(webui_server, tmp_path, monkeypatch): + """POST /api/config with the login off and MODE=report set by Docker; returns the reply.""" + monkeypatch.setattr(os, "environ", {"MODE": "report"}) + monkeypatch.setattr(main, "_docker_env_vars", {"MODE"}) + (tmp_path / "beatscheck.conf").write_text('mode = report\nlidarr_url = ""\n') + webui._save_auth_off(str(tmp_path)) + + def post(config): + req = urllib.request.Request( + webui_server + "/api/config", data=json.dumps({"config": config}).encode(), + headers={"Content-Type": "application/json"}, method="POST") + with urllib.request.urlopen(req, timeout=10) as resp: + return json.loads(resp.read()) + return post + + +def test_a_save_keeps_a_docker_set_variable_and_names_it(save): + reply = save({"mode": "setup", "lidarr_url": "http://lidarr:8686"}) + assert os.environ["MODE"] == "report" + assert reply["docker_set"] == ["mode"] + + +def test_a_save_applies_a_setting_no_docker_variable_sets(save): + save({"lidarr_url": "http://lidarr:8686"}) + assert os.environ["LIDARR_URL"] == "http://lidarr:8686" + + +def test_a_docker_set_variable_the_save_agrees_with_is_not_named(save): + reply = save({"mode": "report"}) + assert reply["docker_set"] == [] + + +def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch): + monkeypatch.setattr(os, "environ", {"CONFIG_DIR": str(tmp_path), "MODE": "report"}) + monkeypatch.setattr(main, "_docker_env_vars", set()) + monkeypatch.setattr(sys, "argv", ["main.py"]) + cfg = main._load_config() + (tmp_path / "beatscheck.conf").write_text("mode = move\n") + main._reload_config(cfg) + assert cfg.saved_mode == "report" From 51c38bea819614480ab9fa3e741b523e6618d06f Mon Sep 17 00:00:00 2001 From: chodeus <190988615+chodeus@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:24:33 +0800 Subject: [PATCH 2/4] fix: reply with an error when a WebUI save can't read beatscheck.conf back --- app/main.py | 4 +++- app/webui.py | 8 ++++++-- docs/configuration.md | 2 +- tests/test_config_save_env.py | 27 ++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 11f4258..463c75e 100644 --- a/app/main.py +++ b/app/main.py @@ -3404,8 +3404,10 @@ def _load_config(): def _sync_env_from_config_file(config_dir): - """Copy beatscheck.conf into os.environ except Docker-set vars; returns the keys Docker overrides, None if unreadable.""" + """Copy beatscheck.conf into os.environ except Docker-set vars; returns keys Docker overrides, None if no readable file.""" path = os.path.join(config_dir, "beatscheck.conf") + if not os.path.isfile(path): + return None try: file_vals = _parse_config_lines(path) except OSError: diff --git a/app/webui.py b/app/webui.py index 7c3e3ff..4f5e718 100644 --- a/app/webui.py +++ b/app/webui.py @@ -1224,8 +1224,12 @@ def do_POST(self): ok = _write_config_file(config_dir, updates) if ok: # Deletes read os.environ, so apply the save now; Docker-set vars still win. - overridden = _sync_env_from_config_file(config_dir) or [] - self._json_response({"ok": True, "docker_set": overridden}) + overridden = _sync_env_from_config_file(config_dir) + if overridden is None: + self._json_response( + {"error": "config saved, but it could not be read back to apply"}, 500) + else: + self._json_response({"ok": True, "docker_set": overridden}) else: self._json_response( {"error": "config file not found"}, 500) diff --git a/docs/configuration.md b/docs/configuration.md index 966996c..6d73d18 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -23,7 +23,7 @@ On first run, BeatsCheck creates `/config/beatscheck.conf` with all options and | `webui` | `false` | Enable the built-in [web interface](webui.md). Requires a published port | | `webui_port` | `8484` | Port for the web interface | -Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set, including values saved in the WebUI. A WebUI save that a variable overrides says so, and the saved value applies once the variable is removed. +Environment variables (uppercase, e.g. `MODE`, `WORKERS`) override the config file if set, including values saved in the WebUI. A WebUI save that a variable overrides says so, and the saved value applies once you remove the variable and recreate the container. ## Container environment variables diff --git a/tests/test_config_save_env.py b/tests/test_config_save_env.py index e69bf45..f6a2821 100644 --- a/tests/test_config_save_env.py +++ b/tests/test_config_save_env.py @@ -3,6 +3,7 @@ import json import os import sys +import urllib.error import urllib.request import pytest @@ -25,7 +26,7 @@ def post(config): req = urllib.request.Request( webui_server + "/api/config", data=json.dumps({"config": config}).encode(), headers={"Content-Type": "application/json"}, method="POST") - with urllib.request.urlopen(req, timeout=10) as resp: + with urllib.request.urlopen(req, timeout=10) as resp: # nosec B310 - the fixture's http://127.0.0.1 URL return json.loads(resp.read()) return post @@ -46,6 +47,30 @@ def test_a_docker_set_variable_the_save_agrees_with_is_not_named(save): assert reply["docker_set"] == [] +def test_a_save_it_cannot_read_back_replies_with_an_error(save, monkeypatch): + def unreadable(path): + raise OSError("unreadable") + monkeypatch.setattr(main, "_parse_config_lines", unreadable) + with pytest.raises(urllib.error.HTTPError) as err: + save({"lidarr_url": "http://lidarr:8686"}) + assert err.value.code == 500 + assert "LIDARR_URL" not in os.environ + + +def test_a_save_whose_file_is_gone_before_it_applies_replies_with_an_error(save, monkeypatch): + write = webui._write_config_file + + def write_then_remove(config_dir, updates): + ok = write(config_dir, updates) + os.remove(os.path.join(config_dir, "beatscheck.conf")) + return ok + monkeypatch.setattr(webui, "_write_config_file", write_then_remove) + with pytest.raises(urllib.error.HTTPError) as err: + save({"lidarr_url": "http://lidarr:8686"}) + assert err.value.code == 500 + assert "LIDARR_URL" not in os.environ + + def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch): monkeypatch.setattr(os, "environ", {"CONFIG_DIR": str(tmp_path), "MODE": "report"}) monkeypatch.setattr(main, "_docker_env_vars", set()) From c6a96c60bec0ce54c96372fe4755bb8c6493ab94 Mon Sep 17 00:00:00 2001 From: chodeus <190988615+chodeus@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:19:58 +0800 Subject: [PATCH 3/4] fix: keep variables set at a path-argument start, and name only the overrides a save posted --- app/main.py | 2 +- app/webui.py | 2 +- tests/test_config_save_env.py | 13 ++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index 463c75e..a21486b 100644 --- a/app/main.py +++ b/app/main.py @@ -3352,6 +3352,7 @@ def _load_config(): Priority: env vars > beatscheck.conf > defaults. """ + _snapshot_docker_env() if len(sys.argv) == 4: input_folder = sys.argv[1].rstrip("/") output_folder = sys.argv[2].rstrip("/") @@ -3362,7 +3363,6 @@ def _load_config(): # Write default config template on first run, then load values. # Config file values fill in for any env vars not already set. _write_default_config(log_dir) - _snapshot_docker_env() _apply_config_file(log_dir) input_folder = os.environ.get("MUSIC_DIR", "/data").rstrip("/") output_folder = os.environ.get("OUTPUT_DIR", "/data/corrupted").rstrip("/") diff --git a/app/webui.py b/app/webui.py index 4f5e718..95a4090 100644 --- a/app/webui.py +++ b/app/webui.py @@ -1229,7 +1229,7 @@ def do_POST(self): self._json_response( {"error": "config saved, but it could not be read back to apply"}, 500) else: - self._json_response({"ok": True, "docker_set": overridden}) + self._json_response({"ok": True, "docker_set": [k for k in overridden if k in updates]}) else: self._json_response( {"error": "config file not found"}, 500) diff --git a/tests/test_config_save_env.py b/tests/test_config_save_env.py index f6a2821..68dd874 100644 --- a/tests/test_config_save_env.py +++ b/tests/test_config_save_env.py @@ -26,7 +26,7 @@ def post(config): req = urllib.request.Request( webui_server + "/api/config", data=json.dumps({"config": config}).encode(), headers={"Content-Type": "application/json"}, method="POST") - with urllib.request.urlopen(req, timeout=10) as resp: # nosec B310 - the fixture's http://127.0.0.1 URL + with urllib.request.urlopen(req, timeout=10) as resp: # nosec B310 # the fixture's http://127.0.0.1 URL return json.loads(resp.read()) return post @@ -47,6 +47,12 @@ def test_a_docker_set_variable_the_save_agrees_with_is_not_named(save): assert reply["docker_set"] == [] +def test_a_save_names_only_the_docker_set_keys_it_saved(save, tmp_path): + (tmp_path / "beatscheck.conf").write_text('mode = setup\nlidarr_url = ""\n') + reply = save({"lidarr_url": "http://lidarr:8686"}) + assert reply["docker_set"] == [] + + def test_a_save_it_cannot_read_back_replies_with_an_error(save, monkeypatch): def unreadable(path): raise OSError("unreadable") @@ -71,10 +77,11 @@ def write_then_remove(config_dir, updates): assert "LIDARR_URL" not in os.environ -def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch): +@pytest.mark.parametrize("paths", [[], ["music", "out", "beats_check.log"]], ids=["docker", "path-arguments"]) +def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch, paths): monkeypatch.setattr(os, "environ", {"CONFIG_DIR": str(tmp_path), "MODE": "report"}) monkeypatch.setattr(main, "_docker_env_vars", set()) - monkeypatch.setattr(sys, "argv", ["main.py"]) + monkeypatch.setattr(sys, "argv", ["main.py"] + [str(tmp_path / p) for p in paths]) cfg = main._load_config() (tmp_path / "beatscheck.conf").write_text("mode = move\n") main._reload_config(cfg) From a031c043951c2198f60bc63a2c04d828e5fa53f1 Mon Sep 17 00:00:00 2001 From: chodeus <190988615+chodeus@users.noreply.github.com> Date: Thu, 15 Oct 2026 18:29:24 +0800 Subject: [PATCH 4/4] fix: apply beatscheck.conf read-backs one at a time, so an older read can't land after a newer save --- app/main.py | 12 +++++++++++- tests/test_config_save_env.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index a21486b..a8b00d5 100644 --- a/app/main.py +++ b/app/main.py @@ -9,6 +9,7 @@ import signal import subprocess import sys +import threading import time import types import urllib.error @@ -3403,9 +3404,18 @@ def _load_config(): ) +_config_sync_lock = threading.Lock() + + def _sync_env_from_config_file(config_dir): """Copy beatscheck.conf into os.environ except Docker-set vars; returns keys Docker overrides, None if no readable file.""" - path = os.path.join(config_dir, "beatscheck.conf") + # One at a time, or an older read of the file can be applied after a newer one. + with _config_sync_lock: + return _copy_config_file_to_env(os.path.join(config_dir, "beatscheck.conf")) + + +def _copy_config_file_to_env(path): + """_sync_env_from_config_file's work, run under _config_sync_lock.""" if not os.path.isfile(path): return None try: diff --git a/tests/test_config_save_env.py b/tests/test_config_save_env.py index 68dd874..a402895 100644 --- a/tests/test_config_save_env.py +++ b/tests/test_config_save_env.py @@ -3,6 +3,7 @@ import json import os import sys +import threading import urllib.error import urllib.request @@ -77,6 +78,33 @@ def write_then_remove(config_dir, updates): assert "LIDARR_URL" not in os.environ +@pytest.mark.parametrize("older", ["save", "reload"]) +def test_an_older_read_of_the_file_is_not_applied_after_a_newer_save(save, tmp_path, monkeypatch, older): + parse, paused, go = main._parse_config_lines, threading.Event(), threading.Event() + + def pause_first_read(path): + vals = parse(path) + if not paused.is_set(): + paused.set() + go.wait(5) + return vals + monkeypatch.setattr(main, "_parse_config_lines", pause_first_read) + if older == "save": + first = threading.Thread(target=save, args=({"lidarr_url": "http://old"},)) + else: + first = threading.Thread(target=main._sync_env_from_config_file, args=(str(tmp_path),)) + first.start() + reached = paused.wait(5) + second = threading.Thread(target=save, args=({"lidarr_url": "http://new"},)) + second.start() + second.join(0.5) + go.set() + first.join(5) + second.join(5) + assert reached + assert os.environ["LIDARR_URL"] == "http://new" + + @pytest.mark.parametrize("paths", [[], ["music", "out", "beats_check.log"]], ids=["docker", "path-arguments"]) def test_the_config_reload_keeps_a_docker_set_variable(tmp_path, monkeypatch, paths): monkeypatch.setattr(os, "environ", {"CONFIG_DIR": str(tmp_path), "MODE": "report"})