Skip to content

fix(settings): take the settings file lock before the first-write read - #124471

Merged
obviyus merged 1 commit into
openclaw:mainfrom
MrSwagatRathod:fix/settings-first-write-lock
Aug 30, 2026
Merged

obviyus merged 1 commit into
openclaw:mainfrom
MrSwagatRathod:fix/settings-first-write-lock

Conversation

@MrSwagatRathod

@MrSwagatRathod MrSwagatRathod commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Two OpenClaw processes can write distinct settings to the same absent settings.json. Both calls report success, but the second write can silently discard the first setting.

Fixes #124394.

Root cause

FileSettingsStorage.withLock read a missing file before it acquired the file lock. Both processes could therefore merge their changes from the same empty object.

Fix

  • Keep missing-file loads lazy through readSettingsScope.
  • Acquire the canonical settings-file lock before every read-modify-write callback.
  • Preserve the shipped SettingsStorage.withLock path for custom storage implementations.

Evidence

The proof started two real Node processes against one absent settings file. One process wrote defaultModel; the other wrote theme.

Revision Process results Final file
Main f46f9b5aa39f Both exited successfully {"theme":"dark"}; defaultModel was lost
This head 22f9230dd6f Both exited successfully {"defaultModel":"provider/model","theme":"dark"}

Current main b4f85100f72d has no later changes to the settings owner files.

Focused validation: node scripts/run-vitest.mjs src/agents/sessions/settings-storage.test.ts src/agents/sessions/settings-manager.test.ts passes 7 tests.

@clawsweeper

clawsweeper Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Aug 16, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 16, 2026
@clawsweeper

clawsweeper Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge. Reviewed August 30, 2026, 9:49 AM ET / 13:49 UTC.

ClawSweeper review

What this changes

The PR serializes file-backed first writes before reading settings and adds a two-process regression test for preserving concurrent settings fields.

Regression provenance

Possible regression — probable (reviewed change; known regression link). No predecessor PR is attributed.

Merge readiness

⚠️ Needs maintainer review before merge - 4 items remain

The lock-first repair addresses the reported first-write race, but it regresses the exported FileSettingsStorage contract: a no-op direct withLock call now creates the settings directory. Keep this PR open for a compatibility-preserving update-path design.

Priority: P1
Reviewed head: 22f9230dd6f6814842b5d8cd10181e2620975462
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The race proof and focused regression test are credible, but the patch needs an SDK-compatible update-path design before merge.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.
Evidence reviewed 7 items Introduced compatibility regression: The PR unconditionally creates the parent directory before invoking every FileSettingsStorage.withLock callback, including callbacks that return undefined.
Previous no-write behavior: Before this PR, FileSettingsStorage created the directory only after the callback returned a value to write, so a direct read-only callback left an absent settings directory absent.
SDK exposure: FileSettingsStorage and SettingsStorage are re-exported through the extension-safe agent-sessions SDK barrel, so direct callers are a supported compatibility surface.
Findings 1 actionable finding [P1] Preserve lazy no-write storage calls
Security None None.

How this fits together

SettingsManager loads and persists agent preferences through SettingsStorage. FileSettingsStorage coordinates global and project settings files, which supply preferences to future agent sessions.

flowchart LR
A[Preference change] --> B[Settings manager]
B --> C[Settings storage]
C --> D{Load or save}
D --> E[Read settings]
D --> F[Lock, read, merge]
F --> G[Settings file]
E --> H[Agent session]
G --> H
Loading

Decision needed

Question Recommendation
Should direct FileSettingsStorage.withLock callers retain the shipped no-write, no-directory side effect behavior? Preserve lazy no-write behavior: Keep existing direct withLock reads side-effect free and add a separate lock-before-read update path for SettingsManager persistence.

Why: This class and its storage interface are exposed through the extension-safe SDK; the repair needs a maintainer choice between preserving that contract and intentionally changing it.

Before merge

  • Preserve lazy no-write storage calls (P1) - withLock is exported through the extension-safe SDK, and before this PR a callback returning undefined did not create the parent directory. The unconditional mkdirSync changes that contract for direct callers; keep the old behavior and use a distinct lock-before-read update path for manager persistence.
  • Resolve merge risk (P1) - Merging changes the extension-SDK-exposed FileSettingsStorage.withLock behavior: direct read-only callers now create a settings directory and lock file path even when they make no write.
  • Resolve merge risk (P1) - The bug fix adds 11 net production lines; that growth is justified only if the separate lazy-read/update contract is kept compatibility-safe.

Findings

  • [P1] Preserve lazy no-write storage calls — src/agents/sessions/settings-storage.ts:158-160
Agent review details

Security

None.

PR surface

Source +11, Tests +111. Total +122 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 30 19 +11
Tests 1 111 0 +111
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 141 19 +122

Review metrics

Metric Value Why it matters
Production versus test LOC production +30/-19 (net +11), tests +111/-0 The production growth adds a storage read/update split; the new test is dedicated process-level race coverage.

Merge-risk options

Maintainer options:

  1. Preserve direct caller semantics (recommended)
    Split the write update path from read-only locking so an absent directory remains absent when a direct withLock callback returns undefined.
  2. Accept and document the API change
    Merge only after an owner explicitly accepts directory creation as the new behavior for direct SDK callers.

Technical review

Best possible solution:

Keep direct no-write withLock calls lazy, and route SettingsManager persistence through a dedicated lock-before-read update path while retaining the race regression coverage.

Do we have a high-confidence way to reproduce the issue?

Yes: current main reads a missing file before taking the lock, while SettingsManager merges fields inside that callback; the added two-process test encodes the same storage race.

Is this the best way to solve the issue?

No: locking before the read is the right owner boundary, but the current implementation also changes the supported direct no-write storage behavior.

Full review comments:

  • [P1] Preserve lazy no-write storage calls — src/agents/sessions/settings-storage.ts:158-160
    withLock is exported through the extension-safe SDK, and before this PR a callback returning undefined did not create the parent directory. The unconditional mkdirSync changes that contract for direct callers; keep the old behavior and use a distinct lock-before-read update path for manager persistence.
    Confidence: 0.96

Overall correctness: patch is incorrect
Overall confidence: 0.94

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against ab989f0d3c79.

Labels

Label changes:

  • add P1: The introduced behavior can alter existing extension callers’ filesystem state even when they perform no settings write.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (terminal): The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.
  • remove P2: Current review triage priority is P1, so this older priority label is no longer current.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P1: The introduced behavior can alter existing extension callers’ filesystem state even when they perform no settings write.
  • merge-risk: 🚨 compatibility: The PR changes an exported storage interface and the observable behavior of direct FileSettingsStorage callers.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🐚 platinum hermit and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Sufficient (terminal): The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body records a current-head two-process Node run against one absent settings file with both fields preserved, matching the changed FileSettingsStorage lock/read owner; the added test independently exercises the same separate-process file path, though it does not cover the direct no-write compatibility regression.

Evidence

What I checked:

Likely related people:

  • Sarah Fortune: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Preserve direct no-op withLock behavior while keeping lock-before-read semantics for manager writes.
  • Add focused coverage that a direct no-op FileSettingsStorage.withLock call leaves an absent directory absent.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (4 earlier review cycles)
  • reviewed 2026-08-18T05:51:14.013Z sha ec3d2dd77acfa7f7feed1af927655c2686c6fccf :: needs real behavior proof before merge. :: [P1] Port the update to the current file-storage owner | [P2] Exercise the manager's process-level write path
  • reviewed 2026-08-21T23:01:30.006Z sha ec3d2dd77acfa7f7feed1af927655c2686c6fccf :: needs real behavior proof before merge. :: [P1] Port the lock-first update to the current storage owner | [P2] Exercise the manager’s concurrent first-write path
  • reviewed 2026-08-23T03:27:00.563Z sha ec3d2dd77acfa7f7feed1af927655c2686c6fccf :: needs real behavior proof before merge. :: [P1] Keep the lock fix in the current storage owner | [P2] Exercise the manager’s concurrent first-write path
  • reviewed 2026-08-29T03:34:56.701Z sha ec3d2dd77acfa7f7feed1af927655c2686c6fccf :: needs real behavior proof before merge. :: [P1] Port the repair to the current file-storage owner

Two processes could read an absent settings file before either acquired its
lock. Each process then merged from an empty object, and the later write
silently discarded the other process's setting.

Keep missing-file reads lazy, but acquire the file lock before every
read-modify-write callback. Existing custom settings stores keep using their
shipped lock-based read contract.
@obviyus
obviyus force-pushed the fix/settings-first-write-lock branch from ec3d2dd to 22f9230 Compare August 30, 2026 13:38
@openclaw-barnacle openclaw-barnacle Bot added triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. and removed triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Aug 30, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 30, 2026
@obviyus
obviyus merged commit 0b2f688 into openclaw:main Aug 30, 2026
281 of 283 checks passed
@obviyus

obviyus commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Thanks @MrSwagatRathod!

Proof: Two real Node processes wrote defaultModel and theme to one absent settings file. The old code lost defaultModel; the merged head retained both fields, and both processes exited successfully.

  • Bug: Concurrent first writes could both merge from an empty file and silently discard one setting.
  • Fix: File-backed updates now acquire the canonical lock before reading, while missing-file loads stay lazy.

Example

Before After
Final file kept only theme Final file kept both defaultModel and theme

obviyus added a commit that referenced this pull request Aug 30, 2026
Remove the unshipped optional settings-read branch and synthetic process test added by #124471.

Keep missing-directory reads side-effect free, lock before reads when the directory exists, and document unsupported concurrent external first creation.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 31, 2026
Prevent concurrent OpenClaw processes from silently discarding distinct settings when they create the same settings file.

Keep missing-file loads lazy, and acquire the canonical file lock before each read-modify-write operation.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 31, 2026
Remove the unshipped optional settings-read branch and synthetic process test added by openclaw#124471.

Keep missing-directory reads side-effect free, lock before reads when the directory exists, and document unsupported concurrent external first creation.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: concurrent first write to a not-yet-created settings.json silently loses one process's setting (file lock taken after the read)

2 participants