Skip to content

CloudRuntimeException on failing VM restart #14840

In review

Description

@DaanHoogland

Hello everyone, with the help of Claude i got this summary:

Observed behavior

When VM HA triggers a restart attempt that fails with a CloudRuntimeException (e.g. QEMU failing to acquire a qcow2 write lock on shared NFS storage), the HA manager incorrectly marks the work as Done at attempt 2/5, logging:

VM i-2-1483-VM has been changed. Current State = Stopped Previous State = Stopped
last updated = 20 previous updated = 17
Completed work HAWork[17-HA-1483-Stopped-Scheduled]. Took 2/5 attempts.

The VM never restarts despite remaining attempts being available.


Root cause

Step 1 — processWork() fetches the VM once and never refreshes it

HighAvailabilityManagerImpl.processWork() fetches the VM at the top of the method before calling restart():

final VMInstanceVO vm = _instanceDao.findById(work.getInstanceId()); // update_count = 17

This reference is never refreshed inside processWork().

Step 2 — a failed start attempt increments update_count three times

restart() → startVm() → startVirtualMachineForHA() → advanceStart() → orchestrateStart()

Inside orchestrateStart(), a single failed attempt produces exactly three stateTransitTo() calls, each invoking VMInstanceDaoImpl.updateState() → incrUpdated():

Transition Event Counter
Stopped → Starting StartRequested 17 → 18
Starting → Starting OperationRetry (per-host loop) 18 → 19
Starting → Stopped OperationFailed (outer finally) 19 → 20

All three increments are written to the DB. The vm object held by processWork() sees none of them.

Step 3 — CloudRuntimeException bypasses the typed catch blocks in restart()

The QEMU write-lock failure surfaces as a CloudRuntimeException. This is not caught by any of the typed catch blocks inside restart():

} catch (final InsufficientCapacityException e) { ... }
} catch (final ResourceUnavailableException e) { ... }
} catch (ConcurrentOperationException e) { ... }
} catch (OperationTimedoutException e) { ... }

It also bypasses the bottom of restart(), which would have correctly re-fetched the VM:

// never reached in this path:
vm = _itMgr.findById(vm.getId());
work.setUpdateTime(vm.getUpdated());  // would have captured 20

Instead it propagates up to processWork()'s generic catch (Exception e) block:

} catch (Exception e) {
    long nextTime = getRescheduleTime(wt);
    rescheduleWork(work, nextTime);
    if (vm != null) {
        work.setUpdateTime(vm.getUpdated());  // stale top-of-method object → 17
        work.setPreviousState(vm.getState()); // Stopped
    }
}

work.setUpdateTime(17) is written to the HA work record and the work is rescheduled.

Step 4 — the guard at attempt 2 fires on the stale snapshot

At attempt 2, restart() fetches a fresh VM from the DB, getting update_count = 20, then immediately hits:

if (vm.getState() != work.getPreviousState() || vm.getUpdated() != work.getUpdateTime()) {
    logger.info("VM {} has been changed. ...", vm, vm.getState(), work.getPreviousState(),
            vm.getUpdated(), work.getUpdateTime());
    return null;
}
  • States match: Stopped == Stopped ✓
  • Counters do not: 20 != 17 → condition is true → return null

Back in processWork(), a null return means "work completed":

if (nextTime == null) {
    logger.info("Completed work {}. Took {}/{} attempts.", work, work.getTimesTried() + 1, _maxRetries);
    work.setStep(Step.Done);
}

All remaining attempts are silently abandoned.


Why the normal retry path does not have this problem

When startVm() fails with one of the typed declared exceptions, the exception is caught inside restart() and execution falls through to the bottom:

vm = _itMgr.findById(vm.getId());       // fresh DB fetch → update_count = 20
work.setUpdateTime(vm.getUpdated());    // correctly snapshots 20
work.setPreviousState(vm.getState());
return (System.currentTimeMillis() >> 10) + _restartRetryInterval;

This correctly re-fetches the VM before snapshotting the counter, so attempt 2's guard sees 20 == 20 and passes. The bug is specific to any unchecked exception not in the typed catch list of restart().


Proposed fix

The catch (Exception e) block in processWork() should re-fetch the VM from the DB before snapshotting the counter, mirroring exactly what the bottom of restart() already does correctly:

} catch (Exception e) {
    logger.warn("Encountered unhandled exception during HA process, reschedule work {}", work, e);
    long nextTime = getRescheduleTime(wt);
    rescheduleWork(work, nextTime);
    if (vm != null) {
        VMInstanceVO reloaded = _instanceDao.findById(vm.getId());
        if (reloaded != null) {
            work.setUpdateTime(reloaded.getUpdated());
            work.setPreviousState(reloaded.getState());
        }
    }
}

Affected scenario

Any start failure that surfaces as CloudRuntimeException rather than one of the four typed exceptions declared in startVm()'s signature. Confirmed trigger: QEMU write-lock contention on shared NFS/SAN storage when the source host has not been fenced and its QEMU processes still hold the disk image open.

Final consideration

Obviously you can implement whatever fix you think is best (or not), i'm just pointing out what i've found and why the vm stays stopped instead of starting. If you see something that does not add up feel free to tell me i'm completely/partially wrong. I'd just like to help, best regards

Originally posted by @gusmef in #13729 (comment)

Activity

  1. changed the title [-]Hello everyone, with the help of Claude i got this summary:[/-] [+]CloudRuntimeException on failing VM restart[/+] on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions