Clean up registry lock file after parse failure #2175
Merged
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.
I have an old KSP 0.90 install that has a corrupted
registry.json
file (truncated in the middle of a string in the section about the installed files). If I try to access this instance withRegistryManager.Instance()
, an appropriateJsonReaderException
is thrown in the constructor ofRegistryManager
and no access is possible, but theregistry.locked
file is still in the folder (it is created byGetLock
immediately before the parsing is attempted). Depending on exactly what the process does after that, additional exceptions can occur at random when various of the object's partially constructed resources are destroyed by the garbage collector. The lock file does disappear later when the process exits.When a constructor throws an exception, this means the requested object cannot be created, so it should first clean up any resources it has allocated up to that point, including things like lock files, so the program is in a consistent state.
This change intercepts parsing exceptions in the constructor and calls
Dispose
to clean up, just like the destructor does, before re-throwing the exception. This causes the lock file to be cleaned up if the registry can't be parsed, while still alerting the calling code to the problem. It also fixes the seemingly random exceptions that I was seeing afterwards.