Skip to content

Commit 27dd999

Browse files
d-e-s-odanobi
authored andcommitted
Eagerly close temporary init script file
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>
1 parent be4e096 commit 27dd999

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/qemu.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ pub struct Qemu {
6565
rootfs: PathBuf,
6666
arch: String,
6767
mounts: HashMap<String, Mount>,
68-
_init: NamedTempFile,
68+
/// A reference to the temporary file representing the init script.
69+
///
70+
/// This object will be cleared as part of the `run` invocation.
71+
init: Option<NamedTempFile>,
6972
updates: Sender<Output>,
7073
/// Whether or not we are running an image target
7174
image: bool,
@@ -687,7 +690,7 @@ impl Qemu {
687690
rootfs: target.rootfs.clone(),
688691
arch: target.arch.clone(),
689692
mounts: target.vm.mounts.clone(),
690-
_init: init,
693+
init: Some(init),
691694
updates,
692695
image: target.image.is_some(),
693696
};
@@ -1070,6 +1073,12 @@ impl Qemu {
10701073
return;
10711074
}
10721075

1076+
// We can safely remove our init script temporary file at this
1077+
// point and stop relying on destructor based clean up. Doing so
1078+
// prevents leakage of the file in case Qemu hangs and has to be
1079+
// killed forcefully or is just exited early via Ctrl-C.
1080+
drop(self.init.take());
1081+
10731082
// At this stage qemu should be prompting us with a shell prompt if running
10741083
// in interactive mode.
10751084
// Once the child has returned, we are done and can exit.
@@ -1102,7 +1111,7 @@ impl Qemu {
11021111
Err(e) => warn!("Failed to wait on child: {}", e),
11031112
},
11041113
// TODO(dxu): debug why we are getting errors here
1105-
Err(e) => debug!("Failed to gracefull quit QEMU: {e}"),
1114+
Err(e) => debug!("Failed to gracefully quit QEMU: {e}"),
11061115
}
11071116
}
11081117
}

0 commit comments

Comments
 (0)