diff --git a/README.md b/README.md index 830efafb5..8e4fa0564 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Make sure you activated the full features of the tokio crate on Cargo.toml: ```toml [dependencies] -tokio = { version = "1.45.0", features = ["full"] } +tokio = { version = "1.45.1", features = ["full"] } ``` Then, on your main.rs: diff --git a/tokio/CHANGELOG.md b/tokio/CHANGELOG.md index 2d2f94ba0..27f84c867 100644 --- a/tokio/CHANGELOG.md +++ b/tokio/CHANGELOG.md @@ -1,3 +1,15 @@ +# 1.45.1 (May 24th, 2025) + +This fixes a regression on the wasm32-unknown-unknown target, where code that +previously did not panic due to calls to `Instant::now()` started failing. This +is due to the stabilization of the first time-based metric. + +### Fixed + +- Disable time-based metrics on wasm32-unknown-unknown ([#7322]) + +[#7322]: https://github.com/tokio-rs/tokio/pull/7322 + # 1.45.0 (May 5th, 2025) ### Added diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index fd7223445..e4be60d0e 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -6,7 +6,7 @@ name = "tokio" # - README.md # - Update CHANGELOG.md. # - Create "v1.x.y" git tag. -version = "1.45.0" +version = "1.45.1" edition = "2021" rust-version = "1.70" authors = ["Tokio Contributors "] diff --git a/tokio/README.md b/tokio/README.md index 830efafb5..8e4fa0564 100644 --- a/tokio/README.md +++ b/tokio/README.md @@ -56,7 +56,7 @@ Make sure you activated the full features of the tokio crate on Cargo.toml: ```toml [dependencies] -tokio = { version = "1.45.0", features = ["full"] } +tokio = { version = "1.45.1", features = ["full"] } ``` Then, on your main.rs: diff --git a/tokio/src/runtime/metrics/batch.rs b/tokio/src/runtime/metrics/batch.rs index 00f9c9898..fe2f4a9da 100644 --- a/tokio/src/runtime/metrics/batch.rs +++ b/tokio/src/runtime/metrics/batch.rs @@ -12,7 +12,7 @@ pub(crate) struct MetricsBatch { busy_duration_total: u64, /// Instant at which work last resumed (continued after park). - processing_scheduled_tasks_started_at: Instant, + processing_scheduled_tasks_started_at: Option, /// Number of times the worker parked. park_count: u64, @@ -67,17 +67,17 @@ cfg_unstable_metrics! { impl MetricsBatch { pub(crate) fn new(worker_metrics: &WorkerMetrics) -> MetricsBatch { - let now = Instant::now(); - Self::new_unstable(worker_metrics, now) + let maybe_now = now(); + Self::new_unstable(worker_metrics, maybe_now) } cfg_metrics_variant! { stable: { #[inline(always)] - fn new_unstable(_worker_metrics: &WorkerMetrics, now: Instant) -> MetricsBatch { + fn new_unstable(_worker_metrics: &WorkerMetrics, maybe_now: Option) -> MetricsBatch { MetricsBatch { busy_duration_total: 0, - processing_scheduled_tasks_started_at: now, + processing_scheduled_tasks_started_at: maybe_now, park_count: 0, park_unpark_count: 0, } @@ -85,7 +85,16 @@ impl MetricsBatch { }, unstable: { #[inline(always)] - fn new_unstable(worker_metrics: &WorkerMetrics, now: Instant) -> MetricsBatch { + fn new_unstable(worker_metrics: &WorkerMetrics, maybe_now: Option) -> MetricsBatch { + let poll_timer = maybe_now.and_then(|now| { + worker_metrics + .poll_count_histogram + .as_ref() + .map(|worker_poll_counts| PollTimer { + poll_counts: HistogramBatch::from_histogram(worker_poll_counts), + poll_started_at: now, + }) + }); MetricsBatch { park_count: 0, park_unpark_count: 0, @@ -97,13 +106,8 @@ impl MetricsBatch { local_schedule_count: 0, overflow_count: 0, busy_duration_total: 0, - processing_scheduled_tasks_started_at: now, - poll_timer: worker_metrics.poll_count_histogram.as_ref().map( - |worker_poll_counts| PollTimer { - poll_counts: HistogramBatch::from_histogram(worker_poll_counts), - poll_started_at: now, - }, - ), + processing_scheduled_tasks_started_at: maybe_now, + poll_timer, } } } @@ -186,13 +190,17 @@ impl MetricsBatch { /// Start processing a batch of tasks pub(crate) fn start_processing_scheduled_tasks(&mut self) { - self.processing_scheduled_tasks_started_at = Instant::now(); + self.processing_scheduled_tasks_started_at = now(); } /// Stop processing a batch of tasks pub(crate) fn end_processing_scheduled_tasks(&mut self) { - let busy_duration = self.processing_scheduled_tasks_started_at.elapsed(); - self.busy_duration_total += duration_as_u64(busy_duration); + if let Some(processing_scheduled_tasks_started_at) = + self.processing_scheduled_tasks_started_at + { + let busy_duration = processing_scheduled_tasks_started_at.elapsed(); + self.busy_duration_total += duration_as_u64(busy_duration); + } } cfg_metrics_variant! { @@ -279,3 +287,17 @@ cfg_rt_multi_thread! { pub(crate) fn duration_as_u64(dur: Duration) -> u64 { u64::try_from(dur.as_nanos()).unwrap_or(u64::MAX) } + +/// Gate unsupported time metrics for `wasm32-unknown-unknown` +/// +fn now() -> Option { + if cfg!(all( + target_arch = "wasm32", + target_os = "unknown", + target_vendor = "unknown" + )) { + None + } else { + Some(Instant::now()) + } +}