Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oneshot channel): ensure msg won't be dropped on sender side when send returns ok #6558

Merged
4 changes: 3 additions & 1 deletion tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ time = []
tokio-macros = { version = "~2.2.0", path = "../tokio-macros", optional = true }

pin-project-lite = "0.2.11"
rustversion = { git = "https://github.com/wenym1/rustversion", rev = "0b11410" }
wenym1 marked this conversation as resolved.
Show resolved Hide resolved

# Everything else is optional...
bytes = { version = "1.0.0", optional = true }
Expand Down Expand Up @@ -152,7 +153,8 @@ wasm-bindgen-test = "0.3.0"
mio-aio = { version = "0.8.0", features = ["tokio"] }

[target.'cfg(loom)'.dev-dependencies]
loom = { version = "0.7", features = ["futures", "checkpoint"] }
loom = { git = "https://github.com/wenym1/loom", rev = "4edf9435", features = ["futures", "checkpoint"] }
#loom = { path = "/Users/william/repo/loom", features = ["futures", "checkpoint"] }

[package.metadata.docs.rs]
all-features = true
Expand Down
27 changes: 26 additions & 1 deletion tokio/src/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,32 @@ impl<T> Sender<T> {
)
});

Ok(())
#[rustversion::since(1.70)]
fn consume_inner<T>(inner: Arc<Inner<T>>) -> Result<(), T> {
if let Some(inner) = Arc::into_inner(inner) {
if let Some(t) = inner.value.with_mut(|ptr| unsafe {
// SAFETY: we have successfully returned with `Some`, which means we are the
// only accessor to `ptr`.
//
// Note: value can be `None` even though we have previously set it as `Some`,
// because the value may have been consumed by receiver before we reach here.
(*ptr).take()
}) {
Err(t)
} else {
Ok(())
}
} else {
Ok(())
}
}

#[rustversion::before(1.70)]
fn consume_inner<T>(_inner: Arc<Inner<T>>) -> Result<(), T> {
Ok(())
}

consume_inner(inner)
}

/// Waits for the associated [`Receiver`] handle to close.
Expand Down
49 changes: 48 additions & 1 deletion tokio/src/sync/tests/loom_oneshot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sync::oneshot;
use crate::sync::{mpsc, oneshot};

use futures::future::poll_fn;
use loom::future::block_on;
Expand Down Expand Up @@ -86,6 +86,8 @@ fn recv_closed() {

// TODO: Move this into `oneshot` proper.

use crate::loom;
use crate::sync::oneshot::{Receiver, Sender};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -138,3 +140,48 @@ fn changing_tx_task() {
}
});
}

#[test]
fn checking_tx_send_ok_not_drop() {
use std::cell::RefCell;
loom::thread_local! {
static IS_RX: RefCell<bool> = RefCell::new(true);
}
wenym1 marked this conversation as resolved.
Show resolved Hide resolved

struct Msg;

impl Drop for Msg {
fn drop(&mut self) {
IS_RX.with(|is_rx: &RefCell<_>| {
// On `tx.send(msg)` returning `Err(msg)`,
// we call `std::mem::forget(msg)`, so that
// `drop` is not expected to be called in the
// tx thread.
assert!(*is_rx.borrow());
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the newly added code in oneshot.rs is removed, we will hit this assertion.

}
}

loom::model(|| {
let (tx, rx) = oneshot::channel();

// tx thread
let tx_thread_join_handle = thread::spawn(move || {
// Ensure that `Msg::drop` in this thread will see is_rx == false
IS_RX.with(|is_rx: &RefCell<_>| {
*is_rx.borrow_mut() = false;
});
if let Err(msg) = tx.send(Msg) {
std::mem::forget(msg);
}
});

// rx thread
let rx_thread_join_handle = thread::spawn(move || {
drop(rx);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just call drop(rx) on the main thread. Spawning threads is very expensive in loom tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a try.

If I simply use drop(rx) in the main thread, when I revert the changes in oneshot.rs, the assertion cannot be hit, and if I do it in a spawned thread, the assertion can be hit.

I doubt that only drop(rx) in a separate thread can cover the case in loom.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you hit the assertion with LOOM_MAX_PREEMPTIONS=2? That's what we run with in CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, when I set it to 2, the assertion can be hit.

Should the CONTRIBUTING.md be updated? I ran the loom test following the guide in it.

LOOM_MAX_PREEMPTIONS=1 LOOM_MAX_BRANCHES=10000 RUSTFLAGS="--cfg loom -C debug_assertions" \

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It takes a lot longer to run with the option set to 2, and most bugs can be caught with the value set to 1. It may be better to have your test use the loom test builder to explicitly set the max preemptions to 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Have updated the code.

tx_thread_join_handle.join().unwrap();
rx_thread_join_handle.join().unwrap();
});
}
Loading