Skip to content
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

Add locking mechanism for Subsegments in Entity.AddException method #307

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sdk/src/Core/Internal/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,13 @@ public void AddException(Exception e)
{
HasFault = true;
Cause = new Cause();
Cause.AddException(AWSXRayRecorder.Instance.ExceptionSerializationStrategy.DescribeException(e, Subsegments));
List<Subsegment> subsegmentsCopy;
lock (_lazySubsegments.Value)
{
subsegmentsCopy = _lazySubsegments.Value.ToList(); // Create a copy to avoid holding the lock during serialization
}

Cause.AddException(AWSXRayRecorder.Instance.ExceptionSerializationStrategy.DescribeException(e, subsegmentsCopy));
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions sdk/test/UnitTests/SegmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Threading.Tasks;
using Amazon.XRay.Recorder.Core.Exceptions;
using Amazon.XRay.Recorder.Core.Internal.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -300,6 +302,48 @@ public void TestAddException()
Assert.ReferenceEquals(e, descriptor.Exception);
}

[TestMethod]
public void TestAddExceptionWithConcurrentSubsegmentAddition()
{
const int EXCEPTION_ITERATIONS = 100;
const int SUBSEGMENT_ITERATIONS = 100_000;
var segment = new Segment("test", TraceId);
var raceConditionOccurred = false;
var cancellationTokenSource = new CancellationTokenSource();

var addExceptionTask = Task.Run(() =>
{
Parallel.ForEach(Enumerable.Range(0, EXCEPTION_ITERATIONS), _ =>
{
try
{
var exception = new Exception("Test Exception");
segment.AddException(exception);
}
catch (InvalidOperationException)
{
raceConditionOccurred = true;
}
});
});

var addSubsegmentsTask = Task.Run(() =>
{
try
{
Parallel.ForEach(Enumerable.Range(0, SUBSEGMENT_ITERATIONS), new ParallelOptions { CancellationToken = cancellationTokenSource.Token }, _ =>
{
segment.AddSubsegment(new Subsegment("TestSubsegment"));
});
}
catch (OperationCanceledException) { }
});

Task.WaitAll(addExceptionTask, addSubsegmentsTask);

Assert.IsFalse(raceConditionOccurred, "A race condition error occurred unexpectedly.");
}

[TestMethod]
public void TestHttpOverwriteValue()
{
Expand Down
Loading