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

agent: use a timeout when shutting down the runtime #1334

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions crates/agent/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ pub struct AgentNotification {
}

/// Handler is the principal trait implemented by the various task-specific
/// event handlers that the agent runs.
/// event handlers that the agent runs. They need to be `Send` because we
/// spawn the handler invocations on a multithreaded runtime.
#[async_trait::async_trait]
pub trait Handler {
pub trait Handler: Send {
async fn handle(&mut self, pg_pool: &sqlx::PgPool) -> anyhow::Result<HandlerStatus>;

fn table_name(&self) -> &'static str;
Expand Down
16 changes: 14 additions & 2 deletions crates/agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ struct Args {
allow_local: bool,
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
fn main() -> Result<(), anyhow::Error> {
// Use reasonable defaults for printing structured logs to stderr.
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
Expand All @@ -59,6 +58,19 @@ async fn main() -> Result<(), anyhow::Error> {
let args = Args::parse();
tracing::info!(?args, "started!");

let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;

let task = runtime.spawn(async move { async_main(args).await });
let result = runtime.block_on(task);

tracing::info!(?result, "main function completed, shutting down runtime");
runtime.shutdown_timeout(std::time::Duration::from_secs(5));
result?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no action: it would be really nice to get a trace of what tasks still haven't stopped after this timeout. Was that feasible in your investigations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, what I found was the dump function, which can give you a nice stack trace for each running task. But it comes with quite a few caveats, which made me pretty hesitant to start using it without further investigation and testing.

Probably the biggest caveat is that it can't actually produce a dump if any tasks are synchronously deadlocked. I'm unsure of what this means for i/o tasks that are using blocking threads. But dumps are also linux-only, and require enabling "unstable" features of tokio (I'm not 100% clear on the implications of that).

IMO it's potentially still worth investigating, though it might be better to wait for more progress on the stabilization. They have a stabilization issue in the tokio repo with a sizeable TODO list.

That said, if all we want is just a list of still-running tasks, the tokio-console does at least provide that. The console still requires enabling "unstable" features, but otherwise seems to have fewer caveats, but also can't provide stack traces. I'd opted not to add it, mostly due to apprehension over enabling the unstable features. But could look into the unstable feature more, if you reckon it's worth the time (I was just trying to time-box my investigation to an hour)

}

async fn async_main(args: Args) -> Result<(), anyhow::Error> {
let bindir = std::fs::canonicalize(args.bindir)
.context("canonicalize --bin-dir")?
.into_os_string()
Expand Down