Skip to content

A settings file that loaded before an app update makes every Get() throw InvalidOperationException instead of being archived as .corrupt #353

Closed

Description

@matt-edmondson

What's wrong

LoadOrCreate recovers from an unreadable file by archiving it as .corrupt and falling back. The catch filter at AppDataStorage/AppData.cs:672 only matches JsonException, ArgumentException, FormatException and NotSupportedException:

catch (Exception ex) when (ex is JsonException or ArgumentException or FormatException or NotSupportedException)

The serializer options set ReferenceHandler = ReferenceHandler.Preserve (AppData.cs:41). With that setting, System.Text.Json throws InvalidOperationException when a $ref points at an object whose type doesn't fit the property it is assigned to. That exception gets past the filter. The file is not archived, no fallback runs, and because InternalState is PublicationOnly (AppData.cs:459), every later Get() retries and throws again for the rest of the process.

Why it matters

This is the "model change in an app update" case that the comment at AppData.cs:674-676 says the catch handles:

  1. v1 of an app has two properties of the same type that share one instance, so the saved file contains "B": { "$ref": "2" }.
  2. v2 changes the type of B.
  3. On first launch after the update, Settings.Get() throws on every call and the app can't start, even though deleting the file would fix it.

Repro (v1 file written with A and B sharing one Inner; v2 declares B as Other; Settings.Get() called three times):

Get #0: System.InvalidOperationException: The object with reference id '2' of type 'Inner' cannot be assigned to the type 'Other'.
Get #1: (same)
Get #2: (same)
files: settings.json        <- not archived, no recovery

LoadOrCreateIn(root) throws the same way on a hand-edited file of that shape.

Suggested fix / acceptance criteria

  • Add InvalidOperationException to the filter at AppData.cs:672, or otherwise cover every exception the deserializer can raise because of the data rather than the environment.
  • Add a test: a file whose $ref points at a mismatched type is archived as settings.json.corrupt.<timestamp>, LoadOrCreate returns defaults (or the .tmp/.bk copy when one exists), and Get() doesn't throw.

Activity

  1. added
    bug
    on Oct 19, 2026
  2. matt-edmondson commented on Oct 19, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions