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

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

Open
cbusoft opened this issue Nov 29, 2024 · 0 comments · May be fixed by #416
Open

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

cbusoft opened this issue Nov 29, 2024 · 0 comments · May be fixed by #416

Comments

@cbusoft
Copy link
Contributor

cbusoft commented Nov 29, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant