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

Revert "ProcessExit event handler improvements" #1410

Merged
merged 1 commit into from
Apr 16, 2021
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
106 changes: 3 additions & 103 deletions src/Datadog.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,39 +712,7 @@ private void InitializeLibLogScopeEventSubscriber(IScopeManager scopeManager, st

private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
// This handles a graceful shutdown or a SIGTERM
// Multiple delegates can be registered to this event. (eg. IHostApplicationLifetime instances)
// So we must be sure we are the last handler of the event to ensure we are flushing all the
// possible traces we are generating.

// For this, we try to get the internal ProcessExit delegate from the event.
var processExitDelegate = DelegatesHelper.GetInternalProcessExitDelegate();
if (processExitDelegate != null)
{
// With the internal MulticastDelegate we first create the delegate instance
// we want to run at last of the event handler.

// Sets the final delegate to be executed by the ProcessExit event.
var lastDelegate = new EventHandler((s, earg) =>
{
RunShutdownTasks();
});

// Try to modify the MulticastDelegate invocation list and set the last delegate.
if (!DelegatesHelper.TrySetLastDelegate(processExitDelegate, lastDelegate))
{
// This means we are in the last delegate already
// or we were unable to set the last delegate, in
// any case we fallback by running the delegate now.
RunShutdownTasks();
}
}
else
{
// If we were unable to extract the internal delegate of the event then
// we just fallback and run the code here.
RunShutdownTasks();
}
RunShutdownTasks();
}

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
Expand All @@ -755,69 +723,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE

private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
// Console Cancel KeyPress is raised when CTRL+C or CTRL+BREAK are pressed in a console windows
// On Unix: is registered using SIGINT and SIGQUIT signals.
// On Windows: is registered with Kernel32.SetConsoleCtrlHandler.
// ...
// If ConsoleCancelEventArgs.Cancel is false then the Process will be exit without calling ProcessExit event. So we shuld flush and close.
// If ConsoleCancelEventArgs.Cancel is true the process will not exit so we will only flush.
// Because the Cancel property can be modified by the application handlers we should listen at the end of the queue.

// For this, we try to get the internal CancelKeyPress delegate from the event.
var cancelKeyPressDelegate = DelegatesHelper.GetInternalCancelKeyPressDelegate();
if (cancelKeyPressDelegate != null)
{
// With the internal MulticastDelegate we first create the delegate instance
// we want to run at last of the event handler.

// Sets the final delegate to be executed by the CancelKeyPress event.
var lastDelegate = new ConsoleCancelEventHandler(HandleCancelKeyPress);

// Try to modify the MulticastDelegate invocation list and set the last delegate.
if (!DelegatesHelper.TrySetLastDelegate(cancelKeyPressDelegate, lastDelegate))
{
// This means we are in the last delegate already
// or we were unable to set the last delegate, in
// any case we fallback by running the delegate now.
HandleCancelKeyPress(sender, e);
}
}
else
{
// If we were unable to extract the internal delegate of the event then
// we just fallback and run the code here.
HandleCancelKeyPress(sender, e);
}

void HandleCancelKeyPress(object s, ConsoleCancelEventArgs cEvtArgs)
{
if (cEvtArgs.Cancel)
{
FlushTraces();
}
else
{
RunShutdownTasks();
}
}

void FlushTraces()
{
SynchronizationContext context = SynchronizationContext.Current;
try
{
SynchronizationContext.SetSynchronizationContext(null);
_agentWriter.FlushTracesAsync().GetAwaiter().GetResult();
}
catch (Exception ex)
{
Log.Error(ex, "Error flushing traces on shutdown.");
}
finally
{
SynchronizationContext.SetSynchronizationContext(context);
}
}
RunShutdownTasks();
}

private void CurrentDomain_DomainUnload(object sender, EventArgs e)
Expand All @@ -827,20 +733,14 @@ private void CurrentDomain_DomainUnload(object sender, EventArgs e)

private void RunShutdownTasks()
{
SynchronizationContext context = SynchronizationContext.Current;
try
{
SynchronizationContext.SetSynchronizationContext(null);
_agentWriter.FlushAndCloseAsync().GetAwaiter().GetResult();
_agentWriter.FlushAndCloseAsync().Wait();
}
catch (Exception ex)
{
Log.Error(ex, "Error flushing traces on shutdown.");
}
finally
{
SynchronizationContext.SetSynchronizationContext(context);
}
}

private void HeartbeatCallback(object state)
Expand Down
129 changes: 0 additions & 129 deletions src/Datadog.Trace/Util/DelegatesHelper.cs

This file was deleted.

Loading