Skip to content

kvm: honour the force flag when stopping a domain - #6441

Merged
eriknordmark merged 2 commits into
lf-edge:masterfrom
eriknordmark:kvm-honour-force-shutdown
Sep 7, 2026
Merged

eriknordmark merged 2 commits into
lf-edge:masterfrom
eriknordmark:kvm-honour-force-shutdown

Conversation

@eriknordmark

@eriknordmark eriknordmark commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #6452.

A forced stop of a KVM domain was the same ACPI poweroff request as a graceful
one: KvmContext.Stop discarded its force argument and issued QMP
system_powerdown either way. A guest which cannot service that yet - one still
early in boot, for instance - ignores both, so nothing made the domain go away
until domainmgr gave up waiting and deleted it. An application stopped shortly
after it started therefore stayed reported as halting for as long as those waits
took, holding its memory and any assigned PCI devices meanwhile, and the
Shutdown(force) succeeded line domainmgr logs was not true.

A forced stop now terminates the process. That is what the interface promises
and what the xen (xl shutdown -F) and containerd implementations of the same
method already do. The termination path Delete already had - pause the vCPUs,
quit over the monitor, and a kill if that does not take - is factored out and
shared rather than duplicated, and now reports whether it worked, so
Shutdown(force) succeeded means what it says.

Measured on EVE-kvm amd64, an HVM Alpine guest with acpid stopped so it
ignores the request, deactivated once running:

build deactivate -> HALTED
master 701s (11m41s)
master + this PR 96s
master + a bounded graceful wait, without this PR 703s

The 605s difference is domainmgr's second wait, and 701s matches the ~11m45s
#5916 recorded for four eclient pods in one CI run. The third row is the
control: bounding the graceful wait cannot help, because HVM already gets
60s there.

Related to #6440 but deliberately not a fix for it -- see "What this does not
fix" below.

PR dependencies

None.

How to test and validate this PR

Covered by an evetest test and by unit tests, all validated against un-fixed
code.

TestHaltUnresponsiveGuest, in evetest/tests/apps and registered in
TestAppsSuite, deploys an HVM Alpine VM whose cloud-init stops acpid and
removes it from the runlevel, so the guest ignores the poweroff request by
construction. Against an EVE image without this change -- one carrying the
bounded graceful wait of #6453, so that only this PR is under test -- it fails
on its 300s bound; with it the application reports HALTED 1m36s after the
deactivation. It pulls its image from dl-cdn.alpinelinux.org, which
tests/apps did not previously depend on (tests/security/vcom_test.go
already does).

The assertion is on elapsed time because the reported state sequence is
identical either way -- RUNNING, HALTING, HALTED -- and only the time spent
halting differs. The bound separates two hardcoded budgets an order of magnitude
apart rather than an amount of work, so it is not a stopwatch on a loaded host.

The unit tests in kvm_force_test.go cover a domain whose monitor cannot be
reached, which is the path a guest that never services the poweroff request
takes; one with no pid file, so the absence of the only handle on the process
cannot wedge the caller; and one whose pid file is there but unreadable, where
nothing can be signalled and the caller has to be told rather than hearing that
the stop succeeded.

Changelog notes

None. A forced stop now does what its name says, which no current caller
reaches before its effect is already moot; nothing user-facing changes yet.

PR Backports

The defect has been present since the KVM driver was first written
(ef4d38f1c, 2020), so every stable branch carries it.

Checklist

  • I've provided a proper description
  • I've added the proper documentation -- no behaviour is documented that
    this changes; the method's contract is stated on the interface.
  • I've tested my PR on amd64 device -- the unit tests cover the changed
    path; the end-to-end reproduction in kvm: forced domain stop is the same ACPI request as a graceful one #6440 was run before the fix, not after.
  • I've tested my PR on arm64 device -- not tested; the change is
    architecture independent.
  • I've written the test verification instructions
  • I've set the proper labels to this PR.

What this does not fix

Stating it plainly, because the two look alike: this change does not shorten
the stall in #6440. There the application's mode is PV -- evetest leaves
VirtualizationMode unset and VmMode_PV is the zero value -- so the
doInactivate switch leaves firstDelay at maxDelay and the whole budget is
spent in the first, graceful waitForDomainGone, before the forced stop
this PR fixes is reached. Deactivating a container application in the same
second it reported RUNNING:

build deactivate -> HALTED
master 637 / 636 / 637 s
master + a bounded graceful wait (#6453) 98 / 95 / 96 s
master + #6453 + this PR 99 / 97 / 99 s

This PR adds nothing to the second row: once the graceful wait is bounded the
guest is ACPI-capable by the time the request is re-sent. #6453 is the cure for
#6440, and it is equally not a cure for #6452 -- the control row in the
Description shows it leaving that stall at 703s. The two are complementary and
neither subsumes the other: bounding the graceful wait fixes the guest which
becomes capable, and this PR fixes the guest which never does.

Note on scope

kubevirtContext.Stop ignores the same argument, and there it is named
force rather than _ so nothing flags it. That is deliberately left alone
here: it is a different mechanism and #6271 is the live conversation about that
code.

Whether PV should keep a 600s graceful budget before escalating - HVM and
FML get 60s, PV falls through to maxDelay - is a separate decision this PR
does not take, and it affects Xen PV guests too. Without it the graceful wait is
unchanged; this PR only makes the escalation after it real.

@eriknordmark

Copy link
Copy Markdown
Contributor Author

@claude

@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @eriknordmark's task in 2m 20s —— View job


Code review

Reviewed the change against origin/master. The fix is sound and well-targeted: forced Stop now shares the terminateQemu termination path with Delete (pause vCPUs → QMP quit → SIGKILL backstop), so a guest that never services the ACPI poweroff is actually torn down instead of stalling. The dedup and the tests are good, and the zombie-reaping detail in the test helper is correctly handled.

Two low-severity observations posted inline:

  • Stop(force) swallows failure — it returns nil unconditionally, so the Shutdown(force) succeeded log can still be false when SIGKILL fails or the pid file is unreadable (early return, no signal sent). terminateQemu already computes this outcome but discards it; returning an error would close the same gap this PR targets. (inline)
  • terminateQemu is half-parameterized — pid comes from the injected stateDir, but the QMP socket still uses the global kvmStateDir. The tests rely on no real forcetest domain existing under /run; passing the socket path too would fully isolate the tested behaviour. (inline)

No correctness, security, or watchdog regressions found (the QMP-hang case was already covered by Delete and is now backstopped for Stop too). The scope notes on kubevirt/PV are reasonable to defer.
· branch kvm-honour-force-shutdown

Comment thread pkg/pillar/hypervisor/kvm.go Resolved
Comment thread pkg/pillar/hypervisor/kvm.go Resolved
@codecov

codecov Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 55.55556% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 26.22%. Comparing base (3aaec95) to head (0f363f2).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
pkg/pillar/hypervisor/kvm.go 55.55% 11 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6441      +/-   ##
==========================================
+ Coverage   25.87%   26.22%   +0.34%     
==========================================
  Files         510      520      +10     
  Lines       93575    95303    +1728     
==========================================
+ Hits        24212    24990     +778     
- Misses      67494    68257     +763     
- Partials     1869     2056     +187     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknordmark
eriknordmark force-pushed the kvm-honour-force-shutdown branch from 52cbfd5 to a68435b Compare August 31, 2026 15:27
eriknordmark added a commit to eriknordmark/eve that referenced this pull request Aug 31, 2026
Picks up the four new commits on lf-edge#6063 through the fork#6 chain and adds lf-edge#6441.
Notes that lf-edge#6440 is an issue rather than a PR, and that lf-edge#6441 states it is not
the cure for it.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eriknordmark added a commit to eriknordmark/eve that referenced this pull request Aug 31, 2026
Picks up the four new commits on lf-edge#6063 through the fork#7 chain and adds lf-edge#6441.
Restates that this line only exercises the fault gates when the build passes
FAULT_INJECTION, and where the rootfs cap and the storage-resizer pin come from.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eriknordmark added a commit to eriknordmark/eve that referenced this pull request Aug 31, 2026
Picks up the four new commits on lf-edge#6063 through the fork#6 chain and adds lf-edge#6441.
Notes that lf-edge#6440 is an issue rather than a PR, and that lf-edge#6441 states it is not
the cure for it.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
eriknordmark added a commit to eriknordmark/eve that referenced this pull request Aug 31, 2026
Picks up the four new commits on lf-edge#6063 through the fork#7 chain and adds lf-edge#6441.
Restates that this line only exercises the fault gates when the build passes
FAULT_INJECTION, and where the rootfs cap and the storage-resizer pin come from.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@eriknordmark
eriknordmark removed the request for review from OhmSpectator August 31, 2026 16:40
@eriknordmark eriknordmark added next-13.4.x-rc PR must be present in the next 13.4.x patch release next-14.5.x-rc PR must be present in the next 14.5.x patch release next-16.0.x-rc PR must be present in the next 16.0.x patch release next-17.0.x-rc PR must be present in the next 17.0.x-lts release stable Should be backported to stable release(s) labels Aug 31, 2026
@rene

rene commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

@eriknordmark , @claude , why you are doing all of this? We can just send a quit command through QEMU QMP and this should finalize the qemu process. Much more simpler.

@github-actions

github-actions Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @rene's task in 50s —— View job


Why not just send quit over QMP?

Good question — and you're right that quit is the mechanism that actually makes the qemu process exit (unlike system_powerdown, which is an ACPI request the guest can ignore). But quit alone isn't enough, and that's the whole reason this PR exists.

quit over QMP can't be relied on to land:

  1. The monitor may be unreachable. terminateQemu guards the QMP work with os.Stat(socket) (kvm.go:2126). If the socket isn't there yet — a domain still early in startup, exactly the case in the PR description — there is nothing to send quit to.
  2. The monitor may be wedged. Even when the socket exists, execQuit can fail or hang if the QMP monitor is unresponsive (kvm.go:2128-2130). A quit that errors or blocks leaves the qemu process alive, still holding its memory and any assigned PCI devices.

That second case is the #5916 stall referenced in the code comment (kvm.go:2101-2106): the caller keeps seeing the domain present and never frees its resources. So the pid capture + waitProcessGone + SIGKILL backstop (kvm.go:2140-2148) isn't extra ceremony — it's precisely the guarantee that a forced stop terminates the process even when quit doesn't take. Without it, "forced" would be as unreliable as the ACPI path this PR is fixing.

This PR doesn't add that machinery — it reuses it. Delete already did pause vCPUs → quit → SIGKILL. All this change really does is:

  • factor that termination path out into terminateQemu, and
  • point forced Stop at it instead of issuing system_powerdown,

so a forced Stop and a Delete can't drift apart. The execStop (pause vCPUs) before quit is a pre-existing detail carried over from Delete, to freeze the guest before teardown.

So the short version: quit is sent — it's just not sufficient on its own. The pieces that look like "all of this" are the fallback for when quit fails or hangs, which is the failure mode being fixed. Sending only quit would re-open it.

@eriknordmark can confirm/correct the intent.
· branch kvm-honour-force-shutdown

Comment thread pkg/pillar/hypervisor/kvm.go Resolved
@github-actions
github-actions Bot requested a review from rene September 2, 2026 17:23
@eriknordmark eriknordmark removed next-13.4.x-rc PR must be present in the next 13.4.x patch release next-14.5.x-rc PR must be present in the next 14.5.x patch release next-16.0.x-rc PR must be present in the next 16.0.x patch release next-17.0.x-rc PR must be present in the next 17.0.x-lts release labels Sep 2, 2026
@eriknordmark
eriknordmark force-pushed the kvm-honour-force-shutdown branch from 2ad60b3 to 6a90531 Compare September 4, 2026 16:31
eriknordmark and others added 2 commits September 4, 2026 18:43
A forced stop was the same ACPI poweroff request as a graceful one, which a
guest that cannot service it yet - one still early in boot, for instance -
simply ignores. Nothing then made the domain go away until domainmgr gave up
waiting and deleted it, so an application stopped shortly after it started
stayed reported as halting for as long as those waits took, and its memory and
any assigned devices stayed held.

A forced stop now terminates the process, which is what the interface promises
and what the xen and containerd implementations of the same method already do.
The termination path Delete already used, a monitor quit followed by a kill if
that does not take, is shared rather than duplicated.

Termination reports whether it worked, so a stop that could not make the
process go away - an unusable pid file leaving nothing to signal, or a process
that outlives the SIGKILL - reaches the caller rather than only the log, and
domainmgr's Shutdown(force) succeeded line means what it says. Delete carries
the same outcome, so a domain whose process is still there is not reported as
deleted just because its state directory was removed.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Nothing obliges a guest to service the ACPI poweroff request, and when it does
not, only terminating the domain ends the stop. The guest here ignores it by
construction: acpid is what services the power button on Alpine, and cloud-init
stops it and removes it from the runlevel. The mode is HVM, which already gets
the short first wait, so what is left under test is what the escalation after
it does.

The assertion is on how long the application takes to be reported HALTED,
because the reported state sequence is the same either way -- only the time
spent halting differs. The bound separates two hardcoded budgets an order of
magnitude apart rather than an amount of work, so a loaded host moves both ends
a little and crosses neither.

Signed-off-by: eriknordmark <erik@zededa.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@eriknordmark
eriknordmark force-pushed the kvm-honour-force-shutdown branch from 6a90531 to 0f363f2 Compare September 4, 2026 16:57
@eriknordmark

Copy link
Copy Markdown
Contributor Author

@rene The quit is what this does — the bug was that the force path never sent one. Stop took its force argument as _ bool and issued system_powerdown either way, so a guest that cannot service ACPI (still early in boot, or with acpid stopped) ignored it and nothing made the domain go away until domainmgr gave up waiting and called Delete. Measured on an HVM Alpine guest: 701s from deactivate to HALTED, 96s with this change.

Delete already did exactly what you describe — pause the vCPUs, quit over the monitor, SIGKILL if that does not take. This factors that out as terminateQemu and points forced Stop at it, so the two cannot drift; it adds no new mechanism. The kill is the only part beyond quit, and it is pre-existing: a quit that fails or hangs on a wedged monitor is the #5916 stall, and if the domain has not got far enough there is no socket to send it to at all.

It also brings kvm in line with the other backends on the same flag — xen appends -F to xl shutdown, containerd passes force through to CtrStopContainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stable Should be backported to stable release(s)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kvm: a guest which never services ACPI poweroff burns the full 600s force wait

2 participants