Repository navigation
fix(dashboard): write the status and config files atomically, one writer at a time - #4
Merged
Yoepan merged 1 commit intoOct 25, 2026
Conversation
…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]>
Owner
|
Thank you @KiloMusician for the clean implementation of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pyall read-modify-writespecs/swarm-status.jsonandspecs/swarm-config.json. They shared one<name>.tmpscratch file and no lock, so onmain(51c772e):os.replaceonto a file a reader still has open raisesPermissionError, which was swallowed, so the update was silently dropped;GET /api/configprobed and then wrote back the copy it had already stripped of provider keys. On Windows this happens not to delete the keys, only because thatos.replaceruns while the handler still holds the file open and fails (and the failure is swallowed, leaving a strayswarm-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
dashboard/statefile.py:atomic_write_jsonwrites 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_filedoes read-modify-write under a per-file lock, so the status and config files never wait for each other.serve.py,swarm_pipeline.pyandcouncil.pygo through it; the duplicatesafe_write_jsonis removed.stream.py(a separate process) uses the atomic replace so readers never see a partial file.GET /api/configrefreshes statuses through the same merge and serves what is on disk.POST /api/configwrites atomically and reports a failed write instead of claiming success.Tests:
tests/test_statefile.py(helpers) andtests/test_dashboard_state.py(the realserve.pyagainst a temporary tree; skipped off Windows, whereserve.pyexits at import). Standard library only. Most of the new tests fail onmain(for example torn JSON under concurrent writers, lost updates and a probe result reverting a concurrent save).Verification
python -I -m unittest discover -s testson head2c50ec6: 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.ymlpins) 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.