Config write safety
config.json is one file mutated from several surfaces and processes. Without a lock over the whole read-modify-write, the later writer discards the earlier one.
from openprogram.state.config import update_config
def mutate(cfg):
cfg["ui"]["web_port"] = 8101
result = update_config(mutate) # returns the resulting config
update_config(mutate, expected_revision=revision) # with a revision check
openprogram.state.config.update_config is the only correct way to change part of the config. Writes back with mode 0o600. The caller never holds a config dict across the lock boundary. read_config and write_config stay for read-only access and full replacement; a bare pair of them races.
| Lock | Covers |
|---|---|
_config_write_lock (threading.Lock) | Worker threads: a TUI tool toggle and a web key save |
config.json.lock, via _private_atomic_update | Other processes: openprogram config and openprogram setup against the worker |
Neither is redundant. A private module lock cannot substitute: storage.py's _cache_lock protects only that module's cache.
Scope is write atomicity alone. Schema and validation belong to state.settings (redesign); the format stays JSON.
Implementation status
| Writer | Path |
|---|---|
settings.set_setting | TUI /config, web Model services → Behavior and General → Web server, openprogram config; _set_at and tools.disabled branches |
routes/settings/config.py | Web API-key merge and delete |
state/preferences.py | set_ui_ports, write_search_default_provider |
interfaces/cli/setup_sections/sections.py | openprogram setup wizard |
models/providers/settings/storage.py | Providers section |
models/providers/catalog/last_used.py, host/machines, models/providers/recording | Last model, machines, recordings |
All config writers route through update_config. A unit test asserts two concurrent mutators serialise and the result reflects both.