Skip to content

fix(resources): make AddLookupAssembly transactional on parse failure (backport #23960) - #25971

Open
mergify[bot] wants to merge 5 commits into
masterfrom
mergify/bp/master/pr-23960
Open

mergify[bot] wants to merge 5 commits into
masterfrom
mergify/bp/master/pr-23960

Conversation

@mergify

@mergify mergify Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #23959

PR Type:

🐞 Bugfix

What changed? 🚀

Follow-up to #23707. AddLookupAssembly rolled back the _lookupAssemblies registration and parsed-resource markers when parsing threw, but ProcessResourceFile merges each key/value pair straight into the live loader dictionaries as it reads. A .upri with a valid header that declares two pairs, carries one complete pair, then truncates fails on the second pair — after the rollback, the unregistered assembly's first pair remained observable through GetString (permanently, when the loader context was already established, since no later lookup reloads).

Every path that mutates the live loader state now goes through a single transactional parse.

  • TransactionalParse (new private nested type) owns the temp dictionaries, the temp marker set, the loader resolver, and the two apply phases (MergeIntoLiveLoaders, ReplaceLiveLoaders). It replaces the three hand-rolled copies of the temp-then-apply pattern the file had grown.
  • AddLookupAssembly — single-assembly branch (changed loader context): ProcessAssemblyTransactionally parses into temporaries with the marker set seeded from the assembly's already-parsed markers, so skip semantics are unchanged, and merges only after every file parsed successfully.
  • AddLookupAssembly — full-reload branch: ReloadResources is itself transactional now, so the failure never touches the live state. That also makes the culture-change path (EnsureLoadersCultures → ReloadResources) transactional, which the earlier revision did not cover.
  • Consequently the liveLoadersTouched control flag, the RebuildLoaderResourcesFromSurvivors() recovery call in the catch, and the live-marker rollback (_parsedResources.RemoveWhere) are all gone: removing the list entry is the whole rollback. RebuildLoaderResourcesFromSurvivors keeps its per-assembly guard and is still used by the ALC sweep. ClearResources() and the two-argument ProcessAssembly overload became unused and were removed.
  • Rollback logging: the catch now names the exception and logs before rethrowing (gated on _log.IsEnabled(LogLevel.Error)), reporting the assembly whose registration was rolled back and whether the whole cache was being reloaded — in that branch the faulting .upri can belong to a pre-existing registration rather than to the assembly being rolled back.
  • Exception contract: a .upri truncated in the middle of its declared pairs surfaced BinaryReader's context-free EndOfStreamException. ProcessResourceFile now restates it as InvalidDataException($"Truncated resource file {fileName}: …", error), matching what it already does (and documents) for every other malformed-.upri case. InvalidDataException is already in the rebuild guard's filter, so recovery semantics are unchanged.
  • Allocation: the merge apply hands the parsed dictionary over on a TryGetValue miss instead of allocating an empty dictionary and copying every pair — the zero-copy ownership transfer RebuildLoaderResourcesFromSurvivors already used.
  • Documented narrowing: RebuildLoaderResourcesFromSurvivors re-derives values only for the last established context's language preferences, so with PreserveParsedResources enabled it drops values preserved for other cultures (they are re-parsed the next time those languages become active). That is now stated in its XML docs.

The HasContextChangedSignificantly branch selection in AddLookupAssembly is unchanged; the two comments describing those branches were swapped and are now accurate.

Red/green regression tests

Two new tests in Given_ResourceLoader_Alc emit (in memory, via ManagedPEBuilder) an assembly embedding a .upri with valid magic/version/name/culture, resourceCount = 2, one complete pair, truncated before the second pair. Both carry [Description] and [GitHubWorkItem]:

  • When_AddLookupAssembly_Truncated_Upri_Then_Throws_And_Partial_Value_Not_Observable — established loader context (full-reload branch): asserts AddLookupAssembly throws InvalidDataException, ContainsLookupAssembly is false, the truncated .upri's loader instance was never created, the first pair's value is not returned by GetString, and surviving registrations still resolve.
  • When_AddLookupAssembly_Truncated_Upri_With_Changed_Context_Then_Live_Loaders_Untouched — changed loader context (single-assembly branch): asserts the throw, the rollback, that the loader instance was never created (absence, not emptiness, so a regression to the full-reload branch cannot keep the test green), and that no value reached the live loaders.

Live state is observed through two new internal test seams next to ContainsLookupAssembly — TryGetMergedResourceForTests and ContainsNamedLoader — replacing the private-field reflection helper the earlier revision used. GetString cannot be used for these assertions: it re-establishes the loader context and would reload (and wipe) the loaders, masking the leak.

Both tests fail on the pre-fix code with the leaked TruncatedValue observable in both branches, and on the loader-instance assertions, and pass with the fix. Full Given_ResourceLoader_Alc (8 tests) and Given_AlcCacheSweep (4 tests) pass; the full Uno.UI.UnitTests run is 4074 total / 12 failed, and those 12 When_Gregorian_FixedDate* failures reproduce identically on the unmodified merge commit 2fc0049a256 (4072 total / same 12) — environment-dependent, unrelated.

PR Checklist ✅

🤖 Generated with Claude Code


This is an automatic backport of pull request #23960 done by [Mergify](https://mergify.com).

Nick Randolph and others added 4 commits November 3, 2026 07:59
- parse a newly registered assembly's .upri files into temporary
  dictionaries and merge into the live loaders only after every file
  parsed successfully (same temp-then-apply pattern as
  RebuildLoaderResourcesFromSurvivors)
- when the failing registration went through the full reload path,
  re-derive the merged values and parsed markers from the surviving
  registrations so a truncated .upri's partially parsed pairs cannot
  remain observable after the rollback
- drop the now-dead live marker rollback (a failing parse no longer
  writes markers into the live set)
- add red/green regression tests covering both AddLookupAssembly parse
  branches with a .upri that declares two pairs and truncates after
  the first

Fixes #23959

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit e47e138)
…actional parse

Review follow-up on the transactional `AddLookupAssembly` fix.

- Extract `TransactionalParse` (temp dictionaries + marker set + resolver +
  merge/replace apply) and route `AddLookupAssembly`, `ReloadResources` and
  `RebuildLoaderResourcesFromSurvivors` through it, replacing three hand-rolled
  copies of the temp-then-apply pattern.
- `ReloadResources` is now transactional too, so the culture-change path via
  `EnsureLoadersCultures` no longer leaks partial state; the `liveLoadersTouched`
  control flag and the rebuild-on-failure recovery in the catch are both gone.
- Log the rollback before rethrowing (gated on `LogLevel.Error`), naming the
  assembly and whether the whole cache was being reloaded.
- Restate a `.upri` truncated mid key/value pair as `InvalidDataException`
  naming the file, matching every other malformed-`.upri` case, instead of
  surfacing `BinaryReader`'s context-free `EndOfStreamException`.
- Hand the parsed dictionary over on a `TryGetValue` miss instead of allocating
  an empty one and copying every pair.
- Document the language-preference narrowing `RebuildLoaderResourcesFromSurvivors`
  applies when `PreserveParsedResources` is enabled.
- Tests: add `[Description]` + `[GitHubWorkItem]`, assert the documented
  exception type, assert the truncated loader instance is never created, and
  replace the private-field reflection helper with the
  `TryGetMergedResourceForTests` / `ContainsNamedLoader` internal seams.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit a44268e)

# Conflicts:
#	src/Uno.WinRT/ApplicationModel/Resources/ResourceLoader.static.cs
- Move the resource-count read inside the try that restates
  EndOfStreamException as the documented InvalidDataException, so a stream
  ending at the count is named like every other truncation point instead of
  leaking BinaryReader's context-free exception.
- Keep the declared count in the message when it was read, and say so
  explicitly when the truncation preceded it.
- Add a red/green regression test for a .upri truncated before its count
  (fails with EndOfStreamException on the previous code).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 4488087)

