Skip to content

Commit

Permalink
Start using fbinit-provided async runtime
Browse files Browse the repository at this point in the history
Summary:
Another take on unifying async runtimes throughout the target_determinator/*.

As was pointed out in D60313119, the blast radius of supertd changes is large, so instead of converting supertd to async and then following up with dependencies one by one, doing it in the opposite direction.

Until we eliminate all ad-hoc runtimes in dependencies, supertd will stay sync, wrapping newly-async stuff in separate block_on's. Then we just switch to one top-level runtime.

Below is the copy of D60313119 summary.

We're spawning tokio runtimes in many places whenever we need to interact with async code. This leads to many weird and confusing things, like creating nested runtimes running on different threads as in [configerator_td](https://www.internalfb.com/code/fbsource/[ea9e394c36cb]/fbcode/target_determinator/configerator_td/src/lib.rs?lines=28) running scheduler which in turn creates 2 separate runtimes.

CLI foundation provides idiomatic way of creating tokio runtime via fbinit::main (or cli::main), and given that we almost always just execute stuff linearly, .await'ing on everything, using it seems like a good idea.

Reviewed By: rjbailey

Differential Revision: D60521049

fbshipit-source-id: c44874da2c346611523921e96825e5003a29ea40
  • Loading branch information
Fedir Panasenko authored and facebook-github-bot committed Aug 6, 2024
1 parent 2809999 commit 1766a10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions supertd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "bin/main.rs"
[dependencies]
anyhow = "1.0"
clap = {version = "4.1.4", features = ["derive"]}
tokio = { version = "1.37", features = ["full"] }
fbinit = { workspace = true }

td_util = {path = "../td_util"}
Expand Down
12 changes: 11 additions & 1 deletion supertd/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#![forbid(unsafe_code)]

use std::future::Future;

use clap::CommandFactory;
use clap::FromArgMatches;
use clap::Parser;
Expand Down Expand Up @@ -60,7 +62,7 @@ pub fn main(fb: FacebookInit) -> anyhow::Result<()> {
#[cfg(fbcode_build)]
Args::VerifiableMatcher(args) => verifiable_matcher::main(args),
#[cfg(fbcode_build)]
Args::Ranker(args) => ranker::main(args),
Args::Ranker(args) => execute_on_runtime(ranker::main(args)),
#[cfg(fbcode_build)]
Args::Rerun(args) => rerun::main(fb, args),
#[cfg(fbcode_build)]
Expand All @@ -82,6 +84,14 @@ fn get_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

fn execute_on_runtime(f: impl Future<Output = anyhow::Result<()>>) -> anyhow::Result<()> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(f)
}

#[cfg(test)]
mod tests {
use clap::Command;
Expand Down

0 comments on commit 1766a10

Please sign in to comment.