Repository: actix/actix-net (language: rust)

Issue:
Make actix-rt (and actix itself) work with tokio console
When using tokio-console, all tasks run in actix-rt appear meaningless because they look like they are being run from the same line (see image below for illustrations). This issue used to be a tokio issue, but this was just fixed in this pr (https://github.com/tokio-rs/tokio/pull/4483). 

Now, have looked into where actix-rt could add "track_caller" attributes to different functions to have the same effect as tokio, but I don't know if this will work due to how actix-rt spawns futures by sending them across channels to arbiters. 

At least actix-rt could add `track_caller` to a few key functions and this would greatly improve usability.

1. https://github.com/actix/actix-net/blob/d229c1e886323d90e7c3a00e2b99d1c95dd759e0/actix-rt/src/system.rs#L208
2. https://github.com/actix/actix-net/blob/d229c1e886323d90e7c3a00e2b99d1c95dd759e0/actix-rt/src/system.rs#L235
3. https://github.com/actix/actix-net/blob/d229c1e886323d90e7c3a00e2b99d1c95dd759e0/actix-rt/src/runtime.rs#L56
4. https://github.com/actix/actix-net/blob/d229c1e886323d90e7c3a00e2b99d1c95dd759e0/actix-rt/src/runtime.rs#L76
5. https://github.com/actix/actix-net/blob/d229c1e886323d90e7c3a00e2b99d1c95dd759e0/actix-rt/src/lib.rs#L202

![image](https://user-images.githubusercontent.com/45189381/153776853-89bba7a5-9f34-40c8-a1d2-be3e1227f2ec.png)


Discussion / hints:
Duplicate of https://github.com/actix/actix-web/issues/2646. Fine to keep it open though since the change will need to happen here.

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index fc82ab18ec..e87c420519 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -73,6 +73,14 @@ jobs:
           command: install
           args: cargo-hack
 
+      - name: Generate Cargo.lock
+        uses: actions-rs/cargo@v1
+        with: { command: generate-lockfile }
+
+      - name: Tweak lockfile
+        run: |
+          cargo update -p=native-tls --precise=0.2.8
+
       - name: check lib
         if: >
           matrix.target.os != 'ubuntu-latest'
@@ -142,10 +150,9 @@ jobs:
         with: { command: generate-lockfile }
 
       - name: Tweak lockfile
-        uses: actions-rs/cargo@v1
-        with:
-          command: update
-          args: -p=rustls --precise=0.20.2
+        run: |
+          cargo update -p=rustls --precise=0.20.2
+          cargo update -p=native-tls --precise=0.2.8
 
       - name: tests
         run: |
diff --git a/actix-rt/CHANGES.md b/actix-rt/CHANGES.md
index 671b623a32..4715a4b56d 100644
--- a/actix-rt/CHANGES.md
+++ b/actix-rt/CHANGES.md
@@ -1,6 +1,9 @@
 # Changes
 
 ## Unreleased - 2022-xx-xx
+- Add `#[track_caller]` attribute to `spawn` functions and methods. [#454]
+
+[#454]: https://github.com/actix/actix-net/pull/454
 
 
 ## 2.7.0 - 2022-03-08
diff --git a/actix-rt/src/arbiter.rs b/actix-rt/src/arbiter.rs
index 95256360c3..4c9a588e10 100644
--- a/actix-rt/src/arbiter.rs
+++ b/actix-rt/src/arbiter.rs
@@ -260,6 +260,7 @@ impl Arbiter {
     /// If you require a result, include a response channel in the future.
     ///
     /// Returns true if future was sent successfully and false if the Arbiter has died.
+    #[track_caller]
     pub fn spawn<Fut>(&self, future: Fut) -> bool
     where
         Fut: Future<Output = ()> + Send + 'static,
@@ -275,6 +276,7 @@ impl Arbiter {
     /// channel in the function.
     ///
     /// Returns true if function was sent successfully and false if the Arbiter has died.
+    #[track_caller]
     pub fn spawn_fn<F>(&self, f: F) -> bool
     where
         F: FnOnce() + Send + 'static,
diff --git a/actix-rt/src/lib.rs b/actix-rt/src/lib.rs
index b2d5f8936b..23dd01e809 100644
--- a/actix-rt/src/lib.rs
+++ b/actix-rt/src/lib.rs
@@ -195,6 +195,7 @@ pub mod task {
 /// assert!(handle.await.unwrap_err().is_cancelled());
 /// # });
 /// ```
+#[track_caller]
 #[inline]
 pub fn spawn<Fut>(f: Fut) -> JoinHandle<Fut::Output>
 where
diff --git a/actix-rt/src/runtime.rs b/actix-rt/src/runtime.rs
index 25937003fe..557dfcfea9 100644
--- a/actix-rt/src/runtime.rs
+++ b/actix-rt/src/runtime.rs
@@ -53,6 +53,7 @@ impl Runtime {
     /// # Panics
     /// This function panics if the spawn fails. Failure occurs if the executor is currently at
     /// capacity and is unable to spawn a new future.
+    #[track_caller]
     pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
     where
         F: Future + 'static,
@@ -73,6 +74,7 @@ impl Runtime {
     ///
     /// The caller is responsible for ensuring that other spawned futures complete execution by
     /// calling `block_on` or `run`.
+    #[track_caller]
     pub fn block_on<F>(&self, f: F) -> F::Output
     where
         F: Future,
diff --git a/actix-rt/src/system.rs b/actix-rt/src/system.rs
index 0ea3547da3..f3a5a4d82c 100644
--- a/actix-rt/src/system.rs
+++ b/actix-rt/src/system.rs
@@ -204,6 +204,7 @@ impl SystemRunner {
     }
 
     /// Runs the provided future, blocking the current thread until the future completes.
+    #[track_caller]
     #[inline]
     pub fn block_on<F: Future>(&self, fut: F) -> F::Output {
         self.rt.block_on(fut)


Test patch: