mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
fix(vrtManager): reconnect after a host's storage or network daemon restarts
On a host with libvirt's modular daemons, a restart of virtstoraged or virtnetworkd (a package update, for one) left the panel's connection to virtproxyd open and alive, but every later storage or network call on it failed with "internal error: client socket is closed". Nothing replaced it: the storage and network pages showed the error and the VM page gave 500 until the panel was restarted, and a clone running then waited in recovery for up to an hour, holding its VM. libvirt reports that error as an RPC internal error and does not say on which connection, so the error handler now has the process retire its open connections at the next get_connection: each opens again when next asked. A retired connection stays with the threads that use them, its close callback unregistered so that it closes once they are done; a connection that failed or is opening keeps its retry delay. On compute1 the pages answered again from the second request on, and the clone's job failed at its first recovery check, 30 s later, freeing its VM. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
aefe551450
commit
b70bab2144
2 changed files with 65 additions and 4 deletions
|
|
@ -52,10 +52,24 @@ CLOSE_REASONS = {
|
|||
SSH_COMMAND = os.path.join(os.path.dirname(os.path.abspath(__file__)), "libvirt-ssh")
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
# libvirt prints each error on stderr without saying where it came from; every
|
||||
# process that talks to libvirt imports this module. The errors are raised to
|
||||
# the callers, which handle or report them.
|
||||
libvirt.registerErrorHandler(lambda context, error: None, None)
|
||||
|
||||
|
||||
def _on_libvirt_error(context, error):
|
||||
"""libvirt prints each error on stderr without saying where it came from;
|
||||
every process that talks to libvirt imports this module. The errors are
|
||||
raised to the callers, which handle or report them.
|
||||
|
||||
A daemon behind the host's virtproxyd (virtstoraged, virtnetworkd, ...)
|
||||
that restarts leaves the connection open and alive, but every later call
|
||||
to it fails with "client socket is closed". libvirt reports that as an
|
||||
RPC internal error and does not say on which connection, so the process
|
||||
opens new ones."""
|
||||
code, domain = error[0], error[1]
|
||||
if code == libvirt.VIR_ERR_INTERNAL_ERROR and domain == libvirt.VIR_FROM_RPC:
|
||||
connection_manager.reopen = True
|
||||
|
||||
|
||||
libvirt.registerErrorHandler(_on_libvirt_error, None)
|
||||
|
||||
|
||||
class wvmEventLoop(threading.Thread):
|
||||
|
|
@ -324,6 +338,17 @@ class wvmConnection(object):
|
|||
def __connect_socket(self):
|
||||
return libvirt.open("qemu:///system")
|
||||
|
||||
def retire(self):
|
||||
"""Open a new connection when next asked, and leave the current one to
|
||||
the threads using it: it closes when they are done. A connection that
|
||||
failed or is opening keeps its state (connection_manager.reopen)."""
|
||||
with self.connection_state_lock:
|
||||
connection, self.connection = self.connection, None
|
||||
if connection is not None:
|
||||
# libvirt holds the callback, and through it the connection
|
||||
with contextlib.suppress(libvirtError):
|
||||
connection.unregisterCloseCallback()
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
closes the connection (if it is active)
|
||||
|
|
@ -381,6 +406,7 @@ class wvmConnectionManager(object):
|
|||
self._connections_lock = threading.RLock() # the dict only: connecting runs outside it
|
||||
self._inherited = []
|
||||
self._host_requests = {} # host: the web requests using it, whatever the login (HOST_REQUESTS)
|
||||
self.reopen = False # an RPC error (_on_libvirt_error): the next get_connection retires the open connections
|
||||
|
||||
def forget_connections(self):
|
||||
"""For a forked child: stop using the inherited connections (the
|
||||
|
|
@ -414,6 +440,11 @@ class wvmConnectionManager(object):
|
|||
passwd = str(passwd) if passwd is not None else None
|
||||
|
||||
with self._connections_lock:
|
||||
if self.reopen:
|
||||
self.reopen = False
|
||||
for entries in self._connections.values():
|
||||
for entry in entries:
|
||||
entry.retire()
|
||||
connection = self._search_connection(host, login, passwd, conn)
|
||||
if connection is None:
|
||||
# Only register it here: connecting under the global lock
|
||||
|
|
|
|||
|
|
@ -177,6 +177,36 @@ class BoundedOpenTestCase(unittest.TestCase):
|
|||
self.assertIsNone(entry.connection)
|
||||
self.assertEqual(entry.last_error, {"text": "Connection closed (keepalive timeout)", "values": {}})
|
||||
|
||||
def test_an_rpc_error_opens_new_connections(self):
|
||||
self.release.set()
|
||||
old, new = alive_connection(), alive_connection()
|
||||
with self.open_auth(old):
|
||||
self.get()
|
||||
with patch("vrtManager.connection.connection_manager", self.manager):
|
||||
# another error keeps the connection
|
||||
connection._on_libvirt_error(None, (libvirt.VIR_ERR_NO_DOMAIN, libvirt.VIR_FROM_QEMU, "no domain", 2))
|
||||
self.assertIs(self.get()[0], old)
|
||||
# a restarted virtstoraged: the connection is alive, its storage calls fail for good
|
||||
connection._on_libvirt_error(None, (libvirt.VIR_ERR_INTERNAL_ERROR, libvirt.VIR_FROM_RPC,
|
||||
"internal error: client socket is closed", 2))
|
||||
with self.open_auth(new):
|
||||
self.assertIs(self.get()[0], new)
|
||||
self.assertIs(self.get()[0], new)
|
||||
old.close.assert_not_called() # a thread may still use it
|
||||
old.unregisterCloseCallback.assert_called_once() # else libvirt keeps it
|
||||
|
||||
def test_an_rpc_error_keeps_a_failed_hosts_retry_delay(self):
|
||||
self.release.set()
|
||||
with self.open_auth(libvirt.libvirtError("unable to connect")):
|
||||
self.get()
|
||||
with patch("vrtManager.connection.connection_manager", self.manager):
|
||||
connection._on_libvirt_error(None, (libvirt.VIR_ERR_INTERNAL_ERROR, libvirt.VIR_FROM_RPC,
|
||||
"internal error: client socket is closed", 2))
|
||||
with self.open_auth(alive_connection()):
|
||||
result, _took = self.get()
|
||||
self.assertIsInstance(result, libvirt.libvirtError) # still within RETRY_AFTER
|
||||
self.assertEqual(self.calls, 1)
|
||||
|
||||
def test_the_log_has_each_change_of_a_host_not_each_retry(self):
|
||||
self.release.set()
|
||||
conn = alive_connection()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue