Skip to content

Commit

Permalink
[virtual machine] reverted back the commit "move the lock into the ch…
Browse files Browse the repository at this point in the history
…eck_state_for_shutdown function since the state variable is the only one needs to be protected, also convert unique_lock to scoped_lock because manual unlock/ lock is no longer needed."
  • Loading branch information
georgeliao committed Jun 26, 2024
1 parent c2384d6 commit 1607cb7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/platform/backends/qemu/qemu_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ void mp::QemuVirtualMachine::start()

void mp::QemuVirtualMachine::shutdown(const bool force)
{
std::unique_lock<std::mutex> lock{state_mutex};

try
{
check_state_for_shutdown(force);
Expand All @@ -363,6 +365,7 @@ void mp::QemuVirtualMachine::shutdown(const bool force)
if (vm_process)
{
mpl::log(mpl::Level::info, vm_name, "Killing process");
lock.unlock();
vm_process->kill();
vm_process->wait_for_finished(timeout);
}
Expand All @@ -371,7 +374,6 @@ void mp::QemuVirtualMachine::shutdown(const bool force)
mpl::log(mpl::Level::debug, vm_name, "No process to kill");
}

std::scoped_lock lock{state_mutex};
if (state == State::suspended || mp::backend::instance_image_has_snapshot(desc.image.image_path, suspend_tag))
{
mpl::log(mpl::Level::info, vm_name, "Deleting suspend image");
Expand All @@ -382,6 +384,8 @@ void mp::QemuVirtualMachine::shutdown(const bool force)
}
else
{
lock.unlock();

drop_ssh_session();

if (vm_process && vm_process->running())
Expand Down
2 changes: 1 addition & 1 deletion src/platform/backends/shared/base_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void mp::BaseVirtualMachine::check_state_for_shutdown(const bool force)
{
const std::string force_statement{"Use --force to override."};

std::scoped_lock lock{state_mutex};
// A mutex should already be locked by the caller here
if (state == State::off || state == State::stopped)
{
throw VMStateIdempotentException{"Ignoring shutdown since instance is already stopped."};
Expand Down

0 comments on commit 1607cb7

Please sign in to comment.