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
1 change: 1 addition & 0 deletions 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
37 changes: 36 additions & 1 deletion tokio/src/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,42 @@ impl<T> Sender<T> {
)
});

Ok(())
#[rustversion::since(1.70)]
fn consume_inner<T>(inner: Arc<Inner<T>>) -> Result<(), T> {
#[cfg(not(loom))]
{
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(())
}
}

#[cfg(loom)]
{
drop(inner);
// The `loom::sync::Arc` does not implement `into_inner` yet.
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
Loading