-
Notifications
You must be signed in to change notification settings - Fork 22
Don't crash on concurrent access to telemetrySettings.json #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/fix-telemetry-settings-ioexception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6992a5a
Initial plan
Copilot 603061a
Tolerate concurrent access to configuration files
Copilot 5bbdd3d
Assert save is pending while the config file is locked
Copilot eec0f72
Address review: repair path for missing file, fail-closed telemetry, …
Copilot 097c635
Only treat real sharing violations as contention; never persist telem…
Copilot 2d819b6
Address review: TryLoadAsync for ResetAsync, read-shared LoadAsync, d…
Copilot 98ec1f0
Address Copilot review: scope the retry signal and correct the TryLoa…
azchohfi 27b238e
Make reset failures visible and stop TryLoadAsync reporting a read th…
azchohfi 4d29c24
Don't log the same contention warning twice
azchohfi b15cd9e
Treat a vanished configuration file as missing, not unreadable
azchohfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using MSStore.CLI.Services; | ||
| using MSStore.CLI.Services.Telemetry; | ||
|
|
||
| namespace MSStore.CLI.UnitTests | ||
| { | ||
| [TestClass] | ||
| public class ConfigurationManagerUnitTests | ||
| { | ||
| private ConfigurationManager<TelemetryConfigurations> _configurationManager = null!; | ||
|
|
||
| public TestContext TestContext { get; set; } = null!; | ||
|
|
||
| [TestInitialize] | ||
| public void Initialize() | ||
| { | ||
| _configurationManager = new ConfigurationManager<TelemetryConfigurations>( | ||
| TelemetrySourceGenerationContext.Default.TelemetryConfigurations, | ||
| $"test_{Guid.NewGuid()}.json", | ||
| null); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public void Cleanup() | ||
| { | ||
| if (File.Exists(_configurationManager.ConfigPath)) | ||
| { | ||
| File.Delete(_configurationManager.ConfigPath); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task SaveAsyncWaitsForOtherProcessToReleaseTheFile() | ||
| { | ||
| await _configurationManager.ClearAsync(TestContext.CancellationToken); | ||
|
|
||
| var otherProcessFile = File.Open(_configurationManager.ConfigPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); | ||
|
|
||
| var saveTask = _configurationManager.SaveAsync(new TelemetryConfigurations { TelemetryEnabled = true }, TestContext.CancellationToken); | ||
|
|
||
| await Task.Delay(100, TestContext.CancellationToken); | ||
|
|
||
| // The save must not have completed yet, otherwise it did not really | ||
| // wait for the other process to release the file. | ||
| saveTask.IsCompleted.Should().BeFalse(); | ||
|
|
||
| otherProcessFile.Dispose(); | ||
|
|
||
| await saveTask; | ||
|
|
||
| var telemetryConfigurations = await _configurationManager.LoadAsync(true, TestContext.CancellationToken); | ||
|
|
||
| telemetryConfigurations.TelemetryEnabled.Should().BeTrue(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task LoadAsyncDoesNotThrowIfFileIsLockedByAnotherProcess() | ||
| { | ||
| await _configurationManager.ClearAsync(TestContext.CancellationToken); | ||
| await _configurationManager.SaveAsync(new TelemetryConfigurations { TelemetryEnabled = true }, TestContext.CancellationToken); | ||
|
|
||
| using var otherProcessFile = File.Open(_configurationManager.ConfigPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); | ||
|
|
||
| var telemetryConfigurations = await _configurationManager.LoadAsync(true, TestContext.CancellationToken); | ||
|
|
||
| telemetryConfigurations.Should().NotBeNull(); | ||
|
|
||
| // The file of the other process should not have been cleared. | ||
| otherProcessFile.Length.Should().BeGreaterThan(0); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ConcurrentLoadsAndSavesDoNotThrow() | ||
| { | ||
| var tasks = new List<Task>(); | ||
|
|
||
| for (var i = 0; i < 5; i++) | ||
| { | ||
| tasks.Add(Task.Run( | ||
| async () => | ||
| { | ||
| var telemetryConfigurations = await _configurationManager.LoadAsync(true, TestContext.CancellationToken); | ||
| telemetryConfigurations.TelemetryGuid = Guid.NewGuid().ToString(); | ||
| await _configurationManager.SaveAsync(telemetryConfigurations, TestContext.CancellationToken); | ||
| }, | ||
| TestContext.CancellationToken)); | ||
| } | ||
|
|
||
| var act = () => Task.WhenAll(tasks); | ||
|
|
||
| await act.Should().NotThrowAsync(); | ||
|
azchohfi marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.