mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
The inventory legacy-date repair no longer takes the save lock during import (#915)
MultiFileConfig.initialize() imports every file while holding the shared save lock, which is not reentrant. The repair added in #901 took that lock again from inside import_all, so any WROLPi whose inventory still held epoch-second dates hung every API process at startup. The repair now only rewrites the in-memory entities during import and records which inventories changed. import_inventories_config() writes those files once it runs, outside the lock. A regression test runs initialize() against an epoch-date file with a timeout. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
7f275284a1
commit
31b47dacf8
2 changed files with 57 additions and 9 deletions
|
|
@ -88,22 +88,39 @@ class InventoriesConfig(MultiFileConfig):
|
|||
# Exclude reserved files (e.g. catalog.yaml) that share the directory but are not inventories.
|
||||
return [slug for slug in super().discover() if slug not in RESERVED_SLUGS]
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
# Slugs repaired in memory by `_repair_legacy_dates` whose files still need to be written.
|
||||
self._legacy_date_slugs: set = set()
|
||||
|
||||
def import_all(self, send_events: bool = False):
|
||||
super().import_all(send_events=send_events)
|
||||
self._repair_legacy_dates()
|
||||
|
||||
def _repair_legacy_dates(self):
|
||||
"""One-time repair: inventories migrated before dates were normalized hold epoch seconds in their date
|
||||
fields. Rewrite those files as YYYY-MM-DD so every reader (API, MCP, frontend) sees a calendar date."""
|
||||
"""Inventories migrated before dates were normalized hold epoch seconds in their date fields. Repair them in
|
||||
memory and remember the slugs; `persist_legacy_date_repairs` writes the files.
|
||||
|
||||
This runs under `save_lock()` when called from `initialize()`, and that lock is not reentrant, so nothing
|
||||
here may take the lock or write a file (doing so hung every API process at startup)."""
|
||||
for slug in list(self._configs.keys()):
|
||||
entity = dict(self._configs[slug])
|
||||
items = [dict(i) for i in entity.get('items') or [] if isinstance(i, dict)]
|
||||
if not normalize_item_dates(items, entity.get('fields') or []):
|
||||
continue
|
||||
entity['items'] = items
|
||||
self._configs[slug] = entity
|
||||
self._legacy_date_slugs.add(slug)
|
||||
|
||||
def persist_legacy_date_repairs(self):
|
||||
"""One-time write of every inventory repaired by `_repair_legacy_dates`, so every reader (API, MCP,
|
||||
frontend) sees calendar dates. Takes the save lock: call it only where the lock is not already held."""
|
||||
while self._legacy_date_slugs:
|
||||
slug = self._legacy_date_slugs.pop()
|
||||
if slug not in self._configs:
|
||||
continue
|
||||
try:
|
||||
with self.save_lock():
|
||||
self._configs[slug] = entity
|
||||
self._persist_slug(slug)
|
||||
logger.info(f'Normalized legacy epoch dates in inventory {slug}')
|
||||
except Exception as e:
|
||||
|
|
@ -324,6 +341,7 @@ def import_inventories_config():
|
|||
except Exception as e:
|
||||
logger.error('Failed to migrate legacy inventories', exc_info=e)
|
||||
config.import_all()
|
||||
config.persist_legacy_date_repairs()
|
||||
config.seed_defaults()
|
||||
|
||||
# Load the shared food catalog and merge in any new shipped defaults.
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import threading
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
|
|
@ -77,22 +79,28 @@ def test_migrate_converts_epoch_expiration_dates(test_inventory_configs):
|
|||
assert '1893456000' not in config.get_file(inventory['slug']).read_text()
|
||||
|
||||
|
||||
def test_import_repairs_legacy_epoch_dates(test_inventory_configs):
|
||||
"""Inventories migrated before dates were normalized are rewritten once on import."""
|
||||
config = test_inventory_configs
|
||||
def _write_epoch_inventory(config, version=3):
|
||||
directory = config.get_directory()
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
file = directory / 'food.yaml'
|
||||
with file.open('wt') as fh:
|
||||
yaml.dump(dict(
|
||||
version=3, slug='food', name='Food', type='food', created_at='2024-01-01', viewed_at='2024-01-01',
|
||||
version=version, slug='food', name='Food', type='food', created_at='2024-01-01', viewed_at='2024-01-01',
|
||||
fields=[dict(key='name', label='Name', type='text', order=0),
|
||||
dict(key='expiration_date', label='Expires', type='date', order=1)],
|
||||
items=[dict(id=1, name='Salt', expiration_date='1893456000'),
|
||||
dict(id=2, name='Rice', expiration_date='2030-06-01')],
|
||||
), fh)
|
||||
return file
|
||||
|
||||
config.import_all()
|
||||
|
||||
def test_import_repairs_legacy_epoch_dates(test_inventory_configs):
|
||||
"""Inventories migrated before dates were normalized are rewritten once by the startup import."""
|
||||
from modules.inventory.common import import_inventories_config
|
||||
config = test_inventory_configs
|
||||
file = _write_epoch_inventory(config)
|
||||
|
||||
import_inventories_config()
|
||||
inventory = config.get_inventory('food')
|
||||
assert [i['expiration_date'] for i in inventory['items']] == ['2030-01-01', '2030-06-01']
|
||||
# Repaired on disk, with a version bump so stale clients notice.
|
||||
|
|
@ -100,7 +108,29 @@ def test_import_repairs_legacy_epoch_dates(test_inventory_configs):
|
|||
assert inventory['version'] == 4
|
||||
|
||||
# A second import finds nothing to repair and does not rewrite the file.
|
||||
config.import_all()
|
||||
import_inventories_config()
|
||||
assert config.get_inventory('food')['version'] == 4
|
||||
|
||||
|
||||
def test_initialize_repairs_legacy_epoch_dates_without_deadlock(test_inventory_configs):
|
||||
"""`initialize()` imports while holding the non-reentrant save lock; the repair must not take it again.
|
||||
|
||||
Every API process initializes the inventory store at startup, so a deadlock here hangs the whole API."""
|
||||
config = test_inventory_configs
|
||||
file = _write_epoch_inventory(config)
|
||||
|
||||
thread = threading.Thread(target=config.initialize, daemon=True)
|
||||
thread.start()
|
||||
thread.join(timeout=10)
|
||||
assert not thread.is_alive(), 'initialize() deadlocked while repairing legacy epoch dates'
|
||||
|
||||
# Repaired in memory, but not written while the lock was held.
|
||||
assert [i['expiration_date'] for i in config.get_inventory('food')['items']] == ['2030-01-01', '2030-06-01']
|
||||
assert '1893456000' in file.read_text()
|
||||
|
||||
# The write happens later, outside the lock.
|
||||
config.persist_legacy_date_repairs()
|
||||
assert '1893456000' not in file.read_text()
|
||||
assert config.get_inventory('food')['version'] == 4
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue