Skip to content

fix(storage): Merge settings writes across instances - #25715

Merged
ramezgerges merged 1 commit into
unoplatform:masterfrom
ramezgerges:dev/rara/localsettings-multi-instance
Oct 27, 2026
Merged

ramezgerges merged 1 commit into
unoplatform:masterfrom
ramezgerges:dev/rara/localsettings-multi-instance

Conversation

@ramezgerges

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/uno-private#2472

PR Type:

🐞 Bugfix

What changed? 🚀

The bug: on Skia desktop (Windows, macOS, Linux), ApplicationData.Current.LocalSettings and RoamingSettings were read from disk once per process, and every write saved that whole in-memory copy back. When two instances of the same app ran at once, the last writer won:

  • keys the other instance added were lost;
  • keys the other instance removed came back.

Two related problems in the same code:

  • A failed read led to an erase. If the read failed (a locked or damaged file), the process carried on with no values, and its next write wiped the file.
  • Writes weren't atomic. File.OpenWrite overwrote the file in place, so a crash part-way through left a broken file.

The fix: the store moves into a new internal SettingsFile class (src/Uno.WinRT/Storage/Internal/SettingsFile.skia.cs):

  • Writes: each change takes an exclusive lock on a sibling .lock file, re-reads the file, applies only that key change, writes a temporary file and atomically replaces the settings file. The lock is flock on Unix and file sharing modes on Windows, both through FileShare.
  • Reads: a read re-checks the file's timestamp and size, and reloads when another instance changed them. Readers take a shared lock, because Windows can't replace a file another process has open.
  • Coarse timestamps: some file systems record modification times only to 1–2 s (FAT, HFS+), so two writes close together can leave the same stamp. A file written within the last 2 s is therefore always re-read.
  • Failures:
    • A change that can't be saved (the lock timed out, or the file is unreadable) stays pending: it remains visible and is saved with the next write.
    • A short read is retried first. A damaged file is then moved aside to .corrupt and rebuilt from the last good copy.
    • Only "file not found" counts as an empty store.
  • No-op changes: a change that makes no difference doesn't rewrite the file.

Two smaller bugs in FilePropertySet are fixed as well:

  • enumerating the container returned the serialized strings instead of the values;
  • setting a key to null wrote the file twice.

The on-disk format is unchanged, so files written by earlier versions are read as before.

Limits (documented on the class): the locks are advisory. They do nothing if an app turns off .NET file locking (System.IO.DisableFileLocking), or on a network file system that ignores flock.

Validation

  • Unit tests: Given_SettingsFile, 16 tests. Two SettingsFile instances on one file stand in for two processes. All 39 Windows_Storage unit tests pass.
  • Fail-before: with the old read-once, write-everything behaviour swapped back in, 6 of the original 10 cross-instance and damaged-file tests fail.
  • Multi-process stress: run outside the repo. Three processes writing 300 keys each, plus two processes reading continuously, kept all 900 keys on Windows and on Linux (Docker). With .NET file locking turned off, Linux kept only 306 of 900, which shows the lock is what keeps the writes.
  • Not run: two real app instances on macOS.

PR Checklist ✅

🤖 Generated with Claude Code

On Skia desktop, ApplicationData settings were read once per process and
every write saved that whole copy back. Two instances of the same app
therefore undid each other: keys the other one added were lost and keys it
removed came back. A failed read left an empty copy whose next write
erased the file, and writes went over the file in place.

The settings file is now shared safely:
- each change takes an exclusive lock (flock on Unix, sharing modes on
  Windows), re-reads the file, applies just that key and replaces the file
  atomically; readers take a shared lock, since Windows can't replace a
  file another process has open;
- reads pick up changes other instances made, re-reading a file written
  within the last 2 s because coarse file systems can repeat its stamp;
- a change that can't be saved (lock timeout, unreadable file) stays
  pending: it remains visible and is saved with the next write;
- a damaged file is moved aside and rebuilt from the last good copy, a
  short read is retried first, and only a missing file counts as empty;
- a change that makes no difference doesn't rewrite the file.

Enumerating the container also returned the serialized strings instead of
the values, and setting a key to null wrote the file twice.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added area/skia ✏️ Categorizes an issue or PR as relevant to Skia area/automation Categorizes an issue or PR as relevant to project automation labels Oct 26, 2026
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 241465 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • ClipboardTests.png-dark
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DisplayInformation.png-dark
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • Examples.png
    • ImageBrushAlignmentXY.png-dark
    • ImageBrushAlignmentXY.png
    • Image_UseTargetSizeLate.png-dark
  • skia-windows-screenshots: 2458 changed over 2490

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppBarButtonTest.png
    • ApplicationViewMode.png
    • ApplicationViewMode.png-dark
    • AppWindowFeatures.png-dark
    • Application_DispatcherShutdownMode.png
    • AppWindowTitleBar Properties.png-dark
    • AppWindowTitleBar Properties.png
    • Arrange_Performance01.png
    • Attributed_text_FontSize_Changing.png-dark
    • AuthenticationBroker_Cancel.png-dark
    • AuthenticationBroker_Demo.png-dark
    • AutoBorderStretchwithrightmargin.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_Relations.png-dark
    • AutomationProperties_AutomationId.png
    • AutoSuggestBoxChosenSuggestion.png-dark
    • AutomationProperties_Landmark.png-dark
    • AutoSuggestBoxChosenSuggestion.png

@mergify

mergify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@ramezgerges
ramezgerges merged commit c61670a into unoplatform:master Oct 27, 2026
46 checks passed
@ramezgerges
ramezgerges deleted the dev/rara/localsettings-multi-instance branch October 27, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation area/skia ✏️ Categorizes an issue or PR as relevant to Skia

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants