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 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-02 18:43:56 -07:00 • committed by Roland
parent 871a26324d
commit 0aa4019f86
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 35 additions and 1 deletions

View file

@ -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')

View file

@ -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):