Repository navigation
fix(config): Make config.xml saves durable and recover from crash damage - #306
Open
karlkeefer wants to merge 1 commit into
Open
karlkeefer wants to merge 1 commit into
karlkeefer wants to merge 1 commit into
Conversation
After a crash or power loss shortly after a save, config.xml could be left at the right length but filled with NUL bytes, and SyncTrayzor then refused to start until the user deleted it. AtomicFileStream wrote a temp file and renamed it over config.xml, but never flushed the temp file's data to disk first, so the rename could reach disk before the contents did. AtomicFileStream: - Add Commit(), which calls FlushFileBuffers before the rename. Disposing without committing (e.g. serialization threw) discards the temp file instead of replacing config.xml with a partial one. - Never touch the filesystem from the finalizer. - Optionally keep a flushed, best-effort copy of the previous file. - Actually report MoveFileEx failures: a Win32 error code was being passed to ThrowExceptionForHR, which ignores positive values, so failed renames were silently dropped. ConfigurationProvider: - Keep the previous config as config.xml.lastgood. - If config.xml fails to parse and looks crash-damaged (empty or contains NUL bytes), and the last-good copy loads fully, recover from it and move the damaged file to config.xml.damaged. Hand-edit typos and a missing config.xml behave as before. - Retry saves on UnauthorizedAccessException too, with a linear backoff (~2s total), and fix the retry loop reporting failure after a retry succeeded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
karlkeefer
marked this pull request as ready for review
November 24, 2026 08:10
Author
|
Zero crashes after ~14 days - prior it was multiple times per week. I'm satisfied this patch improved the situation - so un |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Draft: I'm testing this by running a build of this branch on the affected machine. I'll mark it ready once it has survived some real unclean shutdowns.
AI disclosure: The diagnosis, code, tests and this description were written with Claude (via Claude Code). I've reviewed them, but please review with that in mind.
Problem
After an unclean shutdown, SyncTrayzor can refuse to start:
config.xmlis the right length but all0x00(hexadecimal value 0x00 is an invalid character). The only fix is deleting it and losing the settings. This looks like the same symptom as canton7/SyncTrayzor#471.Cause:
AtomicFileStreamrenamesconfig.xml.tmpoverconfig.xmlwithout first flushing the temp file's data to disk. NTFS saves the rename and the file size before the contents, so a crash in that window leaves a zero-filled file.Changes
AtomicFileStream.Commit()callsFlushFileBuffersbefore the rename. Disposing without a commit discards the temp file instead of renaming a partial one, and nothing happens on the finalizer path.config.xml.lastgood. That copy is used only whenconfig.xmlfails to parse and looks crash-damaged (empty or containing NULs) and the backup loads fully. Typos and a missingconfig.xmlbehave exactly as before.UnauthorizedAccessException, with a ~2 s backoff. The loop no longer throws after a retry has succeeded.ThrowExceptionForHR). A save that keeps failing now goes through the existing retry loop andCouldNotSaveConfigurationExeptiondialog.16 new tests; 74 pass locally.
Environment, stack trace and crash timeline
This machine has frequent unclean shutdowns: 36 Kernel-Power 41 events in 60 days. Some are recurring
0x3Bbugchecks at the same offset (likely a third-party driver I'm still tracking down), and the rest are failed resumes from sleep. That's not SyncTrayzor's fault, but it shouldn't lose its config to them.config.xml0x3B/ Kernel-Power 41config.xml= 2,706 ×0x00Change details
AtomicFileStreamCommit()doesFlush(flushToDisk: true), and only a committed stream is renamed into place onDispose.base.Disposethrows.Dispose(false)(the finalizer) no longer renames anything.MoveFileExerrors now go throughGetHRForLastWin32Error()and are actually thrown.IFilesystemProvider.CreateAtomicreturnsAtomicFileStream, so its only caller canCommit().ConfigurationProviderconfig.xml.damagedfor diagnosis, and only after the backup has fully loaded (parsed, migrated and deserialized).Tests (
ConfigurationDurabilityTests).lastgood; retries until a briefly locked file is released; a persistently locked file throwsCouldNotSaveConfigurationExeptionand is left intact.config.xmlin place;config.xmlwith a backup present still starts from defaults.Real power-loss durability can't be unit-tested; that's what the draft period on the affected machine is for.