Skip to content

fix(storage): write settings in place when renames are refused - #26711

Open
rafael-rosa-knowcode wants to merge 4 commits into
masterfrom
dev/rr/sandbox-settings-write-in-place
Open

rafael-rosa-knowcode wants to merge 4 commits into
masterfrom
dev/rr/sandbox-settings-write-in-place

Conversation

@rafael-rosa-knowcode

@rafael-rosa-knowcode rafael-rosa-knowcode commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: related to unoplatform/studio.live#6221 (not closed by this PR: Studio closes it when it takes the Uno version with this fix)

PR Type:

🐞 Bugfix

Why the sandbox needs this

Win32 app isolation (overview) lets a packaged desktop app run sandboxed, in an AppContainer.

  • The install dialog then no longer says "Uses all system resources", as it does for a full-trust package.
  • The app can still read and write its own data folder (%LOCALAPPDATA%\Packages\<family>\AC).
  • It cannot rename anything in that folder: every rename is refused with access denied. Writing, copying and deleting files there all work.
  • Any Uno app packaged this way hits this, because Uno saves ApplicationData settings through a rename.

The case: Uno Platform Studio's Windows package is moving from full trust to this sandbox (unoplatform/studio.live#5298).

  • In the sandboxed package (Windows 11 build 26300), every settings save fails, and changes are lost when Studio closes (unoplatform/studio.live#6221).
  • The same refusal broke sign-in, fixed in the licensing SDK the same way (unoplatform/uno.licensing#824).
  • It blocks shipping the sandboxed package: a user would lose their settings on every restart.

What changed? 🚀

The bug: in an app packaged with Win32 app isolation (a sandboxed MSIX), ApplicationData.Current.LocalSettings is never saved on Skia desktop.

  • Since fix(storage): Merge settings writes across instances #25715, SettingsFile writes a .tmp file and renames it over Local.dat.
  • The sandbox refuses every rename in the package's data folder (UnauthorizedAccessException).
  • Every write logs Failed to write settings to …\Local.dat; the change is kept in memory and saved on a later write, so changes are lost at exit.
  • A damaged file is also moved to .corrupt with a rename. In the sandbox that is refused too, so one damaged file would block every later save.

The fix, in src/Uno.WinRT/Storage/Internal/SettingsFile.skia.cs:

  • Only the rename is watched. When it is still refused after the existing retries, the file is written in place, under the same exclusive write lock.
  • While renames are refused, each later write tries the rename once, with no retries. If it works, writes go back to the rename. A brief hold by another program therefore doesn't leave an app writing in place for the rest of the run.
  • A damaged file is copied to .corrupt when moving it is refused, then rebuilt as before. This doesn't change the write mode.
  • Any other failure keeps today's behaviour. Wherever renames work, nothing changes.
  • Logs: one Warning when the switch happens, with the HResult; one Info when renames work again; Debug for each write retry; a separate Error when the in-place write also fails.

Flow of one save. Default colour: unchanged. Red: what happened before and no longer does. Green: added by this PR.

flowchart TD
  S1["Set a value"] --> S2["Take the write lock, read Local.dat"]
  S2 -- "file damaged" --> S3{"Move Local.dat to .corrupt"}
  S3 -- "refused (before)" --> R1["Every later save fails"]:::removed
  S3 -- "refused (now)" --> G1["Copy Local.dat to .corrupt"]:::added
  S3 -- "works" --> S4["Rebuild from the last good copy"]
  G1 --> S4
  S2 -- "file fine" --> S5["Apply the change, write a .tmp file"]
  S4 --> S5
  S5 --> S6{"Rename .tmp over Local.dat"}
  S6 -- "works" --> S7["Saved"]
  S7 --> G2["If saves were in place: back to renaming, Info logged"]:::added
  S6 -- "refused" --> G3{"Renames already refused this run?"}:::added
  G3 -- "no" --> S8["Retry, up to 5 times"]
  S8 -- "still refused (before)" --> R2["Error logged; change kept in memory only, lost at exit"]:::removed
  S8 -- "still refused (now)" --> G4["Write Local.dat in place, under the lock"]:::added
  G3 -- "yes" --> G4
  G4 --> G5["Saved; Warning logged once; later saves try the rename once"]:::added

  classDef removed fill:#ffebe9,stroke:#cf222e,color:#82071e,stroke-width:2px
  classDef added fill:#dafbe1,stroke:#1a7f37,color:#044f1e,stroke-width:2px
Loading

Trade-off (only where renames are refused): the in-place write is not atomic.

  • The new content goes down in one write, then the old tail is cut off. Readers already ignore trailing bytes, so a process crash after the write returns is safe.
  • A power loss or OS crash that tears that write can leave a damaged file, or one whose values read back wrong without any error. There is no checksum.
  • This is the same exposure Uno had before fix(storage): Merge settings writes across instances #25715, which wrote in place.

Reviewer's guide

  • Start here: SettingsFile.TryWrite, then ReplaceThroughTemporaryFile (returns the refused rename, or null), TryWriteInPlace / WriteInPlace, SetDamagedFileAside, NoteRenamesRefused / NoteRenamesWork.
  • Seam: an optional moveFile constructor argument (default File.Move(…, overwrite: true)) so the tests can refuse a rename. Both renames go through it.
  • Mechanical: serialisation moved into Serialize (a BinaryWriter over a MemoryStream, same format), and the temporary-file delete into DeleteTemporaryFile so its failure can't hide the write's outcome.
  • Tests (Given_SettingsFile):
    • When_Renames_Are_Refused_Then_The_Value_Is_Saved_In_Place
    • When_Renames_Are_Refused_Then_Later_Writes_Try_The_Rename_Only_Once
    • When_Renames_Work_Again_Then_Writes_Retry_Them_Fully_Again
    • When_Renames_Are_Refused_And_The_Content_Shrinks_Then_No_Old_Bytes_Are_Read
    • When_Renames_Are_Refused_And_The_File_Is_Damaged_Then_It_Is_Copied_Aside_And_Rebuilt
    • When_Only_Moving_A_Damaged_File_Aside_Is_Refused_Then_It_Is_Copied_Aside_And_Saved
    • When_A_Rename_Is_Refused_Once_Then_It_Is_Retried_Rather_Than_Written_In_Place
    • When_A_Rename_Fails_For_Another_Reason_Then_The_File_Is_Not_Written_In_Place

Validation

On fcaf3d77da8:

  • Compile: Uno.UI.UnitTests (net11.0) builds with 0 errors and 0 warnings, analyzers on (UnoFastDevBuild=false).
  • Runtime (unit tests, Linux): Given_SettingsFile 24/24 pass. The first sandbox tests failed before the fix.
  • Full Uno.UI.UnitTests suite (on 8da7433aa95): 15 failures, all of which also fail on unchanged master on the same machine (styles, themes, a time picker). None in storage.
  • Review: two rounds of an internal review panel (correctness, quality, security, operability). The second round found no blocker or high issue.
  • Not yet: a run in a sandboxed package on Windows, including which exception and HResult the refused move raises there. The same sandbox refused renames with UnauthorizedAccessException in the Uno licensing SDK, which is what this relies on.
Review items not taken
  • No .bak backup before the in-place write: it would grow the change, and the torn-write risk above is the one it covers.
  • The in-place write is not retried on a sharing violation; that write fails, and the change stays pending for the next write.
  • The first write in a sandbox pays the five rename retries (about 200 ms) once per settings file per run.

PR Checklist ✅

  • 🧪 Added unit tests
  • 📚 Docs — n/a, no public behaviour change outside sandboxed packages
  • 🖼️ Validated PR Screenshots Compare Test Run results.
  • ❗ Contains NO breaking changes
  • 👀 Reviewed 2 other open pull requests

🤖 Generated with Claude Code

Win32 app isolation refuses every rename in a sandboxed package's data
folder, so saving settings through a temporary file and a rename failed
on every write: changes were kept in memory only and lost at exit.

When the rename is still refused after the retries, write the file in
place under the existing write lock and skip the rename for the rest of
the run. A damaged file is copied aside instead of moved. Wherever
renames work, nothing changes.

Co-Authored-By: Claude Opus 5.5 <[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 Nov 25, 2026
rafael-rosa-knowcode and others added 2 commits November 25, 2026 11:08
Review follow-up. The fallback now triggers only when the rename itself
is refused after the retries, not on any access-denied error from the
write. While renames are refused, each write still tries one rename, so
a brief hold by another program does not leave the app writing in place
for the rest of the run. Moving a damaged file aside no longer switches
the mode; it is copied when the move is refused.

The in-place write puts the new content down in one write and then cuts
the old tail, which readers already ignore. The switch is logged once at
Warning with the error code, and write retries are logged at Debug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Second review round. Deleting the temporary file no longer throws over
the outcome of the write, so a refused rename still reaches the in-place
write. The retry comment sits on the rename path it describes, and two
tests now fail if a single refusal switches to in-place writes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

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

…he temporary-file delete

Review follow-up. Any other exception from the in-place write still
reaches the write's outer handler, which logs it and keeps the change
for a later write, so the behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 249061 found UI Test snapshots differences: skia-linux-screenshots: 223, skia-windows-screenshots: 286

Details
  • skia-linux-screenshots: 223 changed over 2504

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • Accessibility_Showcase.png-dark
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • Basics.png-dark
    • Basics.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • AnimatedVisualsTestbedPage.png-dark
    • AppWindowTitleBar Properties.png-dark
    • AutomationProperties_AutomationId.png-dark
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • BasicAcrylicBrushTest.png-dark
  • skia-windows-screenshots: 286 changed over 2504

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AppWindowTitleBar Properties.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • Buttons.png-dark
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • Accessibility_Showcase.png-dark
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedVisualsTestbedPage.png
    • AutomationProperties_StructureAndProps.png-dark
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • ButtonClippingTestsControl.png-dark

@rafael-rosa-knowcode
rafael-rosa-knowcode marked this pull request as ready for review November 25, 2026 16:30
@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

@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 249078 found UI Test snapshots differences: skia-linux-screenshots: 260, skia-windows-screenshots: 281

Details
  • skia-linux-screenshots: 260 changed over 2504

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_Showcase.png-dark
    • Basics.png-dark
    • Basics.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • AnimatedVisualsTestbedPage.png
    • BitmapIcon_Foreground.png-dark
    • BitmapIcon_Foreground.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • ColorPickerSample.png-dark
    • ColorPickerSample.png
    • AppWindowTitleBar Properties.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • AnimatedVisualsTestbedPage.png-dark
  • skia-windows-screenshots: 281 changed over 2504

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_Showcase.png-dark
    • Basics.png-dark
    • Basics.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • AnimatedBackVisualSourceSamplePage.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • AnimatedVisualsTestbedPage.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • BreadcrumbBarPage.png-dark

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.

3 participants