Skip to content

Server: Problem with consuming behaviour of Server::run_server_on_runtime #415

@cbusoft

Description

@cbusoft

Hi, currently the function Server::run_server_on_runtime consumes the tokio::runtime::Runtime. This works fine with block set to true, but when block is set to false, the runtime gets dropped after Some(runtime.spawn(server_task)).

pub fn run_server_on_runtime<F>(
runtime: tokio::runtime::Runtime,
server_task: F,
block: bool,
) -> Option<tokio::task::JoinHandle<<F as futures::Future>::Output>>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
if block {
runtime.block_on(server_task);
info!("Server has finished");
None
} else {
Some(runtime.spawn(server_task))
}
}

This is a problem, because "Shutting down the runtime is done by dropping the value" (see https://docs.rs/tokio/1.41.1/tokio/runtime/struct.Runtime.html#shutdown). I don't see a reason why the run_server_on_runtime method would need to consume the value, so passing in the runtime by reference would most likely work, but I think it would even be better to use a tokio::runtime::Handle, since this can also be obtained via Handle::current() when the runtime is created by tokio through #[tokio::main] instead of explicitly by the user.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions