Skip to content

make the repl tool work with the tokio console#4418

Closed
Simon-Laux wants to merge 1 commit intomasterfrom
tokio-console
Closed

make the repl tool work with the tokio console#4418
Simon-Laux wants to merge 1 commit intomasterfrom
tokio-console

Conversation

@Simon-Laux
Copy link
Member

https://github.com/tokio-rs/console/tree/main

this can help with debugging async stuff.

it enables the tokio_unstable flag so maybe we can not merge it. but even then we can still rebase and keep this branch up to date and use it for debugging.

Bildschirmfoto 2023-05-22 um 21 03 15

@Simon-Laux Simon-Laux marked this pull request as draft May 22, 2023 19:06
@flub
Copy link
Contributor

flub commented May 22, 2023

This is very nice, we should merge it in some way. The --cfg rustflag is really annoying though. We could make tokio-console support a new cargo feature and pull in the dependency conditional on that feature. Then document that if you enable that feature you also need to set that --cfg in the RUSTFLAGS environment variable. Does that seem like a reasonable approach?

@link2xt
Copy link
Collaborator

link2xt commented May 22, 2023

I have created a question about the column meaning at tokio-rs/console#426

@dignifiedquire
Copy link
Collaborator

very cool, agree with @flub s comments

@link2xt link2xt deleted the branch master October 25, 2023 21:22
@link2xt link2xt closed this Oct 25, 2023
@link2xt link2xt reopened this Oct 25, 2023
@link2xt link2xt closed this Nov 10, 2023
@link2xt
Copy link
Collaborator

link2xt commented Nov 10, 2023

As this can't be merged, moving it away, still can be found with some effort.

@Simon-Laux
Copy link
Member Author

Simon-Laux commented Sep 27, 2024

How about replacing task spawns with a wrapper function like this?:

#[inline(always)]
pub fn spawn_named_task<Fut> (name:&str, future: Fut) -> JoinHandle<Fut::Output> where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static, {
    #[cfg(tokio_unstable)]
    {
        tokio::task::Builder::new().name(name)
        .spawn(future).expect("Failed to spawn task")
        
    }
    #[cfg(not(tokio_unstable))]
    {
        tokio::task::spawn(future)
    }
}

Note that it's always in-lined to remove any performance hits when compiling it without the tokio_unstable rustflag.

Function with documentation comment

disclaimer: doc comment drafted by gpt4o mini then reviewed and refined by me.
why did I use a LLM for drafting the documentation? because it's better than me in writing nice looking text.

/// Spawns a named asynchronous task if the `tokio_unstable` feature is enabled.
/// 
/// Spawns a new asynchronous task, returning a [JoinHandle] for it.
/// The provided future will start running in the background immediately when spawn is called, even if you don't await the returned JoinHandle.
/// See [tokio::task::spawn].
/// 
/// If the rustflag `tokio_unstable` is active, the task will be given the specified `name`
/// for easier identification in monitoring tools (like tokio-console).
/// If `tokio_unstable` is not set, the task is spawned normally without a name.
///
/// # Parameters
///
/// - `name`: The name of the task (used only if `tokio_unstable` is enabled).
/// - `future`: The future to be executed, which must implement `Future`, be `Send`, and `'static`.
///
/// # Returns
///
/// A [JoinHandle] that can be awaited to retrieve the output of the future.
///
/// # Panics
///
/// Panics if the task fails to spawn when `tokio_unstable` is enabled.
///
/// # Example
///
/// ```
/// use tokio::task;
///
/// let handle = spawn_named_task("my_task", async {
///     // Your async code here
/// });
///
/// let result = handle.await.unwrap();
/// ```
#[inline(always)]
pub fn spawn_named_task<Fut> (name:&str, future: Fut) -> JoinHandle<Fut::Output> where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static, {
    #[cfg(tokio_unstable)]
    {
        tokio::task::Builder::new().name(name)
        .spawn(future).expect("Failed to spawn task")
        
    }
    #[cfg(not(tokio_unstable))]
    {
        tokio::task::spawn(future)
    }
}

@Simon-Laux
Copy link
Member Author

Simon-Laux commented Sep 27, 2024

I plan on finishing this soon. so reopening.

Update: opening failed because main branch does not exist anymore. And for some reason github does not allow me to change the base-branch anymore (maybe bug with deleted base branch). I'll recreate the pr soon then link it here.

@Simon-Laux Simon-Laux self-assigned this Sep 27, 2024
@Simon-Laux
Copy link
Member Author

Seems like we need a macro so the location is also correct in tokio-console:

#[macro_export]
macro_rules! spawn_named_task {
    ($name:expr, $future:expr) => {{
        #[inline(always)]
        pub fn __spawn_named_task<Fut>(
            name: &str,
            future: Fut,
        ) -> ::tokio::task::JoinHandle<Fut::Output>
        where
            Fut: ::std::future::Future + Send + 'static,
            Fut::Output: Send + 'static,
        {
            #[cfg(tokio_unstable)]
            {
                ::tokio::task::Builder::new()
                    .name(name)
                    .spawn(future)
                    .expect("Failed to spawn task")
            }
            #[cfg(not(tokio_unstable))]
            {
                ::tokio::task::spawn(future)
            }
        }
        __spawn_named_task($name, $future)
    }};
}
With docs
/// Spawns a named asynchronous task if the `tokio_unstable` feature is enabled.
///
/// Spawns a new asynchronous task, returning a [tokio::task::JoinHandle] for it.
/// The provided future will start running in the background immediately when spawn is called, even if you don't await the returned JoinHandle.
/// See [tokio::task::spawn].
///
/// If the rustflag `tokio_unstable` is active, the task will be given the specified `name`
/// for easier identification in monitoring tools (like tokio-console).
/// If `tokio_unstable` is not set, the task is spawned normally without a name.
///
/// # Parameters
///
/// - `name`: The name of the task (used only if `tokio_unstable` is enabled).
/// - `future`: The future to be executed, which must implement `Future`, be `Send`, and `'static`.
///
/// # Returns
///
/// A [tokio::task::JoinHandle] that can be awaited to retrieve the output of the future.
///
/// # Panics
///
/// Panics if the task fails to spawn when `tokio_unstable` is enabled.
///
/// # Example
///
/// ```
/// use tokio::task;
///
/// let handle = spawn_named_task!("my_task", async {
///     // Your async code here
/// });
///
/// let result = handle.await.unwrap();
/// ```
#[macro_export]
macro_rules! spawn_named_task {
    ($name:expr, $future:expr) => {{
        #[inline(always)]
        pub fn __spawn_named_task<Fut>(
            name: &str,
            future: Fut,
        ) -> ::tokio::task::JoinHandle<Fut::Output>
        where
            Fut: ::std::future::Future + Send + 'static,
            Fut::Output: Send + 'static,
        {
            #[cfg(tokio_unstable)]
            {
                ::tokio::task::Builder::new()
                    .name(name)
                    .spawn(future)
                    .expect("Failed to spawn task")
            }
            #[cfg(not(tokio_unstable))]
            {
                ::tokio::task::spawn(future)
            }
        }
        __spawn_named_task($name, $future)
    }};
}

@iequidoo
Copy link
Collaborator

opening failed because main branch does not exist anymore.

I think it's ok to recreate the master branch and then remove it again :)

@Simon-Laux
Copy link
Member Author

I think it's ok to recreate the master branch and then remove it again :)

I'll make a new pr soon, this pr is already old and probably outdated anyway.

@Simon-Laux
Copy link
Member Author

The new pr: #6018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants