diff --git a/console/novncd b/console/novncd index 8b7b2f50..49d78e17 100755 --- a/console/novncd +++ b/console/novncd @@ -108,13 +108,19 @@ def end_when_refused(instance, user_id, tsock, ended): """Close the session's VNC socket, which ends it, once its user may no longer open the console (as the serial console does).""" while not ended.wait(ACCESS_CHECK): - if not may_still_open(user_id, instance): + try: + if may_still_open(user_id, instance): + continue logging.info("VNC console of VM %s closed: its user may no longer open it", instance.name) - try: - tsock.shutdown(socket.SHUT_RDWR) - except OSError: - pass # the session ended meanwhile - return + except Exception as e: + # unchecked, the session would go on whoever loses the VM meanwhile + logging.warning("VNC console of VM %s closed: its user's access could not be checked: %s", + instance.name, safe_text(e)) + try: + tsock.shutdown(socket.SHUT_RDWR) + except OSError: + pass # the session ended meanwhile + return class ConsoleClient: diff --git a/console/tests/test_novncd.py b/console/tests/test_novncd.py index ff7ec535..33d042ff 100644 --- a/console/tests/test_novncd.py +++ b/console/tests/test_novncd.py @@ -16,6 +16,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission from django.core.signing import TimestampSigner from django.conf import settings +from django.db import OperationalError from django.test import TestCase, override_settings from instances.models import Instance from libvirt import libvirtError @@ -298,6 +299,15 @@ class NovncdDaemonLogicTestCase(TestCase): may_still_open.assert_called_with(7, vm) tsock.shutdown.assert_called_once_with(novncd_mod.socket.SHUT_RDWR) + def test_a_vnc_session_whose_access_cannot_be_checked_ends(self): + tsock, ended = MagicMock(), MagicMock() + ended.wait.return_value = False + with patch.object(novncd_mod, "may_still_open", side_effect=OperationalError("database is locked")), \ + self.assertLogs(level="WARNING") as logs: + novncd_mod.end_when_refused(MagicMock(), 7, tsock, ended) + tsock.shutdown.assert_called_once_with(novncd_mod.socket.SHUT_RDWR) + self.assertIn("database is locked", logs.output[0]) + def test_a_token_of_a_serial_console_does_not_open_a_vnc_console(self): user = get_user_model().objects.create_user("vnc-owner", password="pw") compute = Compute.objects.create(name="novnc-type-compute", hostname="127.0.0.1", login="root", password="", type=1)