# Conflicts:
#	src/Uno.WinRT/ApplicationModel/Resources/ResourceLoader.static.cs
- give each survivor in RebuildLoaderResourcesFromSurvivors its own scratch
  TransactionalParse, folded into the aggregate only once its Parse returned
- the shared parse let a survivor add a marker and its first key/value pair
  before throwing on a later pair; the guard caught, the loop continued, and
  the apply phase published the rejected survivor's partial state
- roll values AND markers back together: a leaked marker makes a later parse
  skip a file it should have read
- factor the temp-to-live merge loop into MergeInto so the new temp-to-temp
  fold reuses the same last-writer-wins-per-key semantics
- add the truncated-after-first-pair rebuild regression, plus the counterpart
  assertion that a good survivor's values are still published

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 09ad807)

# Conflicts:
#	src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.static.cs
@mergify mergify Bot added the conflicts Automated merge hit conflicts label Nov 3, 2026
@mergify

mergify Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of a44268e has failed:

On branch mergify/bp/master/pr-23960
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit a44268e.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   src/Uno.UI.UnitTests/ResourceLoader/Given_ResourceLoader_Alc.cs

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.WinRT/ApplicationModel/Resources/ResourceLoader.static.cs

Cherry-pick of 4488087 has failed:

On branch mergify/bp/master/pr-23960
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 4488087.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   src/Uno.UI.UnitTests/ResourceLoader/Given_ResourceLoader_Alc.cs

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.WinRT/ApplicationModel/Resources/ResourceLoader.static.cs

Cherry-pick of 09ad807 has failed:

On branch mergify/bp/master/pr-23960
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 09ad807.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   src/Uno.UI.UnitTests/ResourceLoader/Given_ResourceLoader_Alc.cs

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	deleted by us:   src/Uno.UWP/ApplicationModel/Resources/ResourceLoader.static.cs

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@github-actions github-actions Bot added the area/automation Categorizes an issue or PR as relevant to project automation label Nov 3, 2026
The backport recreated ResourceLoader.static.cs under the removed
Uno.UWP folder and deleted the live Uno.WinRT copy; move it back.
Port the transactional-parse changes onto master's Uno.WinRT file,
keeping its UPRI v3 parser, and reject an unreadable stream or an
out-of-range name length as InvalidDataException.

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

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Nick Randolph seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

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

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AndroidWindowInsets.png
    • Accessibility_ScreenReader.png-dark
    • AndroidWindowInsets.png-dark
    • Accessibility_ScreenReader.png
    • AnimatedIconPage.png-dark
    • AnimatedIconPage.png
    • AppBarButtonWithIconTest.png
    • AppBarButtonWithIconTest.png-dark
    • AnimatedVisualsTestbedPage.png
    • AppBarToggleButtonTest.png-dark
    • AppBarButtonTest.png-dark
    • AppBarButtonTest.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppBarToggleButtonTest.png
    • ApplicationViewMode.png
    • AppBar_KeyBoard.png
    • ApplicationViewSpanningRectsPage.png
    • ApplicationViewSizing.png-dark
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ApplicationViewMode.png
    • AppWindowClosing.png
    • AppWindowTitleBar Properties.png
    • Attributed_text_Simple.png-dark
    • AuthenticationBroker_Cancel.png-dark
    • AutomationPropertiesExtensions_Role.png-dark
    • AutoSizedPathCentered.png-dark
    • AutoSuggestBoxChosenSuggestion.png
    • AutoSuggestBox_Reason.png-dark
    • AutoSuggestBox_Reason.png
    • BasicAutoSuggestBox.png-dark
    • BasicAutoSuggestBox.png
    • BasicThemeResources.png-dark
    • BasicThemeResources.png
    • BindableDrawerLayout_ChangePane.png-dark
    • BindableDrawerLayout_ChangePane.png
    • BitmapImage_vs_SvgImageSource.png-dark
    • BitmapImage_vs_SvgImageSource.png
    • BorderTopwithmargins.png-dark
    • BorderTopwithmargins.png

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

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

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 conflicts Automated merge hit conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResourceLoader.AddLookupAssembly rollback can leave partially parsed .upri values observable

3 participants