-
Notifications
You must be signed in to change notification settings - Fork 790
Fix race condition in concurrent DeploymentState access causing intermittent AzureDeployerTests failures #11974
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
41c8220
Initial plan
Copilot 51ae33a
Fix race condition in JsonExtensions.Prop causing AzureDeployerTests …
Copilot eeb2ee7
Add thread-safety tests for JsonExtensions.Prop
Copilot 0e9dba7
Use ConditionalWeakTable for lock objects instead of locking on JsonO…
Copilot af312b2
Move thread-safety from JsonExtensions.Prop to ProvisioningContext level
Copilot c4f5c9c
Revert JsonExtensions.cs to original implementation
Copilot 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
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
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
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,60 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace Aspire.Hosting.Azure.Tests; | ||
|
|
||
| public class JsonExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void Prop_ReturnsExistingNode_WhenNodeAlreadyExists() | ||
| { | ||
| // Arrange | ||
| var rootJson = new JsonObject(); | ||
| var azureNode = rootJson.Prop("Azure"); | ||
| azureNode.AsObject()["TestProperty"] = "TestValue"; | ||
|
|
||
| // Act | ||
| var retrievedNode = rootJson.Prop("Azure"); | ||
|
|
||
| // Assert | ||
| Assert.Same(azureNode, retrievedNode); | ||
| Assert.Equal("TestValue", retrievedNode["TestProperty"]!.GetValue<string>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Prop_CreatesNewNode_WhenNodeDoesNotExist() | ||
| { | ||
| // Arrange | ||
| var rootJson = new JsonObject(); | ||
|
|
||
| // Act | ||
| var newNode = rootJson.Prop("NewProperty"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(newNode); | ||
| Assert.Same(rootJson["NewProperty"], newNode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Prop_NestedAccess_CreatesHierarchy() | ||
| { | ||
| // Arrange | ||
| var rootJson = new JsonObject(); | ||
|
|
||
| // Act | ||
| var deeply = rootJson.Prop("Level1") | ||
| .Prop("Level2") | ||
| .Prop("Level3") | ||
| .Prop("Level4"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(rootJson["Level1"]); | ||
| Assert.NotNull(rootJson["Level1"]!["Level2"]); | ||
| Assert.NotNull(rootJson["Level1"]!["Level2"]!["Level3"]); | ||
| Assert.NotNull(rootJson["Level1"]!["Level2"]!["Level3"]!["Level4"]); | ||
| Assert.Same(deeply, rootJson["Level1"]!["Level2"]!["Level3"]!["Level4"]); | ||
| } | ||
| } | ||
|
|
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 |
|---|---|---|
|
|
@@ -157,6 +157,175 @@ public void ProvisioningContext_CanBeCustomized() | |
| Assert.Equal("[email protected]", context.Principal.Name); | ||
| Assert.Equal("value", context.DeploymentState["test"]?.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithDeploymentState_ConcurrentAccess_IsThreadSafe() | ||
| { | ||
| // Arrange | ||
| var deploymentState = new JsonObject(); | ||
| var context = ProvisioningTestHelpers.CreateTestProvisioningContext(userSecrets: deploymentState); | ||
| const int threadCount = 10; | ||
| const int iterationsPerThread = 100; | ||
| var tasks = new Task[threadCount]; | ||
|
|
||
| // Act - Multiple threads accessing the DeploymentState concurrently via WithDeploymentState | ||
| for (int i = 0; i < threadCount; i++) | ||
| { | ||
| int threadId = i; | ||
| tasks[i] = Task.Run(() => | ||
| { | ||
| for (int j = 0; j < iterationsPerThread; j++) | ||
| { | ||
| context.WithDeploymentState(state => | ||
| { | ||
| // All threads try to get or create the same "Azure" property | ||
| var azureNode = state.Prop("Azure"); | ||
|
|
||
| // Each thread creates a unique property | ||
| var threadNode = azureNode.Prop($"Thread{threadId}"); | ||
| threadNode.AsObject()["Counter"] = j; | ||
|
|
||
| // And a shared property under Azure | ||
| var deploymentsNode = azureNode.Prop("Deployments"); | ||
|
|
||
| // Access a deeper nested property | ||
| var resourceNode = deploymentsNode.Prop($"Resource{j % 5}"); | ||
| resourceNode.AsObject()["LastAccess"] = $"Thread{threadId}-{j}"; | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Assert - Should complete without exceptions | ||
| await Task.WhenAll(tasks).WaitAsync(TimeSpan.FromSeconds(10)); | ||
|
|
||
| // Verify the structure was created correctly | ||
| context.WithDeploymentState(state => | ||
| { | ||
| Assert.NotNull(state["Azure"]); | ||
| var azureObj = state["Azure"]!.AsObject(); | ||
| Assert.NotNull(azureObj["Deployments"]); | ||
|
|
||
| // Check that all thread-specific nodes were created | ||
| for (int i = 0; i < threadCount; i++) | ||
| { | ||
| Assert.NotNull(azureObj[$"Thread{i}"]); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WithDeploymentState_Action_ExecutesSuccessfully() | ||
| { | ||
| // Arrange | ||
| var deploymentState = new JsonObject(); | ||
| var context = ProvisioningTestHelpers.CreateTestProvisioningContext(userSecrets: deploymentState); | ||
|
|
||
| // Act | ||
| var executed = false; | ||
| context.WithDeploymentState(state => | ||
| { | ||
| state["TestKey"] = "TestValue"; | ||
| executed = true; | ||
| }); | ||
|
|
||
| // Assert | ||
| Assert.True(executed); | ||
| Assert.Equal("TestValue", deploymentState["TestKey"]!.GetValue<string>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WithDeploymentState_Func_ReturnsValue() | ||
| { | ||
| // Arrange | ||
| var deploymentState = new JsonObject(); | ||
| deploymentState["TestKey"] = "TestValue"; | ||
| var context = ProvisioningTestHelpers.CreateTestProvisioningContext(userSecrets: deploymentState); | ||
|
|
||
| // Act | ||
| var result = context.WithDeploymentState(state => | ||
| { | ||
| return state["TestKey"]!.GetValue<string>(); | ||
| }); | ||
|
|
||
| // Assert | ||
| Assert.Equal("TestValue", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithDeploymentState_ConcurrentReadsAndWrites_MaintainsConsistency() | ||
| { | ||
| // Arrange | ||
| var deploymentState = new JsonObject(); | ||
| var context = ProvisioningTestHelpers.CreateTestProvisioningContext(userSecrets: deploymentState); | ||
| const int writerCount = 5; | ||
| const int readerCount = 5; | ||
| const int iterations = 100; | ||
|
|
||
| // Initialize counter | ||
| context.WithDeploymentState(state => | ||
| { | ||
| state["Counter"] = 0; | ||
| }); | ||
|
|
||
| var writerTasks = new Task[writerCount]; | ||
| var readerTasks = new Task[readerCount]; | ||
|
|
||
| // Act - Writers increment counter | ||
| for (int i = 0; i < writerCount; i++) | ||
| { | ||
| writerTasks[i] = Task.Run(() => | ||
| { | ||
| for (int j = 0; j < iterations; j++) | ||
| { | ||
| context.WithDeploymentState(state => | ||
| { | ||
| var current = state["Counter"]!.GetValue<int>(); | ||
| state["Counter"] = current + 1; | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Readers read counter | ||
| var readValues = new List<int>[readerCount]; | ||
| for (int i = 0; i < readerCount; i++) | ||
| { | ||
| int readerIndex = i; | ||
| readValues[readerIndex] = new List<int>(); | ||
| readerTasks[i] = Task.Run(() => | ||
| { | ||
| for (int j = 0; j < iterations; j++) | ||
| { | ||
| var value = context.WithDeploymentState(state => | ||
| { | ||
| return state["Counter"]!.GetValue<int>(); | ||
| }); | ||
| readValues[readerIndex].Add(value); | ||
| Thread.Sleep(1); // Small delay to allow interleaving | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| await Task.WhenAll(writerTasks.Concat(readerTasks)).WaitAsync(TimeSpan.FromSeconds(15)); | ||
|
|
||
| // Assert - Final counter value should be exactly writerCount * iterations | ||
| var finalValue = context.WithDeploymentState(state => | ||
| { | ||
| return state["Counter"]!.GetValue<int>(); | ||
| }); | ||
|
|
||
| Assert.Equal(writerCount * iterations, finalValue); | ||
|
|
||
| // All read values should be in valid range (0 to finalValue) | ||
| foreach (var readerValues in readValues) | ||
| { | ||
| Assert.All(readerValues, value => | ||
| { | ||
| Assert.InRange(value, 0, finalValue); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class ProvisioningServicesTests | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot undo these changes since the core issue is fixed in WithDeploymentState
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted JsonExtensions.cs to its original implementation. The thread-safety is now solely handled by WithDeploymentState at the ProvisioningContext level. Commit: (see latest)