Skip to content

Commit d01884e

Browse files
committed
chore: Add processor dispose validation test
1 parent d0282c6 commit d01884e

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/OpenTelemetry/Internal/BatchExportTaskWorker.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ await Task.WhenAny(
180180
this.shutdownCompletionSource.Task,
181181
Task.Delay(timeout, combinedTokenSource.Token)).ConfigureAwait(false);
182182
}
183+
catch (ObjectDisposedException)
184+
{
185+
return false; // The worker has been disposed
186+
}
183187
catch (OperationCanceledException)
184188
{
185189
// Expected when timeout or shutdown occurs
@@ -216,13 +220,13 @@ await Task.WhenAny(
216220
}
217221
catch (OperationCanceledException)
218222
{
219-
if (this.isShutdownRequested)
220-
{
221-
break;
222-
}
223-
224223
// Continue to check if there's data to export before exiting
225224
}
225+
catch (ObjectDisposedException)
226+
{
227+
// the exporter is somehow disposed before the worker thread could finish its job
228+
return;
229+
}
226230
}
227231

228232
this.PerformExport();

test/OpenTelemetry.Tests/Logs/BatchLogRecordExportProcessorTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,44 @@ public void LogRecordAddedToBatchIfNotFromAnyPoolTest()
154154
Assert.Single(exportedItems);
155155
Assert.Same(logRecord, exportedItems[0]);
156156
}
157+
158+
[Theory]
159+
[InlineData(true)]
160+
[InlineData(false)]
161+
public void DisposeWithoutShutdown(bool useThread)
162+
{
163+
var scopeProvider = new LoggerExternalScopeProvider();
164+
165+
List<LogRecord> exportedItems = new();
166+
167+
var processor = new BatchLogRecordExportProcessor(
168+
#pragma warning disable CA2000 // Dispose objects before losing scope
169+
new InMemoryExporter<LogRecord>(exportedItems),
170+
#pragma warning restore CA2000 // Dispose objects before losing scope
171+
useThreads: useThread,
172+
maxQueueSize: BatchLogRecordExportProcessor.DefaultMaxQueueSize,
173+
maxExportBatchSize: BatchLogRecordExportProcessor.DefaultMaxExportBatchSize,
174+
exporterTimeoutMilliseconds: BatchLogRecordExportProcessor.DefaultExporterTimeoutMilliseconds,
175+
scheduledDelayMilliseconds: int.MaxValue);
176+
177+
processor.Dispose();
178+
179+
using var scope = scopeProvider.Push(exportedItems);
180+
181+
var pool = LogRecordSharedPool.Current;
182+
183+
var logRecord = pool.Rent();
184+
185+
var state = new LogRecordTests.DisposingState("Hello world");
186+
187+
logRecord.ILoggerData.ScopeProvider = scopeProvider;
188+
logRecord.StateValues = state;
189+
190+
processor.OnEnd(logRecord);
191+
192+
state.Dispose();
193+
194+
Assert.Empty(exportedItems);
195+
}
157196
}
158197
#endif

0 commit comments

Comments
 (0)