1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): an open VM page follows its VM's removal on the host

A VM undefined outside the panel (virsh, virt-manager) while its page was
open made the state and statistics polls answer 500 every 5 seconds, each
an error in the panel's log, and the page stayed as it was. A page read of
an object the host no longer has is now 404, and the VM page reloads on a
404 of its state: it then goes to the VM list with "no longer on its host",
or shows that the VM is not found.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 21:22:33 +03:00
parent 373760f2c5
commit a2f4d3fe06
4 changed files with 45 additions and 14 deletions

View file

@ -1,17 +1,30 @@
// The VM page reloads when the VM's state changes (instance.html, #vm-summary).
// The VM page reloads when the VM's state changes (instance.html, #vm-summary),
// or when the VM is gone: the page then says so.
backgroundJobRunning = false;
// not "status": that is window.status, which the browser may not keep
var vm_summary = document.getElementById("vm-summary");
var vm_status = Number(vm_summary.dataset.status);
var host_lost = document.getElementById("vm-host-lost");
var status_poll = pollWhileVisible(5000, function () {
// a failed read (a host that does not answer, a session that ended) leaves the
// page as it was: it says so until a read is answered
return $.getJSON(vm_summary.dataset.statusUrl).fail(function () { host_lost.hidden = false; });
}, function get_status(data) {
host_lost.hidden = true;
function reloadWhenFree() {
// the page in use (a dialog open, a field in use, an XML edit not saved): the reload waits (poll.js)
if (data['status'] != vm_status && !backgroundJobRunning && !pageInUse()) {
if (!backgroundJobRunning && !pageInUse()) {
window.location.reload();
}
}
var status_poll = pollWhileVisible(5000, function () {
return $.getJSON(vm_summary.dataset.statusUrl).fail(function (xhr) {
// a failed read (a host that does not answer, a session that ended, the VM gone while
// the page is in use) leaves the page as it was: it says so until a read is answered
host_lost.hidden = false;
if (xhr.status == 404) {
reloadWhenFree();
}
});
}, function get_status(data) {
host_lost.hidden = true;
if (data['status'] != vm_status) {
reloadWhenFree();
}
});

View file

@ -158,5 +158,8 @@ class ExceptionMiddleware:
if request.method not in ("GET", "HEAD"):
# a refused form: back to the page it was on, with the reason
return redirect(same_origin_referer(request, "/"))
if exception.get_error_code() in NOT_FOUND_CODES:
# removed on the host, as a VM undefined in virsh: not a fault of the panel
return render(request, "404.html", {"request_path": request.path}, status=404)
return render(request, "500.html", {"libvirt_error": True}, status=500)
# TODO: check connecting to host via VPN

View file

@ -70,6 +70,13 @@ class ConnectionErrorPageTests(TestCase):
self.assertIn(HOST_DETAIL.encode(), response.content)
self.assertTrue(messages)
def test_a_read_of_a_vm_removed_on_its_host_is_not_found(self):
# a VM page polls its state: the VM undefined in virsh answers 404, not a panel fault
error = libvirt_error(libvirt.VIR_FROM_QEMU, "Domain not found", libvirt.VIR_ERR_NO_DOMAIN)
response, _ = self.handle(self.admin, error, path="/instances/1/status/", method="get")
self.assertEqual(response.status_code, 404)
self.assertIn(b"Page not found", response.content)
def test_administrators_see_connection_details(self):
for user in (self.admin, self.viewer):
with self.subTest(user=user.username):

View file

@ -43,28 +43,36 @@ class PollTestCase(unittest.TestCase):
self.assertIn("pollWhileVisible(", source)
@unittest.skipUnless(shutil.which("node"), "node is not installed")
def test_the_vm_page_reloads_only_when_the_state_changes_and_it_is_not_in_use(self):
def test_the_vm_page_reloads_only_when_the_state_changes_or_the_vm_is_gone_and_it_is_not_in_use(self):
# every failed read shows the page's notice; another one than 404 leaves the page
# "status" is a property of the browser's window: a page variable of
# that name holds what the browser makes of it (a string, or nothing)
script = (BASE / "static/js/instance_status.js").read_text()
js = """
const vm = require("vm");
const results = {};
for (const [answer, inUse] of [[1, false], [5, false], [5, true]]) {
for (const [answer, inUse] of [[1, false], [5, false], [5, true], [404, false], [404, true], [500, false]]) {
let reloads = 0;
// the summary and the notice a failed read shows
const element = {dataset: {status: "1", statusUrl: "/instances/1/status/"}, hidden: true};
const page = {
pollWhileVisible: (interval, request, onData) => { onData({status: answer}); return {stop() {}}; },
pollWhileVisible: (interval, request, onData) => {
if (answer >= 400) { request(); } else { onData({status: answer}); }
return {stop() {}};
},
pageInUse: () => inUse,
$: () => ({submit() {}}),
document: {getElementById: () => ({dataset: {status: "1", statusUrl: "/instances/1/status/"}})},
$: {getJSON: () => ({fail: (f) => f({status: answer})})},
document: {getElementById: () => element},
};
page.window = {location: {reload: () => reloads++}};
Object.defineProperty(page, "status", {get: () => "", set: () => {}});
vm.runInNewContext(SCRIPT, vm.createContext(page));
results[answer + (inUse ? " in use" : "")] = reloads;
if (answer >= 400) { results[answer + " notice"] = !element.hidden; }
}
console.log(JSON.stringify(results));
""".replace("SCRIPT", json.dumps(script))
out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
self.assertEqual(out.returncode, 0, out.stderr)
self.assertEqual(json.loads(out.stdout), {"1": 0, "5": 1, "5 in use": 0})
self.assertEqual(json.loads(out.stdout), {"1": 0, "5": 1, "5 in use": 0, "404": 1, "404 in use": 0,
"404 notice": True, "500": 0, "500 notice": True})