From 42d6fdc80d8933ced5ff4ae858f43a3581106c75 Mon Sep 17 00:00:00 2001 From: Pete Hayes Date: Wed, 10 Jun 2026 23:24:30 +1000 Subject: [PATCH] Exclude feature tokio/net for WASM builds (#3784) --- axum/Cargo.toml | 8 ++++++-- axum/src/serve/listener.rs | 11 ++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index ed430d2e..54e01bf5 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -72,7 +72,6 @@ query = [ tokio = [ "dep:hyper-util", "dep:tokio", - "tokio/net", "tokio/rt", "tower/make", "tokio/macros", @@ -102,6 +101,12 @@ __private_docs = [ # in `axum-core` and `axum-extra`. __private = ["tokio", "http1", "dep:reqwest"] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tokio = { package = "tokio", version = "1.44", features = ["time", "net"], optional = true } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +tokio = { package = "tokio", version = "1.44", features = ["time"], optional = true } + [dependencies] axum-core = { path = "../axum-core", version = "0.5.6" } bytes = "1.7" @@ -135,7 +140,6 @@ serde_html_form = { version = "0.4.0", optional = true, default-features = false serde_json = { version = "1.0.29", features = ["raw_value"], optional = true } serde_path_to_error = { version = "0.1.8", optional = true } sha1 = { version = "0.10", optional = true } -tokio = { package = "tokio", version = "1.44", features = ["time"], optional = true } tokio-tungstenite = { version = "0.29.0", optional = true } tracing = { version = "0.1", default-features = false, optional = true } diff --git a/axum/src/serve/listener.rs b/axum/src/serve/listener.rs index 559ffbd6..bebe8948 100644 --- a/axum/src/serve/listener.rs +++ b/axum/src/serve/listener.rs @@ -4,13 +4,11 @@ use std::{ pin::Pin, sync::Arc, task::{Context, Poll}, - time::Duration, }; use pin_project_lite::pin_project; use tokio::{ io::{self, AsyncRead, AsyncWrite}, - net::{TcpListener, TcpStream}, sync::{OwnedSemaphorePermit, Semaphore}, }; @@ -32,8 +30,9 @@ pub trait Listener: Send + 'static { fn local_addr(&self) -> io::Result; } -impl Listener for TcpListener { - type Io = TcpStream; +#[cfg(not(target_arch = "wasm32"))] +impl Listener for tokio::net::TcpListener { + type Io = tokio::net::TcpStream; type Addr = std::net::SocketAddr; async fn accept(&mut self) -> (Self::Io, Self::Addr) { @@ -242,6 +241,7 @@ where } } +#[cfg(not(target_arch = "wasm32"))] async fn handle_accept_error(e: io::Error) { if is_connection_error(&e) { return; @@ -259,9 +259,10 @@ async fn handle_accept_error(e: io::Error) { // // hyper allowed customizing this but axum does not. error!("accept error: {e}"); - tokio::time::sleep(Duration::from_secs(1)).await; + tokio::time::sleep(std::time::Duration::from_secs(1)).await; } +#[cfg(not(target_arch = "wasm32"))] fn is_connection_error(e: &io::Error) -> bool { matches!( e.kind(),