Agent.Tracer.CaptureException throws NullReferenceException if Agent is disabled

Kibana version: N/A

Elasticsearch version: N/A

APM Server version: 7.15.0

APM Agent language and version: c# .Net Core 3.1, v1.11.1

Browser version: N/A

Original install method (e.g. download page, yum, deb, from source, etc.) and version: Nuget package

Description of the problem including expected versus actual behavior. Please include screenshots (if relevant):

Hi,

if Agent.Tracer.CaptureException(exception) gets called and the agent configuration 'enabled' is set to false, the method throws a System.NullReferenceException:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Elastic.Apm.BackendComm.BackendCommComponentBase.ThrowIfDisposed()
   at Elastic.Apm.Report.PayloadSenderV2.EnqueueEvent(Object eventObj, String dbgEventKind)
   at Elastic.Apm.Report.PayloadSenderV2.QueueError(IError error)
   at Elastic.Apm.Model.ExecutionSegmentCommon.CaptureException(Exception exception, IApmLogger logger, IPayloadSender payloadSender, IExecutionSegment executionSegment, IConfigurationReader configurationReader, Transaction transaction, IApmServerInfo apmServerInfo, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at Elastic.Apm.Api.Tracer.CaptureException(Exception exception, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at ConsoleApp1.Program.Main(String[] args) in .\ConsoleApp1\ConsoleApp1\Program.cs:line 13

it looks like 'BackendCommComponentBase' aborts initialization of the '_disposableHelper' if 'enabled' is set to false.

// BackendCommComponentBase.cs Line 39
if (!_isEnabled)
{
    _logger.Debug()?.Log("Disabled - exiting without initializing any members used by work loop");
    return;
}

CancellationTokenSource = new CancellationTokenSource();

_disposableHelper = new DisposableHelper();

However, later if Agent.Tracer.CaptureException gets called 'PayloadSenderV2.EnqueueEvent' calls 'ThrowIfDisposed()' which checks '_disposableHelper.HasStarted' and '_disposableHelper' is null at this point because it was not initialized.

// BackendCommComponentBase.cs Line 131
protected void ThrowIfDisposed()
{
    if (_disposableHelper.HasStarted) throw new ObjectDisposedException( /* objectName: */ _dbgName);
}

Steps to reproduce:

  1. Create .Net Core 3.1 console app
  2. Install Nuget 'Elastic.Apm.AspNetCore'
  3. Copy code:
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    
        Environment.SetEnvironmentVariable("ELASTIC_APM_ENABLED", "false");
        Agent.Tracer.CaptureException(new Exception());
    }
    

Provide logs and/or server output (if relevant):

System.NullReferenceException: Object reference not set to an instance of an object.
   at Elastic.Apm.BackendComm.BackendCommComponentBase.ThrowIfDisposed()
   at Elastic.Apm.Report.PayloadSenderV2.EnqueueEvent(Object eventObj, String dbgEventKind)
   at Elastic.Apm.Report.PayloadSenderV2.QueueError(IError error)
   at Elastic.Apm.Model.ExecutionSegmentCommon.CaptureException(Exception exception, IApmLogger logger, IPayloadSender payloadSender, IExecutionSegment executionSegment, IConfigurationReader configurationReader, Transaction transaction, IApmServerInfo apmServerInfo, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at Elastic.Apm.Api.Tracer.CaptureException(Exception exception, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at ConsoleApp1.Program.Main(String[] args) in .\ConsoleApp1\ConsoleApp1\Program.cs:line 13

Thanks for reporting this @lken0815 - also thanks for the nice and small reproducer, it was super helpful.

I opened a PR to fix this: Handle enabled/recording=false on Tracer.CaptureError by gregkalapos · Pull Request #1557 · elastic/apm-agent-dotnet · GitHub

1 Like

Version 1.12.0 fixes the issue.

Thanks for your help! :+1:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.