Skip to content

Commit

Permalink
Address a few synchronization issues in the codebase (#2276)
Browse files Browse the repository at this point in the history
* ensure Add() is locked on composite disposable

* Ensure _dynamicMethods is readonly in ILHelpersExtensions

* fix synchronization issue in ModuleLookup

* _reflectionObject should not be static in BinaryConverter

* dotnet format
  • Loading branch information
Mpdreamz authored Feb 5, 2024
1 parent 88d3b05 commit aacb5cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/Elastic.Apm/ApmAgentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ public CompositeDisposable Add(IDisposable disposable)
if (_isDisposed)
throw new ObjectDisposedException(nameof(CompositeDisposable));

_disposables.Add(disposable);
return this;
lock (_lock)
{
if (_isDisposed)
throw new ObjectDisposedException(nameof(CompositeDisposable));

_disposables.Add(disposable);
return this;
}
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class BinaryConverter : JsonConverter
#if HAVE_LINQ
private const string BinaryTypeName = "System.Data.Linq.Binary";
private const string BinaryToArrayName = "ToArray";
private static ReflectionObject? _reflectionObject;
private ReflectionObject? _reflectionObject;
#endif

/// <summary>
Expand Down Expand Up @@ -89,7 +89,7 @@ private byte[] GetByteArray(object value)
}

#if HAVE_LINQ
private static void EnsureReflectionObject(Type t)
private void EnsureReflectionObject(Type t)
{
if (_reflectionObject == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Elastic.Apm.Profiler.Managed.DuckTyping
/// </summary>
internal static class ILHelpersExtensions
{
private static List<DynamicMethod> _dynamicMethods = new List<DynamicMethod>();
private static readonly List<DynamicMethod> _dynamicMethods = new();

internal static DynamicMethod GetDynamicMethodForIndex(int index)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ internal static class ModuleLookup
/// </summary>
private const int MaxFailures = 50;

private static ManualResetEventSlim _populationResetEvent = new ManualResetEventSlim(initialState: true);
private static ConcurrentDictionary<Guid, Module> _modules = new ConcurrentDictionary<Guid, Module>();
private static readonly ManualResetEventSlim _populationResetEvent = new(initialState: true);
private static readonly ConcurrentDictionary<Guid, Module> _modules = new();

private static int _failures;
private static bool _shortCircuitLogicHasLogged = false;
private static long _failures;

public static Module GetByPointer(long moduleVersionPointer) =>
Get(Marshal.PtrToStructure<Guid>(new IntPtr(moduleVersionPointer)));

private static readonly object _logLock = new();
private static bool _shortCircuitLogicHasLogged = false;

public static Module Get(Guid moduleVersionId)
{
// First attempt at cached values with no blocking
Expand All @@ -44,15 +46,20 @@ public static Module Get(Guid moduleVersionId)
if (_modules.TryGetValue(moduleVersionId, out value))
return value;

if (_failures >= MaxFailures)
var failures = Interlocked.Read(ref _failures);
if (failures >= MaxFailures)
{
// For some unforeseeable reason we have failed on a lot of AppDomain lookups
if (!_shortCircuitLogicHasLogged)
if (_shortCircuitLogicHasLogged)
return null;
lock (_logLock)
{
if (_shortCircuitLogicHasLogged)
return null;
Logger.Log(LogLevel.Warn,
"Elastic APM is unable to continue attempting module lookups for this AppDomain. Falling back to legacy method lookups.");
}
_shortCircuitLogicHasLogged = true;

}
return null;
}

Expand All @@ -65,7 +72,7 @@ public static Module Get(Guid moduleVersionId)
}
catch (Exception ex)
{
_failures++;
Interlocked.Increment(ref _failures);
Logger.Log(LogLevel.Error, ex, "Error when populating modules.");
}
finally
Expand Down

0 comments on commit aacb5cc

Please sign in to comment.