Skip to content

Commit

Permalink
fix: Fix a segfault when using sendDataOnExit with Linux on Docker.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffinito committed Oct 30, 2023
1 parent 4b75587 commit e3fa584
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Agent/NewRelic/Agent/Core/AgentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sealed public class AgentManager : IAgentManager, IDisposable
private readonly ConfigurationSubscriber _configurationSubscription = new ConfigurationSubscriber();
private readonly static IAgentManager DisabledAgentManager = new DisabledAgentManager();
private readonly static AgentSingleton Singleton = new AgentSingleton();
private bool _connected = false;

private sealed class AgentSingleton : Singleton<IAgentManager>
{
Expand Down Expand Up @@ -121,6 +122,8 @@ private AgentManager()
AssertAgentEnabled(config);

EventBus<KillAgentEvent>.Subscribe(OnShutdownAgent);
EventBus<AgentConnectedEvent>.Subscribe(OnAgentConnected);
EventBus<StopHarvestEvent>.Subscribe(OnAgentReconnecting);

//Initialize the extensions loader with extensions folder based on the the install path
ExtensionsLoader.Initialize(AgentInstallConfiguration.InstallPathExtensionsDirectory);
Expand Down Expand Up @@ -349,8 +352,8 @@ public ITracer GetTracerImpl(string tracerFactoryName, uint tracerArguments, str
private void ProcessExit(object sender, EventArgs e)
{
Log.Debug("Received a ProcessExit CLR event for the application domain. About to shut down the .NET Agent...");
Shutdown(true);

Shutdown(_connected);
}

private void Shutdown(bool cleanShutdown)
Expand All @@ -370,6 +373,7 @@ private void Shutdown(bool cleanShutdown)

if (cleanShutdown)
{
Log.Debug("Agent is connected, executing a clean shutdown.");
EventBus<PreCleanShutdownEvent>.Publish(new PreCleanShutdownEvent());
EventBus<CleanShutdownEvent>.Publish(new CleanShutdownEvent());
}
Expand Down Expand Up @@ -408,6 +412,20 @@ private void OnShutdownAgent(KillAgentEvent eventData)
if (_isInitialized) Shutdown(false);
}

// Automatically triggered once the agent is fully connected.
private void OnAgentConnected(AgentConnectedEvent _)
{
_connected = true;
}

// This event is only triggered during a reconnect.
// We want to mark the agent as disconnected until the reconnect (which is just a Connect) is completed.
// Once the reconnect(Connect) is done, it will automatically trigger AgentConnectedEvent (OnAgentConnected) above.
private void OnAgentReconnecting(StopHarvestEvent _)
{
_connected = false;
}

#endregion Event handlers
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;

namespace NewRelic.Agent.IntegrationTests.Applications.ApiAppNameChange
Expand Down Expand Up @@ -37,6 +38,9 @@ static void Main(string[] args)
Api.Agent.NewRelic.StartAgent();
Api.Agent.NewRelic.SetApplicationName("AgentApi2");

// We need to wait for the reconnect to occur before the new name is picked up.
Task.Delay(30 * 1000).Wait();

// Wait for the test harness to tell us to shut down
eventWaitHandle.WaitOne(TimeSpan.FromMinutes(5));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<LangVersion>default</LangVersion>
<NuGetAudit>false</NuGetAudit>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e3fa584

Please sign in to comment.