-
Notifications
You must be signed in to change notification settings - Fork 889
[OpenTelemetry] Fix SemaphoreSlim leak #7069
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
Merged
Kielek
merged 3 commits into
open-telemetry:main
from
martincostello:batch-exporter-wasm-fix
Apr 14, 2026
+145
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry.Metrics; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Internal.Tests; | ||
|
|
||
| public class TaskWorkerTests | ||
| { | ||
| private const int BaselineCollectCount = 2; | ||
| private const int CircularBufferCapacity = 8; | ||
| private const int IdleCyclesBeforeTrigger = 2; | ||
| private const int MaxExportBatchSize = 4; | ||
| private const int PostBaselinePauseMilliseconds = 20; | ||
| private const int TriggerCompletionTimeoutMilliseconds = 100; | ||
| private const int WaitUntilPollingIntervalMilliseconds = 10; | ||
| private const int WorkerDelayMilliseconds = 250; | ||
| private const int WorkerTimeoutMilliseconds = 1000; | ||
|
|
||
| [Fact] | ||
| public async Task BatchExportTaskWorker_TriggerExportAfterIdleCycles_DoesNotWaitForScheduledDelay() | ||
| { | ||
| // Arrange | ||
| var circularBuffer = new CircularBuffer<Activity>(capacity: CircularBufferCapacity); | ||
| using var exporter = new TestActivityExporter(); | ||
| using var worker = new BatchExportTaskWorker<Activity>( | ||
| circularBuffer, | ||
| exporter, | ||
| maxExportBatchSize: MaxExportBatchSize, | ||
| scheduledDelayMilliseconds: WorkerDelayMilliseconds, | ||
| exporterTimeoutMilliseconds: WorkerTimeoutMilliseconds); | ||
|
|
||
| worker.Start(); | ||
|
|
||
| await Task.Delay(GetIdleWaitDuration()); | ||
|
|
||
| using var activity = new Activity("test"); | ||
|
|
||
| Assert.True(circularBuffer.Add(activity)); | ||
| Assert.True(worker.TriggerExport()); | ||
|
|
||
| // Act | ||
| await WaitUntilAsync(() => exporter.ExportCount >= 1, TriggerCompletionTimeoutMilliseconds); | ||
|
|
||
| // Assert | ||
| Assert.True(worker.Shutdown(WorkerTimeoutMilliseconds)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task PeriodicExportingMetricReaderTaskWorker_TriggerExportAfterIdleCycles_DoesNotWaitForExportInterval() | ||
| { | ||
| // Arrange | ||
| using var reader = new TestMetricReader(); | ||
| using var worker = new PeriodicExportingMetricReaderTaskWorker( | ||
| reader, | ||
| exportIntervalMilliseconds: WorkerDelayMilliseconds, | ||
| exportTimeoutMilliseconds: WorkerTimeoutMilliseconds); | ||
|
|
||
| worker.Start(); | ||
|
|
||
| await WaitUntilAsync(() => reader.CollectCount >= BaselineCollectCount, WorkerTimeoutMilliseconds); | ||
|
|
||
| var baselineCollectCount = reader.CollectCount; | ||
|
|
||
| await Task.Delay(PostBaselinePauseMilliseconds); | ||
|
|
||
| Assert.True(worker.TriggerExport()); | ||
|
|
||
| // Act | ||
| await WaitUntilAsync(() => reader.CollectCount >= baselineCollectCount + 1, TriggerCompletionTimeoutMilliseconds); | ||
|
|
||
| // Assert | ||
| Assert.True(worker.Shutdown(WorkerTimeoutMilliseconds)); | ||
| } | ||
|
|
||
| private static async Task WaitUntilAsync(Func<bool> condition, int timeoutMilliseconds) | ||
| { | ||
| var sw = Stopwatch.StartNew(); | ||
|
|
||
| while (sw.ElapsedMilliseconds < timeoutMilliseconds) | ||
| { | ||
| if (condition()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| await Task.Delay(WaitUntilPollingIntervalMilliseconds); | ||
| } | ||
|
|
||
| Assert.True(condition()); | ||
| } | ||
|
|
||
| private static int GetIdleWaitDuration() | ||
| => (IdleCyclesBeforeTrigger * WorkerDelayMilliseconds) + (WorkerDelayMilliseconds / 5); | ||
|
|
||
| private sealed class TestActivityExporter : BaseExporter<Activity> | ||
| { | ||
| private int exportCount; | ||
|
|
||
| public int ExportCount => this.exportCount; | ||
|
|
||
| public override ExportResult Export(in Batch<Activity> batch) | ||
| { | ||
| Interlocked.Increment(ref this.exportCount); | ||
| return ExportResult.Success; | ||
| } | ||
| } | ||
|
|
||
| #pragma warning disable CA2000 // BaseExportingMetricReader owns the exporter lifecycle | ||
| private sealed class TestMetricReader() : BaseExportingMetricReader(new TestMetricExporter()) | ||
| #pragma warning restore CA2000 | ||
| { | ||
| private int collectCount; | ||
|
|
||
| public int CollectCount => this.collectCount; | ||
|
|
||
| protected override bool OnCollect(int timeoutMilliseconds) | ||
| { | ||
| Interlocked.Increment(ref this.collectCount); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private sealed class TestMetricExporter : BaseExporter<Metric> | ||
| { | ||
| public override ExportResult Export(in Batch<Metric> batch) => ExportResult.Success; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.