Skip to content

Commit bb48945

Browse files
committed
fix: Fix stack-overflow inducing infinite feedback loop when calling tracing::event! after SdkLoggerProvider::shutdown
1 parent b70771a commit bb48945

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use opentelemetry_appender_tracing::layer;
2+
use opentelemetry_sdk::logs::SdkLoggerProvider;
3+
use tracing::info;
4+
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
5+
6+
#[test]
7+
fn test_no_stack_overflow_when_event_is_emitted_after_shutdown() {
8+
let exporter = opentelemetry_stdout::LogExporter::default();
9+
let provider: SdkLoggerProvider = SdkLoggerProvider::builder()
10+
.with_batch_exporter(exporter)
11+
.build();
12+
13+
let otel_layer = layer::OpenTelemetryTracingBridge::new(&provider);
14+
15+
tracing_subscriber::registry().with(otel_layer).init();
16+
17+
provider.shutdown().unwrap();
18+
19+
info!("Don't crash")
20+
}

opentelemetry-sdk/src/logs/batch_log_processor.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,20 @@ impl LogProcessor for BatchLogProcessor {
207207
}
208208
}
209209
Err(mpsc::TrySendError::Disconnected(_)) => {
210-
// Given background thread is the only receiver, and it's
211-
// disconnected, it indicates the thread is shutdown
212-
otel_warn!(
213-
name: "BatchLogProcessor.Emit.AfterShutdown",
214-
message = "Logs are being emitted even after Shutdown. This indicates incorrect lifecycle management of OTelLoggerProvider in application. Logs will not be exported."
215-
);
210+
// Re-emitting the following warning here may lead to a stack overflow due to the
211+
// warning produced by this branch being reconsumed by this same branch, producing a
212+
// cycle. This may occur when integrating with the `tracing` crate due to
213+
// `otel_warn!` possibly calling `tracing::warn!` under the hood, which will emit
214+
// the warning back into the otel log processor.
215+
if matches!(record.event_name, Some(event_name) if event_name != Self::AFTER_SHUTDOWN_WARNING_NAME)
216+
{
217+
// Given background thread is the only receiver, and it's
218+
// disconnected, it indicates the thread is shutdown
219+
otel_warn!(
220+
name: BatchLogProcessor::AFTER_SHUTDOWN_WARNING_NAME,
221+
message = "Logs are being emitted even after Shutdown. This indicates incorrect lifecycle management of OTelLoggerProvider in application. Logs will not be exported."
222+
);
223+
};
216224
}
217225
}
218226
}
@@ -325,6 +333,8 @@ impl LogProcessor for BatchLogProcessor {
325333
}
326334

327335
impl BatchLogProcessor {
336+
const AFTER_SHUTDOWN_WARNING_NAME: &str = "BatchLogProcessor.Emit.AfterShutdown";
337+
328338
pub(crate) fn new<E>(mut exporter: E, config: BatchConfig) -> Self
329339
where
330340
E: LogExporter + Send + Sync + 'static,

0 commit comments

Comments
 (0)