Skip to content

Commit

Permalink
Eagerly close temporary init script file
Browse files Browse the repository at this point in the history
Various of my qemu instances have hung and I had to kill them
forcefully. As a result, I ended up with 10+ vmtest-initGDllO.sh files
in my /tmp/ folder.
I think we don't have to rely on destructor based clean up in this
instance: after the end of the setup phase the init script should no
longer be accessed via file system path and so we can just unlink it.
This way, no matter how the process dies after that point, the temporary
file will always be gone as well.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and danobi committed Aug 9, 2024
1 parent be4e096 commit 27dd999
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ pub struct Qemu {
rootfs: PathBuf,
arch: String,
mounts: HashMap<String, Mount>,
_init: NamedTempFile,
/// A reference to the temporary file representing the init script.
///
/// This object will be cleared as part of the `run` invocation.
init: Option<NamedTempFile>,
updates: Sender<Output>,
/// Whether or not we are running an image target
image: bool,
Expand Down Expand Up @@ -687,7 +690,7 @@ impl Qemu {
rootfs: target.rootfs.clone(),
arch: target.arch.clone(),
mounts: target.vm.mounts.clone(),
_init: init,
init: Some(init),
updates,
image: target.image.is_some(),
};
Expand Down Expand Up @@ -1070,6 +1073,12 @@ impl Qemu {
return;
}

// We can safely remove our init script temporary file at this
// point and stop relying on destructor based clean up. Doing so
// prevents leakage of the file in case Qemu hangs and has to be
// killed forcefully or is just exited early via Ctrl-C.
drop(self.init.take());

// At this stage qemu should be prompting us with a shell prompt if running
// in interactive mode.
// Once the child has returned, we are done and can exit.
Expand Down Expand Up @@ -1102,7 +1111,7 @@ impl Qemu {
Err(e) => warn!("Failed to wait on child: {}", e),
},
// TODO(dxu): debug why we are getting errors here
Err(e) => debug!("Failed to gracefull quit QEMU: {e}"),
Err(e) => debug!("Failed to gracefully quit QEMU: {e}"),
}
}
}
Expand Down

0 comments on commit 27dd999

Please sign in to comment.