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

Only insert executable into cache once it starts #528

Merged
merged 2 commits into from
Aug 13, 2024
Merged
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
34 changes: 20 additions & 14 deletions auraed/src/cells/cell_service/executables/executables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,30 @@ impl Executables {
}

let executable_name = executable_spec.name.clone();
// `or_insert` will always insert as we've already assured ourselves that the key does not exist.
let executable = self
.cache
.entry(executable_name.clone())
.or_insert_with(|| Executable::new(executable_spec));

// TODO: if we fail to start, the exe remains in the cache and start cannot be called again
// solving ^^ was a borrow checker fight and I (future-highway) lost this round.
let mut executable = Executable::new(executable_spec);

// start the exe before we add it to the cache, as otherwise a failure leads to the
// executable remaining in the cache and start cannot be called again.
executable.start().map_err(|e| {
ExecutablesError::FailedToStartExecutable {
executable_name,
executable_name: executable_name.clone(),
source: e,
}
})?;

Ok(executable)
// `or_insert` will always insert as we've already assured ourselves that the key does not
// exist.
let inserted_executable =
self.cache.entry(executable_name).or_insert_with(|| executable);

Ok(inserted_executable)
}

pub fn get(&self, executable_name: &ExecutableName) -> Result<&Executable> {
let Some(executable) = self.cache.get(executable_name) else {
return Err(ExecutablesError::ExecutableNotFound { executable_name: executable_name.clone() });
return Err(ExecutablesError::ExecutableNotFound {
executable_name: executable_name.clone(),
});
};
Ok(executable)
}
Expand All @@ -72,7 +75,9 @@ impl Executables {
executable_name: &ExecutableName,
) -> Result<ExitStatus> {
let Some(executable) = self.cache.get_mut(executable_name) else {
return Err(ExecutablesError::ExecutableNotFound { executable_name: executable_name.clone() });
return Err(ExecutablesError::ExecutableNotFound {
executable_name: executable_name.clone(),
});
};

let exit_status = executable.kill().await.map_err(|e| {
Expand All @@ -84,7 +89,8 @@ impl Executables {

let Some(exit_status) = exit_status else {
// Exes that never started return None
let executable = self.cache.remove(executable_name).expect("exe in cache");
let executable =
self.cache.remove(executable_name).expect("exe in cache");
return Err(ExecutablesError::ExecutableNotFound {
executable_name: executable.name,
});
Expand Down Expand Up @@ -112,4 +118,4 @@ impl Executables {
let _ = self.cache.remove(&name);
}
}
}
}
Loading