Skip to content

fix(config): serialize every home-config mutation behind the state lock - #405

Merged
Sunrisepeak merged 1 commit into
release/0.4.70from
fix/state-lock-home-config
Jul 26, 2026
Merged

Sunrisepeak merged 1 commit into
release/0.4.70from
fix/state-lock-home-config

Conversation

@Sunrisepeak

Copy link
Copy Markdown
Member

Closes the first of the two gaps recorded during the PR #402 architecture review.

The problem

~/.xlings.json has several owners and each one rewrites the whole document:

key writer
versions, workspace install / remove / use
subos, activeSubos subos new / subos use -g / subos rm / self migrate
lang, mirror, index_repos xlings config, MCP add_repo/remove_repo
mirror, version self install

Only the first row took the home-wide state lock added in 0.4.70. So the lock protected install/remove/use from each other — and from nothing else.

xlings subos new big --storage image reads the config, spends seconds in mkfs.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 rm does the same across a remove_all of an entire subos tree.

That used to cost a stale entry. Since 0.4.70 it costs more: versions and workspace are 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:

site command verdict
config.cppm:save_versions install/remove/use already locked
subos.cppm:create subos new fixed — mkfs in the window
subos.cppm:use_global subos use -g fixed
subos.cppm:remove subos rm fixed — remove_all in the window
cli.cppm:cmd_config_ xlings config fixed
capabilities.cppm ×2 MCP add_repo/remove_repo fixed
xself/migrate.cppm self migrate fixed
xself/install.cppm ×2 + xself/init.cppm self install fixed — one lock for the whole stretch

The 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 mkfs or remove_all would 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, so subos new re-checks that the name is still free inside the callback, and remove_repo re-checks that the repo is still there.

self install is 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 makes xlings install fail in the next terminal. The xlings install -g it 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

  • 5 new unit cases (HomeConfig) — 421 total, all green
  • E2E-33 home_config_lock_test.sh — an unrelated flock holder must make subos new, subos use -g, subos rm and config --lang each fail without touching the file; then all of them succeed once released, with versions and workspace intact
  • Red-phase confirmed: reverting the lock acquisition in the helper fails HomeConfig.RefusesWhileAnUnrelatedProcessHoldsTheLock. The test gates the fix rather than describing it.
  • Regression: E2E subos_events / subos_workspace_c2_schema / subos_install_remove_isolation / subos_xpkg_use_cmd / legacy_config / xvm_group_switch all pass locally

Known side effect, documented

A subos new refused 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 across mkfs, which is the trade this PR explicitly declines.

`~/.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.
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.

1 participant