1 Commits
Author SHA1 Message Date
Kacper PaczosandGitHub b7bfdd2dd1 fix(config): share one ConfigManager across the process (#691)
* fix(config): share one ConfigManager across the process

main(), the tray, and the sound-effects check each constructed their own
ConfigManager. Every instance loads the whole config into memory at
construction time and save_config() writes that whole cache back, so any
save from a stale instance silently reverts what another instance saved
in the meantime — no race needed, the second save just wins with old
values. The disk-read workarounds in main (_should_append_trailing_space)
already existed to dodge exactly this.

Add get_shared_config_manager() and route the three call sites through
it. It keeps ConfigManager constructible for tests, and the tray/dialog
pair already shared one instance, so this extends that arrangement to
the rest of the process.

Tests: a regression test runs the two-instance scenario through the
accessor, and a source guard fails if application code constructs
ConfigManager directly again. conftest drops the shared instance
between tests, since it would otherwise be created under whichever
mocks the first caller had active and leak them into later tests.

The two single-instance tests now patch acquire_lock itself: they used
patch.dict(sys.modules, ...), which main()'s "from . import" ignores
once an earlier test has imported the real module — they only passed
through an unrelated mock leaking into the tray's shortcut setup, which
this change stops.

* fix(config): lock the shared accessor and prove the revert it prevents

Review follow-ups on #691:

- guard the lazy create with a lock, so two threads racing the first
  call cannot each build a ConfigManager and reintroduce #689
- take both handles before the first save in the accessor test, and
  assert identity there — the old order let a fresh ConfigManager pass
- add a characterization test: two direct ConfigManager instances still
  revert each other, which is the footgun the accessor exists to hide
- patch get_shared_config_manager in test_main and test_audio_feedback
  instead of the class, so a leftover singleton cannot skip the mock
- close the ExitStack in _boot_under_patches when main() raises; the
  caller only closes a stack it received, so a failure there leaked
  those patches into every later test

* fix(audio): route the tone lookup through the shared ConfigManager

The tone picker merged on main (#707) constructs its own ConfigManager,
which is exactly the stale-cache overwrite this branch removes; the new
source guard in test_shared_config_manager.py flags it.
2026-08-26 23:59:19 -07:00