diff --git a/vrtManager/connection.py b/vrtManager/connection.py index 85af8465..b2091051 100644 --- a/vrtManager/connection.py +++ b/vrtManager/connection.py @@ -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 diff --git a/vrtManager/tests/test_connection.py b/vrtManager/tests/test_connection.py index 077b1392..18401bf2 100644 --- a/vrtManager/tests/test_connection.py +++ b/vrtManager/tests/test_connection.py @@ -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()