Skip to content

Drop Rocket inside the tokio async context when using launch #2822

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test to validate new behavior
  • Loading branch information
the10thWiz authored and SergioBenitez committed Aug 23, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 24a84d52e496a703d280699e4418575f0b5b0e80
50 changes: 50 additions & 0 deletions core/lib/tests/drop-in-async-context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[macro_use] extern crate rocket;

use rocket::{Rocket, Build, build, fairing::AdHoc, Orbit};

struct AsyncDropInAsync;

impl Drop for AsyncDropInAsync {
fn drop(&mut self) {
// Attempt to fetch the current runtime while dropping
// Pools in rocket_sync_db_pools (and maybe rocket_db_pools)
// do use this capability. They spawn tasks to asyncronously
// complete shutdown of the pool, which triggers the same panic.
let _ = rocket::tokio::runtime::Handle::current();
}
}

fn rocket() -> Rocket<Build> {
build().manage(AsyncDropInAsync).attach(AdHoc::on_liftoff(
"Shutdown immediately",
|rocket: &Rocket<Orbit>| Box::pin(async {
rocket.shutdown().notify();
}
)))
}

mod launch {
#[launch]
fn launch() -> _ {
super::rocket()
}
#[test]
fn test_launch() {
main();
}
}

mod main {
#[rocket::main]
async fn main() {
super::rocket().launch().await.unwrap();
}
#[test]
fn test_main() {
main();
}
#[test]
fn test_execute() {
rocket::execute(super::rocket().launch()).unwrap();
}
}