fix(builder): the profile edit lock is an OS advisory lock, freed on exit #3020
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3020
Loadingβ¦
Reference in a new issue
No description provided.
Delete branch "PedroRossi/Longshot:fix/profile-edit-lock-holders"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The profile edit lock protects
profile.jsonandtrust.jsonwhile the builder (serving a playing game's channel),the launcher and
modcommands change them. It worked by writing a lock file holding the holder's pid and the time,and taking it again when it looked stale (a dead holder, or one that had held it too long). Judging a file stale and
moving it aside is racy β the four points from the review:
Rather than patch each race in the by-hand scheme, this replaces the mechanism with an operating-system advisory
lock on the lock file (
std::fs::File::try_lock), exactly as build-core'sWorkLock,GameLockandProfileLockalready do: one holder at a time, released when the guard is dropped or the process ends however it ends. There is no
stale detection left to race, a crashed edit leaves no file that keeps the next one out, and nothing reads or writes
the lock's contents.
edit_lockwaits up toWAITfor a held lock, thenBusy. About 100 lines of pid/time,staleness and move-aside code are gone; the public API (
edit_lock,EditGuard::take,LockError,EDIT_LOCK,WAIT) is unchanged.Read first:
EditGuard::takeinbuilder/crates/launcher-profile/src/edit.rs.Testing
Tested: on this PC (Debian 13), game-free. The four races were timing-dependent in the old scheme and aren't
unit-testable as deterministic races; removing the scheme removes them. Three tests lock in the new behavior:
one_edit_at_a_time_and_the_lock_frees_when_dropped(mutual exclusion and release β fails onmain, whichdeleted the lock file on release, part of what enabled the create-again race),
a_waiting_taker_gets_it_the_moment_the_holder_lets_go(the wait, across threads, on the real OS lock), and
a_lock_file_left_by_a_dead_holder_does_not_keep_the_next_one_out.cargo test -p launcher-profilepasses;cargo clippy -p launcher-profile --all-targets -D warningsand rustfmt areclean;
cargo check -p build-drivercompiles against the unchanged API.mise run teston both lock branches merged on current main: 832 passed, 56 failed, 13 skipped, the same as main β the 56 are pre-existing (45 need a tool this PC lacks on PATH under--strict: clang, spirv-val, glslang; the rest are GPU/compile fixtures), none in launcher-profile or longshot-builder. This change is tooling (the builderand launcher's lock) and does not run in the game, so it was not run in-game.
Still to test:
modcommand or thelauncher edits it), expecting one to wait and neither to lose a write.
std::fs::File::try_lockusesLockFileExthere; the Linux
flockpath is covered above.Measurements
Nothing to measure: a lock held for the moment of an edit; the new path makes fewer system calls than the old pid/time/stale scheme, and nothing a build or the game runs changes.
Risks
The lock file is now left in place when released (as build-core's locks leave theirs), where the old scheme deleted it
on drop; this is what lets the OS own the lock and is the point of the change. The public API is unchanged, so
build-driver's callers are untouched. A revert restores the old scheme.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Found in an AI-assisted audit of the repository. The change, the tests and this description were made with Claude Code;
I reviewed them.
Co-Authored-By: Claude Opus 5.5 [email protected]
π€ Generated with Claude Code
Tested in game: Debian 13, GeForce GTX 1660 SUPER, NVIDIA 550.163.01: the game built by a builder from main @
1938a302with this change merged (and my two other builder pull requests from the same audit), through the PC's job queue, played from the Market entrance repository save, then the control bridge's quit; exit 0. The profile code this changes runs in the launcher's play and build, not in the game.WIP: fix(builder): the profile edit lock takes a stale lock only when it is one, and puts back one moved in errorto fix(builder): the profile edit lock takes a stale lock only when it is one, and puts back one moved in error@review
Recommendation: Request changes. The recovery path can still give two editors the lock and lose profile changes. A partially written holder record can also be mistaken for an old lock.
The change adds a wait for young unreadable locks and failed stale-lock moves, but its move-and-restore protocol does not preserve mutual exclusion during recovery.
Blocking (4): must change before merging.
builder/crates/launcher-profile/src/edit.rs:78: Moving a fresh lock away before checking it leaves the lock path available to another editor, so putting it back cannot ensure mutual exclusion. (more on the line) Do: Use a cross-process locking or stale-recovery protocol that never makes a live holder's lock path available to another taker, and add a controlled concurrent test covering this interleaving.builder/crates/launcher-profile/src/edit.rs:81: Restoring a moved lock by copying its current contents loses a holder's subsequent write to the original file. (more on the line) Do: Keep the original lock object and its ownership intact throughout recovery, including while its creator is writing, and test this interleaving.builder/crates/launcher-profile/src/edit.rs:56: A partially written holder record can already parse as a PID and timestamp, bypassing the new age protection. (more on the line) Do: Publish a complete holder record atomically or use a representation that cannot be accepted until the write is complete; add a test for a parseable partial record.builder/crates/launcher-profile/src/edit.rs:71: An unknown or future modification time is treated as the oldest possible lock, including when the lock was just created. (more on the line) Do: Treat an unavailable or future timestamp conservatively as young, or use a recovery mechanism that does not rely on wall-clock file age.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/launcher-profile/src/edit.rs:140: The Windows held-open-file recovery path still needs the run listed in the pull request. (more on the line) Do: Run the stated Windows case and record the result after the recovery protocol is corrected.@fixcan do L1, L2, L3 and L4.Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven overall. The author reports that the new game-free cases fail on main and pass here, but they do not exercise the concurrent move-and-restore interleavings or a partially written valid record.
Other systems: The lock protects builder channel edits and is exposed to launcher and mod-tool editors. Both Windows and Linux can encounter the recovery races; the reported Linux game run does not exercise them. The Windows held-open-file case remains untested.
Switches: None.
Reviewed:
9b9def088aagainst1938a302fe: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the pull request template, both commits, the complete diff and edit.rs, and the builder channel caller. Skimmed the other lock references and the reported runs. docs/review-checklist.md and the adjacent Tooling checkout were absent.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run during review. The author reports game-free tests and a Linux game run; CI is still running, and the Windows held-open-file case is outstanding.
Overlaps and merge order: No overlapping pull requests identified from the supplied information.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -53,2 +53,4 @@/// it holds it.fn stale(text: &str, age: Duration, now: SystemTime, alive: &dyn Fn(u32) -> bool) -> bool {let mut it = text.split_whitespace();let (Some(pid), Some(at)) =L3 (blocking): A partially written holder record can already parse as a PID and timestamp, bypassing the new age protection.
When: If a reader sees a prefix such as
1234 1while the creator is writing its current timestamp, both fields parse and the timestamp appears decades old. The reader removes the young live lock as stale and both editors can write the profile.Do: Publish a complete holder record atomically or use a representation that cannot be accepted until the write is complete; add a test for a parseable partial record.
@ -63,0 +68,4 @@.and_then(|m| m.modified()).ok().and_then(|at| SystemTime::now().duration_since(at).ok()).unwrap_or(Duration::MAX)L4 (blocking): An unknown or future modification time is treated as the oldest possible lock, including when the lock was just created.
When: If the system clock moves backward after a creator makes an empty lock, its modification time is in the future. A competing taker gets Duration::MAX, immediately moves the live lock away and can edit at the same time.
Do: Treat an unavailable or future timestamp conservatively as young, or use a recovery mechanism that does not rely on wall-clock file age.
@ -63,0 +75,4 @@/// is looked at first: a fresh lock another taker made since the read goes back in its place, never removed.fn move_stale(path: &Path, held: &str) -> bool {let aside = path.with_extension(format!("stale-{}", std::process::id()));if std::fs::rename(path, &aside).is_err() {L1 (blocking): Moving a fresh lock away before checking it leaves the lock path available to another editor, so putting it back cannot ensure mutual exclusion.
When: Two takers judge a dead lock stale. After the first replaces it and starts editing, the second renames that fresh lock aside; a third taker creates the now-missing path and starts editing too. The restore then fails with AlreadyExists and removes the first holder's lock from the aside path.
Do: Use a cross-process locking or stale-recovery protocol that never makes a live holder's lock path available to another taker, and add a controlled concurrent test covering this interleaving.
@ -63,0 +78,4 @@if std::fs::rename(path, &aside).is_err() {return false;}let moved = std::fs::read_to_string(&aside).unwrap_or_default();L2 (blocking): Restoring a moved lock by copying its current contents loses a holder's subsequent write to the original file.
When: A stale-lock taker renames a fresh lock after its owner creates the file but before it writes its text. It reads and restores an empty file, then removes the aside; the owner writes to the moved file and returns a guard, while the visible lock remains empty and can be reclaimed after two seconds during that edit.
Do: Keep the original lock object and its ownership intact throughout recovery, including while its creator is writing, and test this interleaving.
@ -111,2 +139,2 @@if std::fs::rename(&path, &aside).is_ok() {let _ = std::fs::remove_file(&aside);Ok(held) if stale(&held, age(&path), SystemTime::now(), alive) => {if move_stale(&path, &held) {L5 (should fix): The Windows held-open-file recovery path still needs the run listed in the pull request.
When: A Windows program holds a stale lock without delete sharing; the launcher must stop retrying at its deadline and report the profile busy.
Do: Run the stated Windows case and record the result after the recovery protocol is corrected.
@fix
The lock recovery protocol (L1βL4): make lock publication and stale recovery mutually safe for concurrent takers, and treat an unreadable or future modification time as young rather than ancient.
9b9def088aff9c6d922afix(builder): the profile edit lock takes a stale lock only when it is one, and puts back one moved in errorto fix(builder): the profile edit lock is an OS advisory lock, freed on exitReworked after the review. Rather than patch the four races in the stale-detection scheme, I replaced the mechanism with an OS advisory lock (std::fs::File::try_lock, as build-core's WorkLock/GameLock/ProfileLock already use): one holder at a time, released by the OS when the guard drops or the process ends, so there is no stale detection left to race and a crashed edit leaves no file that blocks the next one. ~100 lines of pid/time/stale/move-aside code gone; the public API is unchanged and build-driver compiles against it. New title and description above.
@review
Recommendation: Approve. The advisory lock removes the four stale-recovery races, and the reported Linux tests exercise exclusion and release. Windows concurrent editing and the pending CI checks still need results before merge. Nothing blocking is left. Recommend approval; not approved on the forge.
Profile edits use an OS-managed lock that remains on disk and is released when its holder exits. This removes the stale-file recovery code and its races, at the cost of changing the lock-file lifecycle.
Since the last review (on
9b9def088a)Resolved: L1 (Moving a fresh lock away before checking it leaves the lock path available to another editβ¦); L2 (Restoring a moved lock by copying its current contents loses a holder's subsequent write tβ¦); L3 (A partially written holder record can already parse as a PID and timestamp, bypassing theβ¦); L4 (An unknown or future modification time is treated as the oldest possible lock, including wβ¦); L5 (The Windows held-open-file recovery path still needs the run listed in the pull request.)
Checks: Still running: 10 checks.
Also noticed (4; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Yes on Linux: the reported launcher-profile tests cover exclusion, waiting and release, and the new code removes the stale-recovery paths. Windows behavior remains untested.
Other systems: The builder, mod commands and launcher share this profile lock on Linux and Windows; the game rendering path is unaffected. Windows concurrent editing has not been run. Existing build-driver tests still expect the lock file to be deleted.
Switches: None.
Reviewed:
ff9c6d922aagainstb77c2f4b3e: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the pull request template, the full changed edit.rs, the diff and commit, and build-core's lock implementation. Read relevant build-driver callers and tests and the build reference; other callers were located and skimmed. Tooling's AGENTS.md was unavailable; no review checklist was found.; nothing run (a code review).Checked and fine:
Testing: No commands were run beyond read-only inspection. The author reports launcher-profile tests, clippy and formatting passing on Debian; the Windows run is outstanding and CI is still running.
Overlaps and merge order: None found in the supplied information.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.