Skip to content

Commit

Permalink
Make the SDK more forgiving for ObjectDisposedException (open-telemet…
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored May 19, 2022
1 parent a3e619f commit fd63991
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/OpenTelemetry/BatchExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ protected override void OnExport(T data)
{
if (this.circularBuffer.Count >= this.maxExportBatchSize)
{
this.exportTrigger.Set();
try
{
this.exportTrigger.Set();
}
catch (ObjectDisposedException)
{
}
}

return; // enqueue succeeded
Expand All @@ -121,7 +127,14 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
return true; // nothing to flush
}

this.exportTrigger.Set();
try
{
this.exportTrigger.Set();
}
catch (ObjectDisposedException)
{
return false;
}

if (timeoutMilliseconds == 0)
{
Expand Down Expand Up @@ -186,7 +199,15 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
protected override bool OnShutdown(int timeoutMilliseconds)
{
this.shutdownDrainTarget = this.circularBuffer.AddedCount;
this.shutdownTrigger.Set();

try
{
this.shutdownTrigger.Set();
}
catch (ObjectDisposedException)
{
return false;
}

OpenTelemetrySdkEventSource.Log.DroppedExportProcessorItems(this.GetType().Name, this.exporter.GetType().Name, this.droppedCount);

Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

* Fix null reference exception when a metric view does not match an instrument.
([#3285](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3285))
* Swallow `ObjectDisposedException` in `BatchExportProcessor` and
* `PeriodicExportingMetricReader`.
([#3291](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3291))

## 1.3.0-beta.2

Expand Down
9 changes: 8 additions & 1 deletion src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ protected override bool OnShutdown(int timeoutMilliseconds)
{
var result = true;

this.shutdownTrigger.Set();
try
{
this.shutdownTrigger.Set();
}
catch (ObjectDisposedException)
{
return false;
}

if (timeoutMilliseconds == Timeout.Infinite)
{
Expand Down

0 comments on commit fd63991

Please sign in to comment.