Repository navigation
fix(config): serialize every home-config mutation behind the state lock - #405
Merged
Merged
Conversation
`~/.xlings.json` has several owners and each one rewrites the whole document. install/remove/use write `versions` and `workspace`; the subos commands write `subos` and `activeSubos`; `xlings config` writes `lang`, `mirror` and `index_repos`; the MCP repo capabilities write `index_repos`; `self install` writes `mirror` and `version`. Only the first group took the home-wide state lock, so the lock protected install/remove/use from each other and from nothing else. `xlings subos new big --storage image` read the config, spent seconds in mkfs.ext4, then wrote back the document it had read -- reverting anything an install committed in between. `subos rm` did the same across a `remove_all` of the whole subos tree. The payload stayed on disk with no record of it, and since 0.4.70 `versions` and `workspace` are two halves of one release: reverting one of them alone is enough to make a whole toolchain refuse to switch. The audit found nine read-modify-writes of the file. Six are now committed through `update_home_config`, which takes the lock, re-reads the document inside it and writes only the caller's delta: subos new / subos use --global / subos rm, xlings config, MCP add_repo / remove_repo, self migrate `self install` is the seventh: its three writes are interleaved with replacing the binary and laying down the home, so it takes one lock across that whole stretch instead. Deliberately not at the top of the function -- the confirmation prompts above it would hold the lock on human time. The `xlings install -g` it spawns for the patchelf step inherits the re-entrancy marker and works under the same lock. `xself/init.cppm`'s config-defaults write is only reachable from there and is covered by it. The lock covers the commit, not the whole command: holding it across mkfs or remove_all would make a routine install in the next terminal wait past its 30s timeout and fail. The cost is that anything a caller decided from a lock-free read may be stale by the time it commits, so each call site re-checks inside the callback -- `subos new` re-checks that the name is still free, `remove_repo` re-checks that the repo is still there. Not covered, and deliberately: the project state file and the per-subos `.xlings.json`. Those are scoped to a project tree or a subos rather than to the home, and the state lock is per-home; giving it to them would serialize unrelated projects against each other. Verified: 5 unit cases (HomeConfig) + E2E-33. Reverting the lock acquisition in the helper fails HomeConfig.RefusesWhileAnUnrelatedProcessHoldsTheLock, so the test gates the fix rather than describing it.
This was referenced Jul 26, 2026
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.
Closes the first of the two gaps recorded during the PR #402 architecture review.
The problem
~/.xlings.jsonhas several owners and each one rewrites the whole document:versions,workspaceinstall/remove/usesubos,activeSubossubos new/subos use -g/subos rm/self migratelang,mirror,index_reposxlings config, MCPadd_repo/remove_repomirror,versionself installOnly the first row took the home-wide state lock added in 0.4.70. So the lock protected
install/remove/usefrom each other — and from nothing else.xlings subos new big --storage imagereads the config, spends seconds inmkfs.ext4, then writes back the document it read at the start. An install that committed in between is silently reverted: the payload stays on disk with no record of it.subos rmdoes the same across aremove_allof an entire subos tree.That used to cost a stale entry. Since 0.4.70 it costs more:
versionsandworkspaceare two halves of one release, so reverting one of them alone leaves a group manifest naming a member the database no longer has — and the whole toolchain refuses to switch.The audit
Nine read-modify-writes of
<home>/.xlings.json:config.cppm:save_versionssubos.cppm:createsubos newsubos.cppm:use_globalsubos use -gsubos.cppm:removesubos rmremove_allin the windowcli.cppm:cmd_config_xlings configcapabilities.cppm×2add_repo/remove_repoxself/migrate.cppmself migratexself/install.cppm×2 +xself/init.cppmself installThe design
New
src/core/home_config.cppm.update_home_config(home, mutate)takes the lock, re-reads the document inside it, applies the caller's delta and writes.The lock covers the commit, not the command. Holding it across
mkfsorremove_allwould make a routine install in the next terminal wait past its 30s timeout and fail. The cost of that choice is stated in the API doc and honored at every call site: anything decided from a lock-free read may be stale, sosubos newre-checks that the name is still free inside the callback, andremove_repore-checks that the repo is still there.self installis different — its three writes are interleaved with replacing the binary and laying down the home, so it takes one lock across that whole stretch. Placed after the confirmation prompts on purpose: a lock held on human time is a lock that makesxlings installfail in the next terminal. Thexlings install -git spawns for the patchelf step inherits the re-entrancy marker and works under the same lock rather than deadlocking against it.Deliberately not covered
The project state file and the per-subos
.xlings.json. Both are scoped to one project tree or one subos, while the state lock is per-home; giving them the home lock would serialize unrelated projects against each other. They keep last-writer-wins.Verification
HomeConfig) — 421 total, all greenhome_config_lock_test.sh— an unrelatedflockholder must makesubos new,subos use -g,subos rmandconfig --langeach fail without touching the file; then all of them succeed once released, withversionsandworkspaceintactHomeConfig.RefusesWhileAnUnrelatedProcessHoldsTheLock. The test gates the fix rather than describing it.Known side effect, documented
A
subos newrefused by the lock has already created its directories. They are inert until the entry is registered and the retry reuses them; the error hint says so, and E2E-33 scenario 5 checks the retry. Avoiding it would mean holding the lock acrossmkfs, which is the trade this PR explicitly declines.