aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChih-hung Hsieh <chh@google.com>2020-11-05 23:33:09 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-11-05 23:33:09 +0000
commit01c57918c4b9c5e48d302d6964150db531feaf29 (patch)
tree6da637261bc53435d3f319460265eca62c16b50d
parenteebe2aa275482024a9c415f4324b113db8e513cf (diff)
parentd45ebac89067bf60e5226c2a3378bf47186fd82f (diff)
downloadtokio-01c57918c4b9c5e48d302d6964150db531feaf29.tar.gz
Merge "Upgrade rust/crates/tokio to 0.3.3" am: d45ebac890
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/tokio/+/1484997 Change-Id: I102c6f1751b450e7ba465df2f471e55b0050bd9b
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--Android.bp9
-rw-r--r--CHANGELOG.md16
-rw-r--r--Cargo.toml16
-rw-r--r--Cargo.toml.orig14
-rw-r--r--METADATA8
-rw-r--r--build.rs22
-rw-r--r--src/lib.rs2
-rw-r--r--src/net/tcp/socket.rs121
-rw-r--r--src/net/unix/ucred.rs75
-rw-r--r--src/runtime/blocking/pool.rs2
-rw-r--r--src/runtime/blocking/task.rs3
-rw-r--r--src/runtime/handle.rs16
-rw-r--r--src/runtime/mod.rs7
-rw-r--r--src/task/blocking.rs1
-rw-r--r--src/task/local.rs2
-rw-r--r--src/task/spawn.rs1
-rw-r--r--src/util/trace.rs48
18 files changed, 296 insertions, 69 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 9faa151..2b16356 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,5 @@
{
"git": {
- "sha1": "9097ae548f9a1bcd261385ceba29b800b0ee2a21"
+ "sha1": "42de3bc7a44100eec06645a5bfef8be286ff0507"
}
}
diff --git a/Android.bp b/Android.bp
index 0faaf79..3e26007 100644
--- a/Android.bp
+++ b/Android.bp
@@ -8,7 +8,6 @@ rust_library {
edition: "2018",
features: [
"bytes",
- "fnv",
"futures-core",
"io-util",
"lazy_static",
@@ -25,9 +24,11 @@ rust_library {
"sync",
"tokio-macros",
],
+ flags: [
+ "--cfg tokio_track_caller",
+ ],
rustlibs: [
"libbytes",
- "libfnv",
"libfutures_core",
"liblazy_static",
"liblibc",
@@ -41,15 +42,15 @@ rust_library {
}
// dependent_library ["feature_list"]
+// autocfg-1.0.1
// bytes-0.6.0 "default,std"
// cfg-if-0.1.10
-// fnv-1.0.7 "default,std"
// futures-core-0.3.7 "alloc,default,std"
// lazy_static-1.4.0
// libc-0.2.80 "default,extra_traits,std"
// log-0.4.11
// memchr-2.3.4 "default,std"
-// mio-0.7.4 "default,os-poll,os-util,tcp,udp,uds"
+// mio-0.7.5 "default,os-poll,os-util,tcp,udp,uds"
// num_cpus-1.13.0
// pin-project-lite-0.1.11
// proc-macro2-1.0.24 "default,proc-macro"
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fa17be4..ce979cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,18 @@
+# 0.3.3 (November 2, 2020)
+
+Fixes a soundness hole by adding a missing `Send` bound to
+`Runtime::spawn_blocking()`.
+
+### Fixed
+- rt: include missing `Send`, fixing soundness hole (#3089).
+- tracing: avoid huge trace span names (#3074).
+
+### Added
+- net: `TcpSocket::reuseport()`, `TcpSocket::set_reuseport()` (#3083).
+- net: `TcpSocket::reuseaddr()` (#3093).
+- net: `TcpSocket::local_addr()` (#3093).
+- net: add pid to `UCred` (#2633).
+
# 0.3.2 (October 27, 2020)
Adds `AsyncFd` as a replacement for v0.2's `PollEvented`.
@@ -66,6 +81,7 @@ Biggest changes are:
- fs: `File` operations take `&self` (#2930).
- rt: runtime API, and `#[tokio::main]` macro polish (#2876)
- rt: `Runtime::enter` uses an RAII guard instead of a closure (#2954).
+- net: the `from_std` function on all sockets no longer sets socket into non-blocking mode (#2893)
### Added
- sync: `map` function to lock guards (#2445).
diff --git a/Cargo.toml b/Cargo.toml
index da14246..db23f03 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,11 +13,11 @@
[package]
edition = "2018"
name = "tokio"
-version = "0.3.2"
+version = "0.3.3"
authors = ["Tokio Contributors <team@tokio.rs>"]
description = "An event-driven, non-blocking I/O platform for writing asynchronous I/O\nbacked applications.\n"
homepage = "https://tokio.rs"
-documentation = "https://docs.rs/tokio/0.3.2/tokio/"
+documentation = "https://docs.rs/tokio/0.3.3/tokio/"
readme = "README.md"
keywords = ["io", "async", "non-blocking", "futures"]
categories = ["asynchronous", "network-programming"]
@@ -33,10 +33,6 @@ features = ["full"]
version = "0.6.0"
optional = true
-[dependencies.fnv]
-version = "1.0.6"
-optional = true
-
[dependencies.futures-core]
version = "0.3.0"
optional = true
@@ -50,7 +46,7 @@ version = "2.2"
optional = true
[dependencies.mio]
-version = "0.7.3"
+version = "0.7.5"
optional = true
[dependencies.num_cpus]
@@ -73,7 +69,7 @@ version = "0.3.0"
optional = true
[dependencies.tracing]
-version = "0.1.16"
+version = "0.1.21"
features = ["std"]
optional = true
default-features = false
@@ -89,6 +85,8 @@ version = "3.1.0"
[dev-dependencies.tokio-test]
version = "0.3.0"
+[build-dependencies.autocfg]
+version = "1"
[features]
default = []
@@ -103,7 +101,7 @@ rt = ["slab"]
rt-multi-thread = ["num_cpus", "rt"]
signal = ["lazy_static", "libc", "mio/os-poll", "mio/uds", "mio/os-util", "signal-hook-registry", "winapi/consoleapi"]
stream = ["futures-core"]
-sync = ["fnv"]
+sync = []
test-util = []
time = []
[target."cfg(loom)".dev-dependencies.loom]
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 144c2d5..d1dbda1 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -8,12 +8,12 @@ name = "tokio"
# - README.md
# - Update CHANGELOG.md.
# - Create "v0.3.x" git tag.
-version = "0.3.2"
+version = "0.3.3"
edition = "2018"
authors = ["Tokio Contributors <team@tokio.rs>"]
license = "MIT"
readme = "README.md"
-documentation = "https://docs.rs/tokio/0.3.2/tokio/"
+documentation = "https://docs.rs/tokio/0.3.3/tokio/"
repository = "https://github.com/tokio-rs/tokio"
homepage = "https://tokio.rs"
description = """
@@ -84,7 +84,7 @@ signal = [
"winapi/consoleapi",
]
stream = ["futures-core"]
-sync = ["fnv"]
+sync = []
test-util = []
time = []
@@ -95,15 +95,14 @@ pin-project-lite = "0.1.1"
# Everything else is optional...
bytes = { version = "0.6.0", optional = true }
-fnv = { version = "1.0.6", optional = true }
futures-core = { version = "0.3.0", optional = true }
lazy_static = { version = "1.0.2", optional = true }
memchr = { version = "2.2", optional = true }
-mio = { version = "0.7.3", optional = true }
+mio = { version = "0.7.5", optional = true }
num_cpus = { version = "1.8.0", optional = true }
parking_lot = { version = "0.11.0", optional = true } # Not in full
slab = { version = "0.4.1", optional = true }
-tracing = { version = "0.1.16", default-features = false, features = ["std"], optional = true } # Not in full
+tracing = { version = "0.1.21", default-features = false, features = ["std"], optional = true } # Not in full
[target.'cfg(unix)'.dependencies]
libc = { version = "0.2.42", optional = true }
@@ -127,6 +126,9 @@ tempfile = "3.1.0"
[target.'cfg(loom)'.dev-dependencies]
loom = { version = "0.3.5", features = ["futures", "checkpoint"] }
+[build-dependencies]
+autocfg = "1" # Needed for conditionally enabling `track-caller`
+
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
diff --git a/METADATA b/METADATA
index 0de43aa..f70c049 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/tokio/tokio-0.3.2.crate"
+ value: "https://static.crates.io/crates/tokio/tokio-0.3.3.crate"
}
- version: "0.3.2"
+ version: "0.3.3"
license_type: NOTICE
last_upgrade_date {
year: 2020
- month: 10
- day: 28
+ month: 11
+ day: 2
}
}
diff --git a/build.rs b/build.rs
new file mode 100644
index 0000000..fe5c830
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,22 @@
+use autocfg::AutoCfg;
+
+fn main() {
+ match AutoCfg::new() {
+ Ok(ac) => {
+ // The #[track_caller] attribute was stabilized in rustc 1.46.0.
+ if ac.probe_rustc_version(1, 46) {
+ autocfg::emit("tokio_track_caller")
+ }
+ }
+
+ Err(e) => {
+ // If we couldn't detect the compiler version and features, just
+ // print a warning. This isn't a fatal error: we can still build
+ // Tokio, we just can't enable cfgs automatically.
+ println!(
+ "cargo:warning=tokio: failed to detect compiler features: {}",
+ e
+ );
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index b14ae72..229f050 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![doc(html_root_url = "https://docs.rs/tokio/0.3.2")]
+#![doc(html_root_url = "https://docs.rs/tokio/0.3.3")]
#![allow(
clippy::cognitive_complexity,
clippy::large_enum_variant,
diff --git a/src/net/tcp/socket.rs b/src/net/tcp/socket.rs
index 5b0f802..9e3ca99 100644
--- a/src/net/tcp/socket.rs
+++ b/src/net/tcp/socket.rs
@@ -183,6 +183,127 @@ impl TcpSocket {
self.inner.set_reuseaddr(reuseaddr)
}
+ /// Retrieves the value set for `SO_REUSEADDR` on this socket
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use tokio::net::TcpSocket;
+ ///
+ /// use std::io;
+ ///
+ /// #[tokio::main]
+ /// async fn main() -> io::Result<()> {
+ /// let addr = "127.0.0.1:8080".parse().unwrap();
+ ///
+ /// let socket = TcpSocket::new_v4()?;
+ /// socket.set_reuseaddr(true)?;
+ /// assert!(socket.reuseaddr().unwrap());
+ /// socket.bind(addr)?;
+ ///
+ /// let listener = socket.listen(1024)?;
+ /// Ok(())
+ /// }
+ /// ```
+ pub fn reuseaddr(&self) -> io::Result<bool> {
+ self.inner.get_reuseaddr()
+ }
+
+ /// Allow the socket to bind to an in-use port. Only available for unix systems
+ /// (excluding Solaris & Illumos).
+ ///
+ /// Behavior is platform specific. Refer to the target platform's
+ /// documentation for more details.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use tokio::net::TcpSocket;
+ ///
+ /// use std::io;
+ ///
+ /// #[tokio::main]
+ /// async fn main() -> io::Result<()> {
+ /// let addr = "127.0.0.1:8080".parse().unwrap();
+ ///
+ /// let socket = TcpSocket::new_v4()?;
+ /// socket.set_reuseport(true)?;
+ /// socket.bind(addr)?;
+ ///
+ /// let listener = socket.listen(1024)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
+ #[cfg_attr(
+ docsrs,
+ doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
+ )]
+ pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
+ self.inner.set_reuseport(reuseport)
+ }
+
+ /// Allow the socket to bind to an in-use port. Only available for unix systems
+ /// (excluding Solaris & Illumos).
+ ///
+ /// Behavior is platform specific. Refer to the target platform's
+ /// documentation for more details.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use tokio::net::TcpSocket;
+ ///
+ /// use std::io;
+ ///
+ /// #[tokio::main]
+ /// async fn main() -> io::Result<()> {
+ /// let addr = "127.0.0.1:8080".parse().unwrap();
+ ///
+ /// let socket = TcpSocket::new_v4()?;
+ /// socket.set_reuseport(true)?;
+ /// assert!(socket.reuseport().unwrap());
+ /// socket.bind(addr)?;
+ ///
+ /// let listener = socket.listen(1024)?;
+ /// Ok(())
+ /// }
+ /// ```
+ #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
+ #[cfg_attr(
+ docsrs,
+ doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
+ )]
+ pub fn reuseport(&self) -> io::Result<bool> {
+ self.inner.get_reuseport()
+ }
+
+ /// Get the local address of this socket.
+ ///
+ /// Will fail on windows if called before `bind`.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use tokio::net::TcpSocket;
+ ///
+ /// use std::io;
+ ///
+ /// #[tokio::main]
+ /// async fn main() -> io::Result<()> {
+ /// let addr = "127.0.0.1:8080".parse().unwrap();
+ ///
+ /// let socket = TcpSocket::new_v4()?;
+ /// socket.bind(addr)?;
+ /// assert_eq!(socket.local_addr().unwrap().to_string(), "127.0.0.1:8080");
+ /// let listener = socket.listen(1024)?;
+ /// Ok(())
+ /// }
+ /// ```
+ pub fn local_addr(&self) -> io::Result<SocketAddr> {
+ self.inner.get_localaddr()
+ }
+
/// Bind the socket to the given address.
///
/// This calls the `bind(2)` operating-system function. Behavior is
diff --git a/src/net/unix/ucred.rs b/src/net/unix/ucred.rs
index ef214a7..7d73ee0 100644
--- a/src/net/unix/ucred.rs
+++ b/src/net/unix/ucred.rs
@@ -1,8 +1,10 @@
-use libc::{gid_t, uid_t};
+use libc::{gid_t, pid_t, uid_t};
/// Credentials of a process
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct UCred {
+ /// PID (process ID) of the process
+ pid: Option<pid_t>,
/// UID (user ID) of the process
uid: uid_t,
/// GID (group ID) of the process
@@ -19,6 +21,13 @@ impl UCred {
pub fn gid(&self) -> gid_t {
self.gid
}
+
+ /// Gets PID (process ID) of the process.
+ ///
+ /// This is only implemented under linux, android, IOS and MacOS
+ pub fn pid(&self) -> Option<pid_t> {
+ self.pid
+ }
}
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -26,12 +35,13 @@ pub(crate) use self::impl_linux::get_peer_cred;
#[cfg(any(
target_os = "dragonfly",
- target_os = "macos",
- target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
+pub(crate) use self::impl_bsd::get_peer_cred;
+
+#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) use self::impl_macos::get_peer_cred;
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
@@ -77,6 +87,7 @@ pub(crate) mod impl_linux {
Ok(super::UCred {
uid: ucred.uid,
gid: ucred.gid,
+ pid: Some(ucred.pid),
})
} else {
Err(io::Error::last_os_error())
@@ -87,13 +98,11 @@ pub(crate) mod impl_linux {
#[cfg(any(
target_os = "dragonfly",
- target_os = "macos",
- target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
-pub(crate) mod impl_macos {
+pub(crate) mod impl_bsd {
use crate::net::unix::UnixStream;
use libc::getpeereid;
@@ -114,6 +123,54 @@ pub(crate) mod impl_macos {
Ok(super::UCred {
uid: uid.assume_init(),
gid: gid.assume_init(),
+ pid: None,
+ })
+ } else {
+ Err(io::Error::last_os_error())
+ }
+ }
+ }
+}
+
+#[cfg(any(target_os = "macos", target_os = "ios"))]
+pub(crate) mod impl_macos {
+ use crate::net::unix::UnixStream;
+
+ use libc::{c_void, getpeereid, getsockopt, pid_t, LOCAL_PEEREPID, SOL_LOCAL};
+ use std::io;
+ use std::mem::size_of;
+ use std::mem::MaybeUninit;
+ use std::os::unix::io::AsRawFd;
+
+ pub(crate) fn get_peer_cred(sock: &UnixStream) -> io::Result<super::UCred> {
+ unsafe {
+ let raw_fd = sock.as_raw_fd();
+
+ let mut uid = MaybeUninit::uninit();
+ let mut gid = MaybeUninit::uninit();
+ let mut pid: MaybeUninit<pid_t> = MaybeUninit::uninit();
+ let mut pid_size: MaybeUninit<u32> = MaybeUninit::new(size_of::<pid_t>() as u32);
+
+ if getsockopt(
+ raw_fd,
+ SOL_LOCAL,
+ LOCAL_PEEREPID,
+ pid.as_mut_ptr() as *mut c_void,
+ pid_size.as_mut_ptr(),
+ ) != 0
+ {
+ return Err(io::Error::last_os_error());
+ }
+
+ assert!(pid_size.assume_init() == (size_of::<pid_t>() as u32));
+
+ let ret = getpeereid(raw_fd, uid.as_mut_ptr(), gid.as_mut_ptr());
+
+ if ret == 0 {
+ Ok(super::UCred {
+ uid: uid.assume_init(),
+ gid: gid.assume_init(),
+ pid: Some(pid.assume_init()),
})
} else {
Err(io::Error::last_os_error())
@@ -154,7 +211,11 @@ pub(crate) mod impl_solaris {
ucred_free(cred);
- Ok(super::UCred { uid, gid })
+ Ok(super::UCred {
+ uid,
+ gid,
+ pid: None,
+ })
} else {
Err(io::Error::last_os_error())
}
diff --git a/src/runtime/blocking/pool.rs b/src/runtime/blocking/pool.rs
index 2967a10..6b9fb1b 100644
--- a/src/runtime/blocking/pool.rs
+++ b/src/runtime/blocking/pool.rs
@@ -70,6 +70,7 @@ const KEEP_ALIVE: Duration = Duration::from_secs(10);
pub(crate) fn spawn_blocking<F, R>(func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
{
let rt = context::current().expect("not currently running on the Tokio runtime.");
rt.spawn_blocking(func)
@@ -79,6 +80,7 @@ where
pub(crate) fn try_spawn_blocking<F, R>(func: F) -> Result<(), ()>
where
F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
{
let rt = context::current().expect("not currently running on the Tokio runtime.");
diff --git a/src/runtime/blocking/task.rs b/src/runtime/blocking/task.rs
index a521af4..ee2d8d6 100644
--- a/src/runtime/blocking/task.rs
+++ b/src/runtime/blocking/task.rs
@@ -19,7 +19,8 @@ impl<T> Unpin for BlockingTask<T> {}
impl<T, R> Future for BlockingTask<T>
where
- T: FnOnce() -> R,
+ T: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
{
type Output = R;
diff --git a/src/runtime/handle.rs b/src/runtime/handle.rs
index b1e8d8f..72b9c06 100644
--- a/src/runtime/handle.rs
+++ b/src/runtime/handle.rs
@@ -39,13 +39,27 @@ impl Handle {
// context::enter(self.clone(), f)
// }
- /// Run the provided function on an executor dedicated to blocking operations.
+ /// Run the provided function on an executor dedicated to blocking
+ /// operations.
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub(crate) fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
{
#[cfg(feature = "tracing")]
let func = {
+ #[cfg(tokio_track_caller)]
+ let location = std::panic::Location::caller();
+ #[cfg(tokio_track_caller)]
+ let span = tracing::trace_span!(
+ target: "tokio::task",
+ "task",
+ kind = %"blocking",
+ function = %std::any::type_name::<F>(),
+ spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
+ );
+ #[cfg(not(tokio_track_caller))]
let span = tracing::trace_span!(
target: "tokio::task",
"task",
diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs
index be4aa38..f85344d 100644
--- a/src/runtime/mod.rs
+++ b/src/runtime/mod.rs
@@ -357,11 +357,14 @@ cfg_rt! {
/// });
/// # }
/// ```
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
+ #[cfg(feature = "tracing")]
+ let future = crate::util::trace::task(future, "task");
match &self.kind {
#[cfg(feature = "rt-multi-thread")]
Kind::ThreadPool(exec) => exec.spawn(future),
@@ -385,9 +388,11 @@ cfg_rt! {
/// println!("now running on a worker thread");
/// });
/// # }
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
+ R: Send + 'static,
{
self.handle.spawn_blocking(func)
}
@@ -415,7 +420,7 @@ cfg_rt! {
///
/// # Panics
///
- /// This function panics if the provided future panics, or if not called within an
+ /// This function panics if the provided future panics, or if called within an
/// asynchronous execution context.
///
/// # Examples
diff --git a/src/task/blocking.rs b/src/task/blocking.rs
index fc6632b..36bc457 100644
--- a/src/task/blocking.rs
+++ b/src/task/blocking.rs
@@ -104,6 +104,7 @@ cfg_rt_multi_thread! {
/// # Ok(())
/// # }
/// ```
+#[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
diff --git a/src/task/local.rs b/src/task/local.rs
index 5896126..566b2f2 100644
--- a/src/task/local.rs
+++ b/src/task/local.rs
@@ -190,6 +190,7 @@ cfg_rt! {
/// }).await;
/// }
/// ```
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
@@ -273,6 +274,7 @@ impl LocalSet {
/// }
/// ```
/// [`spawn_local`]: fn@spawn_local
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn_local<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
diff --git a/src/task/spawn.rs b/src/task/spawn.rs
index 77acb57..a060852 100644
--- a/src/task/spawn.rs
+++ b/src/task/spawn.rs
@@ -122,6 +122,7 @@ cfg_rt! {
/// ```text
/// error[E0391]: cycle detected when processing `main`
/// ```
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub fn spawn<T>(task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
diff --git a/src/util/trace.rs b/src/util/trace.rs
index 18956a3..96a9db9 100644
--- a/src/util/trace.rs
+++ b/src/util/trace.rs
@@ -1,47 +1,27 @@
cfg_trace! {
cfg_rt! {
- use std::future::Future;
- use std::pin::Pin;
- use std::task::{Context, Poll};
- use pin_project_lite::pin_project;
-
- use tracing::Span;
-
- pin_project! {
- /// A future that has been instrumented with a `tracing` span.
- #[derive(Debug, Clone)]
- pub(crate) struct Instrumented<T> {
- #[pin]
- inner: T,
- span: Span,
- }
- }
-
- impl<T: Future> Future for Instrumented<T> {
- type Output = T::Output;
-
- fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
- let this = self.project();
- let _enter = this.span.enter();
- this.inner.poll(cx)
- }
- }
-
- impl<T> Instrumented<T> {
- pub(crate) fn new(inner: T, span: Span) -> Self {
- Self { inner, span }
- }
- }
+ pub(crate) use tracing::instrument::Instrumented;
#[inline]
+ #[cfg_attr(tokio_track_caller, track_caller)]
pub(crate) fn task<F>(task: F, kind: &'static str) -> Instrumented<F> {
+ use tracing::instrument::Instrument;
+ #[cfg(tokio_track_caller)]
+ let location = std::panic::Location::caller();
+ #[cfg(tokio_track_caller)]
+ let span = tracing::trace_span!(
+ target: "tokio::task",
+ "task",
+ %kind,
+ spawn.location = %format_args!("{}:{}:{}", location.file(), location.line(), location.column()),
+ );
+ #[cfg(not(tokio_track_caller))]
let span = tracing::trace_span!(
target: "tokio::task",
"task",
%kind,
- future = %std::any::type_name::<F>(),
);
- Instrumented::new(task, span)
+ task.instrument(span)
}
}
}