Skip to content

When unexpected power outage, settings.json may half save. #1275

@fernicar

Description

@fernicar

What happened?

If the application is in the process of saving settings.json when an unexpected power outage occurs, the file may not be fully written to disk, resulting in possible corruption. This issue could leave users without a way to recover their settings, causing frustration and loss of important configurations. A backup or recovery system could be implemented to ensure settings are preserved even in cases of abrupt shutdowns.

Steps to reproduce

  1. Run the application and modify settings.
  2. Allow the application to save settings.json.
  3. Close the application.
  4. Manually open settings.json in a text editor and delete a few lines at the end to simulate an incomplete save.
  5. Open the application again and observe how it handles the corrupted file.

This version should make it easier to reproduce the issue reliably without the power outage.

Relevant logs

2025-05-22 13:05:36.4092|WARN|StabilityMatrix.Avalonia.Program|Unhandled "JsonException": "'0x00' is invalid within a JSON string. The string should be correctly escaped. Path: $ | LineNumber: 3293 | BytePositionInLine: 9."|System.Text.Json.JsonException: '0x00' is invalid within a JSON string. The string should be correctly escaped. Path: $ | LineNumber: 3293 | BytePositionInLine: 9.
 ---> System.Text.Json.JsonReaderException: '0x00' is invalid within a JSON string. The string should be correctly escaped. LineNumber: 3293 | BytePositionInLine: 9.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ConsumeStringAndValidate(ReadOnlySpan`1 data, Int32 idx)
   at System.Text.Json.Utf8JsonReader.ConsumeString()
   at System.Text.Json.Utf8JsonReader.ConsumePropertyName()
   at System.Text.Json.Utf8JsonReader.ConsumeNextToken(Byte marker)
   at System.Text.Json.Utf8JsonReader.ConsumeNextTokenOrRollback(Byte marker)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.ContinueDeserialize(ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, T& value)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Stream utf8Json)
   at StabilityMatrix.Core.Services.SettingsManager.LoadSettings(CancellationToken cancellationToken)
   at StabilityMatrix.Core.Services.SettingsManager.TryFindLibrary(Boolean forceReload)
   at StabilityMatrix.Avalonia.App.ConfigureServiceProvider()
   at StabilityMatrix.Avalonia.App.OnFrameworkInitializationCompleted()
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, Action`1 lifetimeBuilder)
   at StabilityMatrix.Avalonia.Program.Main(String[] args)

Version

v2.14.1

What Operating System are you using?

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleStale issue or inactive for long period of time

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions