Skip to content

Commit 2eba546

Browse files
authored
Fix shutdown of single threaded trace batch provider (#1964)
1 parent 383c7d1 commit 2eba546

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

opentelemetry-sdk/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## vNext
44

55
- `opentelemetry_sdk::logs::record::LogRecord` and `opentelemetry_sdk::logs::record::TraceContext` derive from `PartialEq` to facilitate Unit Testing.
6+
- Fixed an issue causing a panic during shutdown when using the `TokioCurrentThread` tracing batch processor.
67

78
## v0.24.1
89

opentelemetry-sdk/src/trace/span_processor.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,28 @@ impl<R: RuntimeChannel> BatchSpanProcessor<R> {
449449
pub(crate) fn new(exporter: Box<dyn SpanExporter>, config: BatchConfig, runtime: R) -> Self {
450450
let (message_sender, message_receiver) =
451451
runtime.batch_message_channel(config.max_queue_size);
452-
let ticker = runtime
453-
.interval(config.scheduled_delay)
454-
.map(|_| BatchMessage::Flush(None));
455-
let timeout_runtime = runtime.clone();
456-
457-
let messages = Box::pin(stream::select(message_receiver, ticker));
458-
let processor = BatchSpanProcessorInternal {
459-
spans: Vec::new(),
460-
export_tasks: FuturesUnordered::new(),
461-
runtime: timeout_runtime,
462-
config,
463-
exporter,
464-
};
465452

453+
let inner_runtime = runtime.clone();
466454
// Spawn worker process via user-defined spawn function.
467-
runtime.spawn(Box::pin(processor.run(messages)));
455+
runtime.spawn(Box::pin(async move {
456+
// Timer will take a reference to the current runtime, so its important we do this within the
457+
// runtime.spawn()
458+
let ticker = inner_runtime
459+
.interval(config.scheduled_delay)
460+
.map(|_| BatchMessage::Flush(None));
461+
let timeout_runtime = inner_runtime.clone();
462+
463+
let messages = Box::pin(stream::select(message_receiver, ticker));
464+
let processor = BatchSpanProcessorInternal {
465+
spans: Vec::new(),
466+
export_tasks: FuturesUnordered::new(),
467+
runtime: timeout_runtime,
468+
config,
469+
exporter,
470+
};
471+
472+
processor.run(messages).await
473+
}));
468474

469475
// Return batch processor with link to worker
470476
BatchSpanProcessor { message_sender }

0 commit comments

Comments
 (0)