Skip to content

Commit b60f13c

Browse files
Fix race condition in BatchExportProcessorAsync constructor
1 parent 02d1dc3 commit b60f13c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

next-gen/src/OpenTelemetry.AutoInstrumentation.Sdk/BatchExportProcessorAsync.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ protected BatchExportProcessorAsync(
3535
IExporterAsync<TBatchWriter> exporter,
3636
BatchExportProcessorOptions options)
3737
{
38+
// Validate all parameters first, before initializing anything
3839
ArgumentNullException.ThrowIfNull(options);
39-
4040
_Logger = logger ?? throw new ArgumentNullException(nameof(logger));
4141
_Exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
4242

@@ -46,6 +46,7 @@ protected BatchExportProcessorAsync(
4646
_ExportIntervalMilliseconds = options.ExportIntervalMilliseconds;
4747
_ExportTimeoutMilliseconds = options.ExportTimeoutMilliseconds;
4848

49+
// Only start the thread after all validation and initialization is complete
4950
_ExporterThread = new Thread(ExporterProc)
5051
{
5152
IsBackground = true,
@@ -196,7 +197,14 @@ private void ExporterProc(object? state)
196197
ExportAsync().ContinueWith(
197198
static (t, o) =>
198199
{
199-
((EventWaitHandle)o!).Set();
200+
try
201+
{
202+
((EventWaitHandle)o!).Set();
203+
}
204+
catch (ObjectDisposedException)
205+
{
206+
// EventWaitHandle was disposed during shutdown, nothing to signal
207+
}
200208
},
201209
_ExportAsyncTaskCompleteTrigger,
202210
CancellationToken.None,
@@ -205,6 +213,11 @@ private void ExporterProc(object? state)
205213

206214
_ExportAsyncTaskCompleteTrigger.WaitOne();
207215
}
216+
catch (ObjectDisposedException)
217+
{
218+
// The processor is being disposed, exit the worker thread
219+
return;
220+
}
208221
finally
209222
{
210223
_BufferedBatch.Reset();

0 commit comments

Comments
 (0)