From 0aa4019f8672a38a5809503aa9b4c1f743afc88d Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Mon, 2 Nov 2026 18:43:56 -0700 Subject: [PATCH] A refresh that finishes before tags.yaml is imported no longer reports a failed save (#941) Every refresh fires save_tags_config at its cleanup. In the first seconds after a start, a refresh can finish before the startup import of tags.yaml, which on a rebuilt database happens while the first refresh runs. The dump then asked ConfigFile.save to write a database that held no tags over the file; the save guard refused, as it must, but it did so with an ERROR traceback and a "Failed to save config: config/tags.yaml" event for the user, although nothing was lost: the import that followed read the file and recreated the tags. save_tags_config now skips the dump while the config has not been imported and says so at WARNING. The file stays the source of truth until the import. Co-authored-by: Claude Fable 5.1 --- wrolpi/tags.py | 10 ++++++++- wrolpi/test/test_config_partial_import.py | 26 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/wrolpi/tags.py b/wrolpi/tags.py index 9d05b5fa..38f26262 100644 --- a/wrolpi/tags.py +++ b/wrolpi/tags.py @@ -909,7 +909,15 @@ def test_tags_config(): @register_switch_handler('save_tags_config') def save_tags_config(): """Schedule a background task to save all TagFiles to the config file. If testing, save synchronously.""" - get_tags_config().dump_config() + config = get_tags_config() + if config.get_file().exists() and not config.successful_import: + # Every refresh fires this switch at its cleanup, so a refresh in the first seconds after a start can get + # here before tags.yaml has been imported (a rebuilt database has nothing to dump yet). The file is the + # source of truth until the import, which still runs; dumping now would only have `save` refuse the + # write and send the user a "Failed to save config" error for a change nobody made. + logger.warning('Not saving tags.yaml: the config has not been imported yet') + return + config.dump_config() logger.info('save_tags_config complete') diff --git a/wrolpi/test/test_config_partial_import.py b/wrolpi/test/test_config_partial_import.py index 10fd5380..0d199403 100644 --- a/wrolpi/test/test_config_partial_import.py +++ b/wrolpi/test/test_config_partial_import.py @@ -10,6 +10,7 @@ refuses to overwrite an existing config while `import_skipped` is non-zero (unle `overwrite=True`). A later complete import resets the counter and dumps resume. """ from copy import deepcopy +from unittest import mock import pytest @@ -60,6 +61,31 @@ async def test_tags_partial_import_blocks_dump(async_client, test_session, test_ config.save(overwrite=True) +@pytest.mark.asyncio +async def test_tags_dump_before_import_is_skipped(async_client, test_session, test_directory, test_tags_config, + await_switches): + """Every refresh fires `save_tags_config` at its cleanup. A refresh that finishes before tags.yaml has been + imported (the first seconds after a start, with a rebuilt database) must not dump the empty database over the + file, and must not alarm the user either: the save guard would refuse the write with an error event, although + nothing was lost. The dump is skipped; the import that follows still reads the file.""" + from wrolpi import tags + config = get_tags_config() + file = config.get_file() + file.write_text(TAGS_YAML) + config.successful_import = False + before = file.read_text() + + with mock.patch('wrolpi.events.Events.send_config_save_failed') as send_failed: + tags.save_tags_config.activate_switch() + await await_switches() + send_failed.assert_not_called() + assert file.read_text() == before, 'A dump before the import must not modify the config file' + + config.import_config() + assert config.successful_import is True + assert test_session.query(Tag).filter_by(name='important').count() == 1 + + @pytest.mark.asyncio async def test_tags_import_recovers_after_file_indexed(async_client, test_session, test_directory, test_tags_config, await_switches):