Exclude feature tokio/net for WASM builds (#3784)

This commit is contained in:
Pete Hayes
2026-06-10 15:24:30 +02:00
committed by GitHub
parent e1f5f5eaa7
commit 42d6fdc80d
2 changed files with 12 additions and 7 deletions
+6 -2
View File
@@ -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 }
+6 -5
View File
@@ -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<Self::Addr>;
}
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(),