Skip to content

[Repo Assist] improve: cache JsonSerializerOptions + atomic write in AppSettings #162

Closed

Description

@github-actions

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Two self-contained improvements to AppSettings.cs:

1. Cache JsonSerializerOptions as static readonly fields

Previously, Load() allocated a new JsonSerializerOptions on every call, and Save() did the same. JsonSerializerOptions is a heavyweight object — it builds internal reflection caches on construction. Promoting them to static readonly means the cost is paid once at class load time.

// Before (per-call allocation)
var options = new JsonSerializerOptions { AllowTrailingCommas = true, ... };

// After (one-time allocation)
private static readonly JsonSerializerOptions ReadOptions = new JsonSerializerOptions { ... };
private static readonly JsonSerializerOptions WriteOptions = new JsonSerializerOptions { ... };

2. Atomic write in Save()

Previously, Save() called File.WriteAllText() directly on settings.json. File.WriteAllText opens the file, truncates it, then writes. If the process is killed between truncation and the final flush (e.g. forced shutdown, BSOD), the file is left in a partially-written or zero-length state — causing the next launch to silently reset all settings.

Fix: write to settings.json.tmp first, then call File.Move(tempPath, realPath, overwrite: true). On NTFS, a rename within the same volume is a single atomic metadata operation — the target is either the old intact file or the new complete file, never a partial write.

// Before
File.WriteAllText(SettingsFilePath, json);

// After
var tempPath = SettingsFilePath + ".tmp";
File.WriteAllText(tempPath, json);
File.Move(tempPath, SettingsFilePath, overwrite: true);

Trade-offs

  • No behaviour change for successful writes.
  • The .tmp file is overwritten on the next save, so a leftover file from a prior crash doesn't cause issues.
  • File.Move(overwrite: true) is available from .NET 3.0+.

Test Status

Step Result
dotnet build -p:EnableWindowsTargeting=true ✅ 0 errors, 0 warnings

Note

🔒 Integrity filter blocked 3 items

The following items were blocked because they don't meet the GitHub integrity level.

  • #144 list_pull_requests: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • Detect camera is being used by another application and automatically turn on #21 list_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #21 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist · ● 3.3M · ◷

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch repo-assist/improve-appsettings-2026-08-03-7895d572406b69f6.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (95 of 95 lines)
From 73e9576aea770ac6ff246987e91105dea15cf719 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Mon, 3 Aug 2026 13:46:42 +0000
Subject: [PATCH] improve: cache JsonSerializerOptions + atomic write in
 AppSettings
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Two improvements to AppSettings:

1. Static readonly JsonSerializerOptions
   Previously, Load() and Save() each allocated a new JsonSerializerOptions
   on every call. JsonSerializerOptions is a heavyweight object (it builds
   internal caches). Promoting them to static readonly fields means the
   object is created once at class load time.

2. Atomic write in Save()
   Previously, Save() called File.WriteAllText() directly on settings.json.
   If the process was killed or lost power between the file being truncated
   and the write completing, the file would be left in a partially-written,
   corrupt state — causing the next launch to silently reset all settings.

   Fix: write to settings.json.tmp first, then use File.Move(overwrite:true)
   to rename it into place. On NTFS this rename is an atomic metadata
   operation: the target is either the old intact file or the new complete
   file, never a partial write.

Build: 0 errors, 0 warnings (net10.0-windows, EnableWindowsTargeting=true)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 WindowsEdgeLight/AppSettings.cs | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/WindowsEdgeLight/AppSettings.cs b/WindowsEdgeLight/AppSettings.cs
index 1f46de9..af1b5d3 100644
--- a/WindowsEdgeLight/AppSettings.cs
+++ b/WindowsEdgeLight/AppSettings.cs
@@ -14,6 +14,17 @@ public class AppSettings
         "WindowsEdgeLight",
         "settings.json");
 
+    private static readonly JsonSerializerOptions ReadOptions = new JsonSerializerOptions
+    {
+        AllowTrailingCommas = true,
+        ReadComme
... (truncated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions