Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Forcefully shut down the runtime before existing the main task#3378

Merged
chkeita merged 1 commit into
microsoft:mainfrom
chkeita:bug_fix
Aug 2, 2023
Merged

Forcefully shut down the runtime before existing the main task#3378
chkeita merged 1 commit into
microsoft:mainfrom
chkeita:bug_fix

Conversation

@chkeita

@chkeita chkeita commented Aug 2, 2023

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

The default behavior or the runtime is wait for all spawned work to be done before exiting. This was causing the task to hang when exiting.
This change adds a force shutdown at the end of the main function of the task.

closes #3377

@chkeita chkeita requested review from Porges and tevoinea August 2, 2023 01:54

@tevoinea tevoinea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to help me better understand:

  • We'll never call rt.shutdown_background() until let result = rt.block_on(run(matches)); completes
  • run(matches) will only complete when one of the futures complete from this select! macro
    let result = tokio::select! {
    result = config.run() => result,
    // Ignore this task if it returns due to a querying error.
    Ok(oom) = check_oom => {
    // Convert the OOM notification to an error, so we can log it below.
    let err = format_err!("out of memory: {} bytes available, {} required", oom.available_bytes, oom.min_bytes);
    Err(err)
    },
    _shutdown = shutdown_listener => {
    Ok(())
    }
    };
  • select! claims to cancel the remaining branches

Waits on multiple concurrent branches, returning when the first branch completes, cancelling the remaining branches.

Does that mean the cancellation from select! happens in the background? And that prior to this PR, we wait for the cancellation to complete when we drop the runtime?

@chkeita

chkeita commented Aug 2, 2023

Copy link
Copy Markdown
Contributor Author

Does that mean the cancellation from select! happens in the background? And that prior to this PR, we wait for the cancellation to complete when we drop the runtime?

This is not necessarily a problem with select!. The tokio docs says it waits on all the spawned work. Select will cancel everything related the task itself But something was spawned inside that task of it becomes an independent branch that is not waited on.

@chkeita chkeita merged commit 4c6fb7a into microsoft:main Aug 2, 2023
AdamL-Microsoft added a commit that referenced this pull request Aug 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tasks hangs when shutting down

3 participants