Skip to content

fix(dashboard): write the status and config files atomically, one writer at a time - #4

Merged
Yoepan merged 1 commit into
Yoepan:mainfrom
KiloMusician:fix/dashboard-atomic-state-files
Oct 25, 2026
Merged

Yoepan merged 1 commit into
Yoepan:mainfrom
KiloMusician:fix/dashboard-atomic-state-files

Conversation

@KiloMusician

Copy link
Copy Markdown
Contributor

First of the pieces you asked for when splitting #1 (this one: atomic state-file writes and their tests).

Problem

The dashboard server threads, the provider-probe worker, the swarm pipeline thread and council.py all read-modify-write specs/swarm-status.json and specs/swarm-config.json. They shared one <name>.tmp scratch file and no lock, so on main (51c772e):

  • two concurrent writers tear the JSON (the next read fails with "Extra data") and lose each other's updates; 8 threads updating the status file produced invalid JSON in 5 of 5 runs here;
  • on Windows os.replace onto a file a reader still has open raises PermissionError, which was swallowed, so the update was silently dropped;
  • the provider probe worker wrote back the whole config it had read before probing, reverting anything saved in the meantime;
  • GET /api/config probed and then wrote back the copy it had already stripped of provider keys. On Windows this happens not to delete the keys, only because that os.replace runs while the handler still holds the file open and fails (and the failure is swallowed, leaving a stray swarm-config.json.tmp); if the replace ever succeeded, a status change during one page load would delete every stored key. I treat it as a latent bug and the test pins it.

Change

  • New dashboard/statefile.py: atomic_write_json writes a unique temp file next to the target and replaces it (short retry while a Windows reader holds the target, existing file mode and symlinks preserved); update_json_file does read-modify-write under a per-file lock, so the status and config files never wait for each other.
  • serve.py, swarm_pipeline.py and council.py go through it; the duplicate safe_write_json is removed. stream.py (a separate process) uses the atomic replace so readers never see a partial file.
  • The probe worker merges only the status fields into a fresh read of the file. GET /api/config refreshes statuses through the same merge and serves what is on disk. POST /api/config writes atomically and reports a failed write instead of claiming success.
  • Default launch and UI are unchanged.

Tests: tests/test_statefile.py (helpers) and tests/test_dashboard_state.py (the real serve.py against a temporary tree; skipped off Windows, where serve.py exits at import). Standard library only. Most of the new tests fail on main (for example torn JSON under concurrent writers, lost updates and a probe result reverting a concurrent save).

Verification

python -I -m unittest discover -s tests on head 2c50ec6: Windows 11 / Python 3.13: 32 tests, OK (2 skipped: POSIX-only permission and symlink tests). Linux (Ubuntu, Python 3.12): 23 tests, OK, 2 skipped (the server tests).

Not verified

GitHub CI was not run on this head; Python 3.11 (the version ci.yml pins) was checked for syntax only. Another process that writes the same files (stream.py, an editor) is not covered by the in-process lock; the atomic replace still protects readers from partial files.

Prepared with AI coding assistants (OpenAI GPT and Anthropic Claude) on behalf of @KiloMusician. Commands and counts above were run on the head named. MIT licence untouched.

…ter at a time

The dashboard server threads, the provider probe worker, the swarm pipeline
thread and council.py all read-modify-write specs/swarm-status.json and
specs/swarm-config.json.  They shared one "<name>.tmp" scratch file with no
lock.  What goes wrong (each is reproduced by a test in this change):

* two concurrent writers tear the JSON ("Extra data" on the next read) and
  lose each other's updates;
* on Windows os.replace onto a file a reader still has open raises
  PermissionError, which was swallowed, so the update was dropped silently;
* the provider probe worker wrote back the whole config it had read before
  probing (sockets, and HTTP for rate-limited cloud providers), reverting
  anything saved in the meantime;
* a failed replace left a stray swarm-config.json.tmp behind.

* dashboard/statefile.py: atomic_write_json writes a unique temp file in the
  same directory and replaces the target, retrying briefly while a Windows
  reader holds it; an existing file keeps its permissions and a symlink is
  followed rather than replaced.  update_json_file does the read-modify-write
  under a lock that belongs to that one file, so a writer waiting for a reader
  of the status file never delays writers of the config (no code path holds
  two of these locks).  A UTF-8 byte-order mark is accepted when reading.
* serve.py, swarm_pipeline.py and council.py go through it (stream.py, a
  separate process, uses the atomic replace so readers never see a partial
  file).  The duplicate safe_write_json definition is removed.
* The probe worker now merges only the status fields into a fresh read of
  the file.
* POST /api/config writes atomically and reports a failed write instead of
  claiming success.
* Latent, not reproducible on Windows today: GET /api/config probed local
  providers and wrote back the copy it had just stripped of provider keys.
  The old code did that from inside "with open(cfg_file)", where the replace
  is refused on Windows and the error was swallowed, so the keys survived by
  accident; wherever the replace succeeds every stored key would be deleted.
  It now refreshes the statuses through the same merge and serves what is on
  disk.

Tests: tests/test_statefile.py (helpers) and tests/test_dashboard_state.py
(the real serve.py against a temporary tree; skipped off Windows, where
serve.py cannot be imported).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@Yoepan

Yoepan commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Thank you @KiloMusician for the clean implementation of statefile.py and for resolving the provider probing edge case where keys could be stripped on config save. The Windows-aware atomic write retry mechanism is very well thought out. Merging!

@Yoepan
Yoepan merged commit b21e357 into Yoepan:main Oct 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants