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

Cleanup running containers on the Control-C signal #422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ammernico
Copy link
Collaborator

  • Add the signal feature to tokio to interrupt and handle the Control-C signal in Butido.
  • Add Control-C signal handling into the Orchestrator.
  • Implement Drop on the JobHandle to ensure container cleanup.

This is a working draft pr for testing purposes and still missing some features.

@christophprokop
Copy link
Collaborator

Tested while multiple containers were running on all build hosts.
All containeres were successfully stopped after just a few seconds.
Nice! :)

@christophprokop christophprokop added the prerelease PRs which are merged to staging branch but not in main/master label Oct 1, 2024
@primeos-work primeos-work linked an issue Oct 7, 2024 that may be closed by this pull request
@ammernico ammernico marked this pull request as ready for review October 14, 2024 09:42
- Add the `signal` feature to `tokio` to interrupt and handle the
  Control-C signal in Butido.
- Add Control-C signal handling into the `Orchestrator`.
- Implement `Drop` on the `JobHandle` to ensure container cleanup.

Fixes science-computing#409

Signed-off-by: Nico Steinle <[email protected]>
Copy link
Member

@primeos-work primeos-work left a comment

Choose a reason for hiding this comment

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

I haven't done any testing yet but already found some potential issues in the code.

Comment on lines -210 to +214
drop(self.bar);
drop(self.bar.clone());
Copy link
Member

Choose a reason for hiding this comment

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

Huh, we're cloning and immediately dropping the clone? Shouldn't this be a no-op? I'm a bit surprised that Clippy doesn't catch this.

(By only looking at this context I'm surprised why there is a self.bar related change at all...)

@@ -370,6 +374,36 @@ impl JobHandle {
}
}

impl Drop for JobHandle {
fn drop(&mut self) {
debug!("Cleaning up JobHandle");
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Might be nice/useful to include the job ID (but debugging is obviously optional).

if self.container_id.is_some() {
debug!("Container was already started");
let docker = self.endpoint.docker().clone();
let container_id = self.container_id.take().unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

This isn't pretty and not guaranteed to be safe - please use if let Some(container_id) = self.container_id or something similar instead.

let container_info = container.inspect().await.unwrap();

if container_info.state.running {
debug!("Container is still running, cleaning up...");
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Including the container ID would be nice.


tokio::spawn(async move {
let container = docker.containers().get(&container_id);
let container_info = container.inspect().await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid the unwrap here - I'd probably just log the error (in theory we might occasionally run into such errors when the containers terminate between the if and this inspect).

PS: We want to avoid unwrap() as much as possible in general (but there are of course exceptions where it's fine).

@@ -265,12 +267,26 @@ impl Borrow<ArtifactPath> for ProducedArtifact {

impl<'a> Orchestrator<'a> {
pub async fn run(self, output: &mut Vec<ArtifactPath>) -> Result<HashMap<Uuid, Error>> {
let (results, errors) = self.run_tree().await?;
let token = CancellationToken::new();
let cloned_token = token.clone();
Copy link
Member

Choose a reason for hiding this comment

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

I'd just clone it where needed instead - why use a variable here?

let cloned_token = token.clone();

tokio::spawn(async move {
info!("Received the ctl-c signal, stopping...");
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the code I would assume that this always gets printed - shouldn't you move this below tokio::signal::ctrl_c().await?

Comment on lines -455 to -493
let running_jobs = jobs
.into_iter()
.map(|prep| {
trace!(parent: &run_span, job_uuid = %prep.1.jobdef.job.uuid(), "Creating JobTask");
// the sender is set or we need to use the root sender
let sender = prep
.3
.into_inner()
.unwrap_or_else(|| vec![root_sender.clone()]);
JobTask::new(prep.0, prep.1, sender)
})
.inspect(
|task| trace!(parent: &run_span, job_uuid = %task.jobdef.job.uuid(), "Running job"),
)
.map(|task| {
task.run()
.instrument(tracing::debug_span!(parent: &run_span, "JobTask::run"))
})
.collect::<futures::stream::FuturesUnordered<_>>();
debug!("Built {} jobs", running_jobs.len());

running_jobs
.collect::<Result<()>>()
.instrument(run_span.clone())
.await?;
trace!(parent: &run_span, "All jobs finished");
drop(run_span);

match root_receiver.recv().await {
None => Err(anyhow!("No result received...")),
Some(Ok(results)) => {
let results = results
.into_iter()
.flat_map(|tpl| tpl.1.into_iter())
.map(ProducedArtifact::unpack)
.collect();
Ok((results, HashMap::with_capacity(0)))

tokio::select! {
_ = token.cancelled() => {
anyhow::bail!("Received Control-C signal");
}
r = async {
let running_jobs = jobs
.into_iter()
.map(|prep| {
trace!(parent: &run_span, job_uuid = %prep.1.jobdef.job.uuid(), "Creating JobTask");
// the sender is set or we need to use the root sender
let sender = prep
.3
.into_inner()
.unwrap_or_else(|| vec![root_sender.clone()]);
JobTask::new(prep.0, prep.1, sender)
})
.inspect(
|task| trace!(parent: &run_span, job_uuid = %task.jobdef.job.uuid(), "Running job"),
)
.map(|task| {
task.run()
.instrument(tracing::debug_span!(parent: &run_span, "JobTask::run"))
})
.collect::<futures::stream::FuturesUnordered<_>>();
debug!("Built {} jobs", running_jobs.len());

running_jobs
.collect::<Result<()>>()
.instrument(run_span.clone())
.await?;
trace!(parent: &run_span, "All jobs finished");
drop(run_span);

match root_receiver.recv().await {
None => Err(anyhow!("No result received...")),
Some(Ok(results)) => {
let results = results
.into_iter()
.flat_map(|tpl| tpl.1.into_iter())
.map(ProducedArtifact::unpack)
.collect();
Ok((results, HashMap::with_capacity(0)))
}
Some(Err(errors)) => Ok((vec![], errors)),
}
} => {
r
}
Some(Err(errors)) => Ok((vec![], errors)),
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: I still need to properly review this part.

pub struct JobHandle {
log_dir: Option<PathBuf>,
endpoint: EndpointHandle,
container_id: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

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

This approach seems fine but we never set container_id back to None for finished jobs - it would be more elegant if we could do so (after ensuring that the container has indeed exited but that might already be implemented to check if the job has finished).

@@ -202,12 +206,12 @@ impl JobHandle {
package_name: &package.name,
package_version: &package.version,
log_dir: self.log_dir.as_ref(),
job: self.job,
job: self.job.clone(),
Copy link
Member

Choose a reason for hiding this comment

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

TODO/question: Why do we now need to clone here? (The whole cloning is irritating me a bit in general and can be quite dangerous if the data can diverge)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
prerelease PRs which are merged to staging branch but not in main/master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cleanup job for running containers
3 participants