diff --git a/src/platform/backends/hyperv/hyperv_virtual_machine.cpp b/src/platform/backends/hyperv/hyperv_virtual_machine.cpp index d8370ba0ff..2439d1b13d 100644 --- a/src/platform/backends/hyperv/hyperv_virtual_machine.cpp +++ b/src/platform/backends/hyperv/hyperv_virtual_machine.cpp @@ -387,7 +387,6 @@ void mp::HyperVVirtualMachine::shutdown(ShutdownPolicy shutdown_policy) state_wait.wait(lock, [this] { return shutdown_while_starting; }); } - ip = std::nullopt; update_state(); } @@ -440,6 +439,16 @@ void mp::HyperVVirtualMachine::ensure_vm_is_running() void mp::HyperVVirtualMachine::update_state() { + // Invalidate the management IP address on state update. + if (current_state() == VirtualMachine::State::running) + { + // Cached IPs become stale when the guest is restarted from within. By resetting them here + // we at least cover multipass's restart initiatives, which include state updates. + mpl::log(mpl::Level::debug, + vm_name, + "Invalidating cached mgmt IP address upon state update"); + management_ip = std::nullopt; + } monitor->persist_state_for(vm_name, state); } @@ -455,7 +464,7 @@ std::string mp::HyperVVirtualMachine::ssh_username() std::string mp::HyperVVirtualMachine::management_ipv4() { - if (!ip) + if (!management_ip) { // Not using cached SSH session for this because a) the underlying functions do not // guarantee constness; b) we endure the penalty of creating a new session only when we @@ -463,9 +472,9 @@ std::string mp::HyperVVirtualMachine::management_ipv4() auto result = remote_ip(VirtualMachine::ssh_hostname(), ssh_port(), ssh_username(), key_provider); if (result) - ip.emplace(result.value()); + management_ip.emplace(result.value()); } - return ip ? ip.value().as_string() : "UNKNOWN"; + return management_ip ? management_ip.value().as_string() : "UNKNOWN"; } std::string mp::HyperVVirtualMachine::ipv6() diff --git a/src/platform/backends/hyperv/hyperv_virtual_machine.h b/src/platform/backends/hyperv/hyperv_virtual_machine.h index 450575aec4..c28f8dc727 100644 --- a/src/platform/backends/hyperv/hyperv_virtual_machine.h +++ b/src/platform/backends/hyperv/hyperv_virtual_machine.h @@ -95,7 +95,6 @@ class HyperVVirtualMachine final : public BaseVirtualMachine VirtualMachineDescription desc; // TODO we should probably keep this in the base class instead const QString name; - std::optional ip; std::unique_ptr power_shell; VMStatusMonitor* monitor; bool update_suspend_status{true};