Skip to content

Commit a3842cb

Browse files
jnsnowphilmd
authored andcommitted
python/machine.py: Prohibit multiple shutdown() calls
If the VM is not launched, don't try to shut it down. As a change, _post_shutdown now unconditionally also calls _early_cleanup in order to offer comprehensive object cleanup in failure cases. As a courtesy, treat it as a NOP instead of rejecting it as an error. This is slightly nicer for acceptance tests where vm.shutdown() is issued unconditionally in tearDown callbacks. Signed-off-by: John Snow <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
1 parent 3a7d64b commit a3842cb

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

python/qemu/machine.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ def _post_launch(self):
294294
self._qmp.accept()
295295

296296
def _post_shutdown(self):
297+
"""
298+
Called to cleanup the VM instance after the process has exited.
299+
May also be called after a failed launch.
300+
"""
301+
# Comprehensive reset for the failed launch case:
302+
self._early_cleanup()
303+
297304
if self._qmp:
298305
self._qmp.close()
299306
self._qmp = None
@@ -339,7 +346,7 @@ def launch(self):
339346
self._launch()
340347
self._launched = True
341348
except:
342-
self.shutdown()
349+
self._post_shutdown()
343350

344351
LOG.debug('Error launching VM')
345352
if self._qemu_full_args:
@@ -368,6 +375,8 @@ def _launch(self):
368375
def _early_cleanup(self) -> None:
369376
"""
370377
Perform any cleanup that needs to happen before the VM exits.
378+
379+
Called additionally by _post_shutdown for comprehensive cleanup.
371380
"""
372381
# If we keep the console socket open, we may deadlock waiting
373382
# for QEMU to exit, while QEMU is waiting for the socket to
@@ -388,6 +397,9 @@ def shutdown(self, has_quit=False, hard=False):
388397
"""
389398
Terminate the VM and clean up
390399
"""
400+
if not self._launched:
401+
return
402+
391403
self._early_cleanup()
392404

393405
if self.is_running():

0 commit comments

Comments
 (0)