diff --git a/src/Agent/NewRelic/Agent/Core/AgentManager.cs b/src/Agent/NewRelic/Agent/Core/AgentManager.cs index c42363611d..bf1a49d138 100644 --- a/src/Agent/NewRelic/Agent/Core/AgentManager.cs +++ b/src/Agent/NewRelic/Agent/Core/AgentManager.cs @@ -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 { @@ -121,6 +122,8 @@ private AgentManager() AssertAgentEnabled(config); EventBus.Subscribe(OnShutdownAgent); + EventBus.Subscribe(OnAgentConnected); + EventBus.Subscribe(OnAgentReconnecting); //Initialize the extensions loader with extensions folder based on the the install path ExtensionsLoader.Initialize(AgentInstallConfiguration.InstallPathExtensionsDirectory); @@ -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) @@ -370,6 +373,7 @@ private void Shutdown(bool cleanShutdown) if (cleanShutdown) { + Log.Debug("Agent is connected, executing a clean shutdown."); EventBus.Publish(new PreCleanShutdownEvent()); EventBus.Publish(new CleanShutdownEvent()); } @@ -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 } } diff --git a/tests/Agent/IntegrationTests/Applications/ApiAppNameChange/Program.cs b/tests/Agent/IntegrationTests/Applications/ApiAppNameChange/Program.cs index b5eab8d0f9..1e5034411f 100644 --- a/tests/Agent/IntegrationTests/Applications/ApiAppNameChange/Program.cs +++ b/tests/Agent/IntegrationTests/Applications/ApiAppNameChange/Program.cs @@ -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 @@ -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)); } diff --git a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/MultiFunctionApplicationHelpers.csproj b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/MultiFunctionApplicationHelpers.csproj index 62c8d18153..0cd536719b 100644 --- a/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/MultiFunctionApplicationHelpers.csproj +++ b/tests/Agent/IntegrationTests/SharedApplications/Common/MultiFunctionApplicationHelpers/MultiFunctionApplicationHelpers.csproj @@ -6,6 +6,7 @@ true true default + false