From 865b259b95fb3c41ce09eb425b0487c48a311088 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jun 2024 20:47:28 +0200 Subject: [PATCH] fix: prometheus graceful shutdown (#8784) --- .../src/metrics/prometheus_exporter.rs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/node-core/src/metrics/prometheus_exporter.rs b/crates/node-core/src/metrics/prometheus_exporter.rs index 1ce85346fb0e..b7a3ba7015c3 100644 --- a/crates/node-core/src/metrics/prometheus_exporter.rs +++ b/crates/node-core/src/metrics/prometheus_exporter.rs @@ -2,6 +2,7 @@ use crate::metrics::version_metrics::register_version_metrics; use eyre::WrapErr; +use futures::{future::FusedFuture, FutureExt}; use http::Response; use metrics::describe_gauge; use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; @@ -65,17 +66,14 @@ async fn start_endpoint( tokio::net::TcpListener::bind(listen_addr).await.wrap_err("Could not bind to address")?; task_executor.spawn_with_graceful_shutdown_signal(|signal| async move { - let mut shutdown = signal.ignore_guard(); + let mut shutdown = signal.ignore_guard().fuse(); loop { - let io = tokio::select! { - res = listener.accept() => match res { - Ok((stream, _remote_addr)) => stream, - Err(err) => { - tracing::error!(%err, "failed to accept connection"); - continue; - } - }, - _ = &mut shutdown => break, + let io = match listener.accept().await { + Ok((stream, _remote_addr)) => stream, + Err(err) => { + tracing::error!(%err, "failed to accept connection"); + continue; + } }; let handle = handle.clone(); @@ -89,7 +87,11 @@ async fn start_endpoint( if let Err(error) = jsonrpsee::server::serve_with_graceful_shutdown(io, service, &mut shutdown).await { - tracing::error!(%error, "metrics endpoint crashed") + tracing::debug!(%error, "failed to serve request") + } + + if shutdown.is_terminated() { + break; } } });