From 959ba27854aaae17821bd9e6fade56f2fb6b9b30 Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Tue, 23 Sep 2025 22:29:09 +0300 Subject: [PATCH 1/6] tokio-util: enable loom testing for cancellation token Fixes: #7271 --- .github/labeler.yml | 4 ++++ .github/workflows/loom.yml | 16 ++++++++++++++++ tokio-util/Cargo.toml | 3 +++ tokio-util/src/loom.rs | 10 +++++++++- tokio-util/src/net/mod.rs | 2 ++ tokio-util/src/sync/mod.rs | 3 +++ tokio-util/src/sync/tests/mod.rs | 3 ++- tokio-util/src/udp/mod.rs | 2 ++ tokio-util/tests/udp.rs | 1 + 9 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index d48b9fdd5d3..05fc74e714b 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -19,3 +19,7 @@ R-loom-multi-thread: - tokio/src/runtime/scheduler/multi_thread/** - tokio/src/runtime/task/* - tokio/src/runtime/task/** + +R-loom-util-sync: + - tokio-util/src/sync/* + - tokio-util/src/sync/**/* diff --git a/.github/workflows/loom.yml b/.github/workflows/loom.yml index 5efa0aca74b..f7f436fd5c3 100644 --- a/.github/workflows/loom.yml +++ b/.github/workflows/loom.yml @@ -95,3 +95,19 @@ jobs: working-directory: tokio env: SCOPE: ${{ matrix.scope }} + + loom-util-sync: + name: loom tokio-util::sync + # base_ref is null when it's not a pull request + if: github.repository_owner == 'tokio-rs' && (contains(github.event.pull_request.labels.*.name, 'R-loom-util-sync') || (github.base_ref == null)) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Install Rust ${{ env.rust_stable }} + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ env.rust_stable }} + - uses: Swatinem/rust-cache@v2 + - name: run tests + run: cargo test --lib --release --features full -- --nocapture sync::tests + working-directory: tokio-util diff --git a/tokio-util/Cargo.toml b/tokio-util/Cargo.toml index 29ea79ef726..ccfecd077f6 100644 --- a/tokio-util/Cargo.toml +++ b/tokio-util/Cargo.toml @@ -57,6 +57,9 @@ futures-test = "0.3.5" parking_lot = "0.12.0" tempfile = "3.1.0" +[target.'cfg(loom)'.dev-dependencies] +loom = { version = "0.7", features = ["futures", "checkpoint"] } + [package.metadata.docs.rs] all-features = true # enable unstable features in the documentation diff --git a/tokio-util/src/loom.rs b/tokio-util/src/loom.rs index dd03feaba1a..4b0a4274857 100644 --- a/tokio-util/src/loom.rs +++ b/tokio-util/src/loom.rs @@ -1 +1,9 @@ -pub(crate) use std::sync; +//! This module abstracts over `loom` and `std::sync` types depending on whether we +//! are running loom tests or not. + +pub(crate) mod sync { + #[cfg(loom)] + pub(crate) use loom::sync::{Arc, Mutex, MutexGuard}; + #[cfg(not(loom))] + pub(crate) use std::sync::{Arc, Mutex, MutexGuard}; +} diff --git a/tokio-util/src/net/mod.rs b/tokio-util/src/net/mod.rs index a548c1c4a41..d64b451e177 100644 --- a/tokio-util/src/net/mod.rs +++ b/tokio-util/src/net/mod.rs @@ -1,3 +1,5 @@ +#![cfg(not(loom))] + //! TCP/UDP/Unix helpers for tokio. use crate::either::Either; diff --git a/tokio-util/src/sync/mod.rs b/tokio-util/src/sync/mod.rs index c6c97471ec2..3bb767a13b8 100644 --- a/tokio-util/src/sync/mod.rs +++ b/tokio-util/src/sync/mod.rs @@ -15,3 +15,6 @@ pub use poll_semaphore::PollSemaphore; mod reusable_box; pub use reusable_box::ReusableBoxFuture; + +#[cfg(test)] +mod tests; diff --git a/tokio-util/src/sync/tests/mod.rs b/tokio-util/src/sync/tests/mod.rs index 8b137891791..4ed3fe2028b 100644 --- a/tokio-util/src/sync/tests/mod.rs +++ b/tokio-util/src/sync/tests/mod.rs @@ -1 +1,2 @@ - +#[cfg(loom)] +mod loom_cancellation_token; diff --git a/tokio-util/src/udp/mod.rs b/tokio-util/src/udp/mod.rs index f88ea030aa3..3ea2eeff2c4 100644 --- a/tokio-util/src/udp/mod.rs +++ b/tokio-util/src/udp/mod.rs @@ -1,3 +1,5 @@ +#![cfg(not(loom))] + //! UDP framing mod frame; diff --git a/tokio-util/tests/udp.rs b/tokio-util/tests/udp.rs index 6e31b3a5394..12c852fa75f 100644 --- a/tokio-util/tests/udp.rs +++ b/tokio-util/tests/udp.rs @@ -1,6 +1,7 @@ #![warn(rust_2018_idioms)] #![cfg(not(target_os = "wasi"))] // Wasi doesn't support UDP #![cfg(not(miri))] // No `socket` in Miri. +#![cfg(not(loom))] // No udp / UdpFramed in loom use tokio::net::UdpSocket; use tokio_stream::StreamExt; From 24187f9f550d779fafcd31554dc5de2c1f630a3e Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Tue, 23 Sep 2025 23:39:54 +0300 Subject: [PATCH 2/6] tokio-util: use loom types in test mode only --- tokio-util/src/loom.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio-util/src/loom.rs b/tokio-util/src/loom.rs index 4b0a4274857..563e6d5ad25 100644 --- a/tokio-util/src/loom.rs +++ b/tokio-util/src/loom.rs @@ -2,8 +2,8 @@ //! are running loom tests or not. pub(crate) mod sync { - #[cfg(loom)] + #[cfg(all(test, loom))] pub(crate) use loom::sync::{Arc, Mutex, MutexGuard}; - #[cfg(not(loom))] + #[cfg(not(all(test, loom)))] pub(crate) use std::sync::{Arc, Mutex, MutexGuard}; } From 2b275166b501d696a573a0c5ccb1caab234396aa Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Wed, 24 Sep 2025 00:17:13 +0300 Subject: [PATCH 3/6] tokio-stream: skip compiling loom-incompatible wrappers on cfg(loom) --- tokio-stream/src/wrappers.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tokio-stream/src/wrappers.rs b/tokio-stream/src/wrappers.rs index 62cabe4f7d0..c79b6c7681b 100644 --- a/tokio-stream/src/wrappers.rs +++ b/tokio-stream/src/wrappers.rs @@ -22,9 +22,9 @@ cfg_sync! { } cfg_signal! { - #[cfg(unix)] + #[cfg(all(unix, not(loom)))] mod signal_unix; - #[cfg(unix)] + #[cfg(all(unix, not(loom)))] pub use signal_unix::SignalStream; #[cfg(any(windows, docsrs))] @@ -39,12 +39,14 @@ cfg_time! { } cfg_net! { + #[cfg(not(loom))] mod tcp_listener; + #[cfg(not(loom))] pub use tcp_listener::TcpListenerStream; - #[cfg(unix)] + #[cfg(all(unix, not(loom)))] mod unix_listener; - #[cfg(unix)] + #[cfg(all(unix, not(loom)))] pub use unix_listener::UnixListenerStream; } @@ -57,6 +59,8 @@ cfg_io_util! { } cfg_fs! { + #[cfg(not(loom))] mod read_dir; + #[cfg(not(loom))] pub use read_dir::ReadDirStream; } From 1981e523d46738a41882064cece5a2293fab11d6 Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Wed, 24 Sep 2025 11:41:34 +0300 Subject: [PATCH 4/6] tmp: always run loom-util-sync job --- .github/workflows/loom.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/loom.yml b/.github/workflows/loom.yml index f7f436fd5c3..15f5391f665 100644 --- a/.github/workflows/loom.yml +++ b/.github/workflows/loom.yml @@ -99,7 +99,7 @@ jobs: loom-util-sync: name: loom tokio-util::sync # base_ref is null when it's not a pull request - if: github.repository_owner == 'tokio-rs' && (contains(github.event.pull_request.labels.*.name, 'R-loom-util-sync') || (github.base_ref == null)) +# if: github.repository_owner == 'tokio-rs' && (contains(github.event.pull_request.labels.*.name, 'R-loom-util-sync') || (github.base_ref == null)) runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 From 3bdecd2b59dbecde4f048aa3f44c822f3be090b8 Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Wed, 24 Sep 2025 11:55:06 +0300 Subject: [PATCH 5/6] tokio-util: disable failing loom tests --- tokio-util/src/sync/tests/loom_cancellation_token.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tokio-util/src/sync/tests/loom_cancellation_token.rs b/tokio-util/src/sync/tests/loom_cancellation_token.rs index b57503ddb16..c92c8c807f1 100644 --- a/tokio-util/src/sync/tests/loom_cancellation_token.rs +++ b/tokio-util/src/sync/tests/loom_cancellation_token.rs @@ -100,6 +100,7 @@ fn drop_token_no_child() { }); } +#[ignore] #[test] fn drop_token_with_children() { loom::model(|| { @@ -125,6 +126,7 @@ fn drop_token_with_children() { }); } +#[ignore] #[test] fn drop_and_cancel_token() { loom::model(|| { @@ -150,6 +152,7 @@ fn drop_and_cancel_token() { }); } +#[ignore] #[test] fn cancel_parent_and_child() { loom::model(|| { From 361b3fcc0daa6095e0e8572caa05a3b8dcfdaf8c Mon Sep 17 00:00:00 2001 From: Benjamin Ran Date: Wed, 24 Sep 2025 17:22:09 +0300 Subject: [PATCH 6/6] Revert "tmp: always run loom-util-sync job" This reverts commit 1981e523d46738a41882064cece5a2293fab11d6. --- .github/workflows/loom.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/loom.yml b/.github/workflows/loom.yml index 15f5391f665..f7f436fd5c3 100644 --- a/.github/workflows/loom.yml +++ b/.github/workflows/loom.yml @@ -99,7 +99,7 @@ jobs: loom-util-sync: name: loom tokio-util::sync # base_ref is null when it's not a pull request -# if: github.repository_owner == 'tokio-rs' && (contains(github.event.pull_request.labels.*.name, 'R-loom-util-sync') || (github.base_ref == null)) + if: github.repository_owner == 'tokio-rs' && (contains(github.event.pull_request.labels.*.name, 'R-loom-util-sync') || (github.base_ref == null)) runs-on: ubuntu-latest steps: - uses: actions/checkout@v5