fix(instance): release locks explicitly across all instance operations
This commit is contained in:
1 parent
3479a05e63
commit
7cbfe9cd0d
4 files changed
+31
-14
No files matched your search
@@ -14,7 +14,7 @@ const MANIFEST_VERSION: u32 = 1;
|
||||
|
||||
pub(crate) struct ContentLock {
|
||||
file: File,
|
||||
instance_lock: Option<File>,
|
||||
_instance_lock: Option<crate::instance::runtime::InstanceLock>,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ impl ContentLock {
|
||||
}
|
||||
Ok(Self {
|
||||
file,
|
||||
instance_lock,
|
||||
_instance_lock: instance_lock,
|
||||
path: path.to_owned(),
|
||||
})
|
||||
}
|
||||
@@ -105,14 +105,6 @@ impl Drop for ContentLock {
|
||||
self.path.display()
|
||||
);
|
||||
}
|
||||
if let Some(file) = &self.instance_lock
|
||||
&& let Err(error) = file.unlock()
|
||||
{
|
||||
tracing::warn!(
|
||||
"Could not release instance lock for '{}': {error}",
|
||||
self.path.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -111,10 +111,22 @@ pub fn cleanup_kill_sender(name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct InstanceLock(std::fs::File);
|
||||
|
||||
impl Drop for InstanceLock {
|
||||
fn drop(&mut self) {
|
||||
// A concurrent fork can retain a descriptor until exec; closing our copy
|
||||
// alone would leave the instance locked during that interval.
|
||||
if let Err(error) = self.0.unlock() {
|
||||
tracing::warn!("Could not release instance lock: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn lock_instance(
|
||||
root: &std::path::Path,
|
||||
name: &str,
|
||||
) -> Result<std::fs::File, crate::instance::manager::InstanceError> {
|
||||
) -> Result<InstanceLock, crate::instance::manager::InstanceError> {
|
||||
crate::instance::manager::validate_name(name)?;
|
||||
let locks = root.join(".rmcl-locks");
|
||||
std::fs::create_dir_all(&locks)?;
|
||||
@@ -125,7 +137,7 @@ pub(crate) fn lock_instance(
|
||||
.write(true)
|
||||
.open(locks.join(format!("{name}.lock")))?;
|
||||
match file.try_lock() {
|
||||
Ok(()) => Ok(file),
|
||||
Ok(()) => Ok(InstanceLock(file)),
|
||||
Err(std::fs::TryLockError::WouldBlock) => Err(
|
||||
crate::instance::manager::InstanceError::InstanceRunning(name.to_owned()),
|
||||
),
|
||||
|
||||
@@ -22,12 +22,11 @@ fn content_lock_excludes_independent_handles_until_drop() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_guard_releases_locks_with_duplicated_handles() {
|
||||
fn dropping_guard_releases_the_content_lock_with_a_duplicated_handle() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("instance/rmcl/content/manifest.json");
|
||||
let lock = ContentLock::acquire(&path).unwrap();
|
||||
let _manifest_handle = lock.file.try_clone().unwrap();
|
||||
let _instance_handle = lock.instance_lock.as_ref().unwrap().try_clone().unwrap();
|
||||
drop(lock);
|
||||
ContentLock::acquire(&path).unwrap();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,20 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn dropping_an_instance_lock_releases_it_even_with_a_duplicated_descriptor() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let lock = lock_instance(temp.path(), "duplicated").unwrap();
|
||||
let duplicate = lock.0.try_clone().unwrap();
|
||||
drop(lock);
|
||||
let next = lock_instance(temp.path(), "duplicated");
|
||||
drop(duplicate);
|
||||
assert!(
|
||||
next.is_ok(),
|
||||
"A duplicate descriptor retained the released instance lock"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_and_get_state() {
|
||||
set_state("run_test_1", RunState::Starting);
|
||||
|
||||
Reference in new issue
Block a user