Skip to content

KVM: Fix storage pool refcount leak on creation failure - #14694

Open
dheeraj12347 wants to merge 4 commits into
apache:4.22from
dheeraj12347:fix/kvm-storage-pool-refcount-leak
Open

dheeraj12347 wants to merge 4 commits into
apache:4.22from
dheeraj12347:fix/kvm-storage-pool-refcount-leak

Conversation

@dheeraj12347

Copy link
Copy Markdown
Contributor

Description

This PR fixes a storage pool reference-count leak in createStoragePoolLocked().

When the reference count is incremented for a secondary storage pool, subsequent failures in checkNetfsStoragePoolMounted() or getStoragePool() can leave the reference count incremented because the existing exception handling only catches LibvirtException.

Solution

  • Track whether the storage pool reference count was successfully incremented.
  • Track whether the operation completed successfully.
  • Use a finally block to decrement the reference count if the operation fails after the increment.
  • Preserve existing LibvirtException handling.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Bug Severity

  • Minor

How Has This Been Tested?

Added testCreateStoragePoolDecrementsRefCountWhenGetStoragePoolFails() to verify that the reference count is decremented when getStoragePool() throws a CloudRuntimeException.

  • git diff --check: Passed.
  • Maven tests: Not rerun after the final changes.

Related: #14189

bhouse-nexthop and others added 4 commits October 22, 2026 13:10
Two bugs in the storage pool teardown path.

adjustStoragePoolRefCount() means to lock on the single String instance
held as the map key, so that all callers share a monitor. When the map
has no entry for the pool, orElse(uuid) returns the caller's own instance
instead and the synchronized block guards nothing.

Increments cannot race each other: they happen inside createStoragePool,
which is only reached through KVMStoragePoolManager.createStoragePool,
and that is synchronized. Decrements are not covered, because
KVMStoragePoolManager.deleteStoragePool is not. So an increment and a
decrement can run at the same time, and while the map has no entry for
the pool they take different monitors and the decrement's remove() can
erase the increment. The count then reaches zero while the pool is still
in use. Use ConcurrentHashMap.compute(), which is atomic for the key.

deleteStoragePool() decided whether the retried umount had worked from
the return of runSimpleBashScript(), which is null both when the command
fails, because runScript() discards the output on a non-zero exit, and
when it succeeds without printing anything. A failed umount was therefore
logged and returned as a success. Take the outcome from whether the path
is still a mount point, which also covers the pool having been unmounted
by something else in the meantime, and return false rather than throwing
when it is still mounted: deleteStoragePool() is called from finally
blocks, where a throw would discard the result of an operation that has
already succeeded.

Signed-off-by: Brad House <bhouse@nexthop.ai>
The refcount is atomic, but it cannot keep a pool alive on its own.
deleteStoragePool() drops the last reference and then goes on to destroy
and unmount the pool without holding anything. createStoragePool() is
serialised by KVMStoragePoolManager, but deleteStoragePool() is not, so
a create can find the still active pool and take a reference after the
delete has seen the count reach zero, and the delete then tears the pool
down underneath it.

Take a monitor per pool uuid for the whole of createStoragePool() and
deleteStoragePool(). Every delete path ends in deleteStoragePool(String),
including LibvirtStoragePool.delete(), so locking there covers callers
that bypass the manager. Pools other than the one being deleted are not
held up, which matters because a delete can sit in the five second umount
retry.

Signed-off-by: Brad House <bhouse@nexthop.ai>
The previous commit took the per-pool lock inside
LibvirtStorageAdaptor.createStoragePool(), which is only reached through
KVMStoragePoolManager.createStoragePool(), and that is synchronized on
the manager. A create of a pool that is being torn down therefore waited
for the teardown while holding the manager wide lock. The umount retry in
deleteStoragePool() runs with no timeout, so a umount hung on storage
that has gone away held up the creates of every pool on the host, not
just the one being deleted.

Take the pool's lock in the manager first, before its own monitor, so a
create waiting for a teardown holds nothing that other pools need. The
locks move to KVMStoragePoolLocks so the manager and the adaptor share
them; the adaptor still takes the same lock, reentrantly on the create
path and on its own for deletes, including LibvirtStoragePool.delete(),
which does not go through the manager.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop

Copy link
Copy Markdown
Contributor

Thanks for picking this up. The fix itself looks right to me: decrementing in a finally covers the CloudRuntimeException from getStoragePool(), and gating it on refCountIncremented also stops the old catch from decrementing a primary pool that was never counted.

One problem: this depends on #14189 but targets 4.22 without it. createStoragePoolLocked only exists in #14189, and incStoragePoolRefCount/decStoragePoolRefCount are private on 4.22 (#14189 makes them protected). Applied to 4.22 alone, LibvirtStorageAdaptorTest doesn't compile:

LibvirtStorageAdaptorTest.java:[197,19] error: cannot find symbol
  symbol:   method createStoragePoolLocked(String,String,int,String,<null>,StoragePoolType,<null>,boolean)
LibvirtStorageAdaptorTest.java:[210,35] error: incStoragePoolRefCount(String) has private access in LibvirtStorageAdaptor
LibvirtStorageAdaptorTest.java:[211,35] error: decStoragePoolRefCount(String) has private access in LibvirtStorageAdaptor

On top of #14189, the main change applies cleanly but the test file conflicts with the tests #14189 adds there.

Two ways to go: rebase this onto 4.22 once #14189 merges, which is the simplest; or make it standalone now by testing through the public createStoragePool(...) and checking the ref count through storagePoolRefCounts rather than spying on the private methods. Either works for me.

@dheeraj12347

Copy link
Copy Markdown
Contributor Author

Thanks for picking this up. The fix itself looks right to me: decrementing in a finally covers the CloudRuntimeException from getStoragePool(), and gating it on refCountIncremented also stops the old catch from decrementing a primary pool that was never counted.

One problem: this depends on #14189 but targets 4.22 without it. createStoragePoolLocked only exists in #14189, and incStoragePoolRefCount/decStoragePoolRefCount are private on 4.22 (#14189 makes them protected). Applied to 4.22 alone, LibvirtStorageAdaptorTest doesn't compile:

LibvirtStorageAdaptorTest.java:[197,19] error: cannot find symbol
  symbol:   method createStoragePoolLocked(String,String,int,String,<null>,StoragePoolType,<null>,boolean)
LibvirtStorageAdaptorTest.java:[210,35] error: incStoragePoolRefCount(String) has private access in LibvirtStorageAdaptor
LibvirtStorageAdaptorTest.java:[211,35] error: decStoragePoolRefCount(String) has private access in LibvirtStorageAdaptor

On top of #14189, the main change applies cleanly but the test file conflicts with the tests #14189 adds there.

Two ways to go: rebase this onto 4.22 once #14189 merges, which is the simplest; or make it standalone now by testing through the public createStoragePool(...) and checking the ref count through storagePoolRefCounts rather than spying on the private methods. Either works for me.

Thanks for pointing that out, @bhouse-nexthop! I’ll wait for #14189 to get merged and then update my PR on top of it. Appreciate you taking a look!

@DaanHoogland DaanHoogland added this to the 4.22.3 milestone Nov 14, 2026
@dheeraj12347

Copy link
Copy Markdown
Contributor Author

Hi @bhouse-nexthop and @DaanHoogland ,

I've completed the implementation and added a regression test for the storage pool refcount leak.

I've tested the changes on top of PR #14189. The focused KVM storage tests passed (12/12). The full KVM plugin suite reported 668 passed, 2 skipped, and 1 error due to the missing native libvirt.so library in my WSL environment.

Could you please review the implementation and let me know if you recommend any further changes? I understand this PR depends on #14189, so I'll update it once the dependency is merged.

Thanks!

@DaanHoogland
DaanHoogland requested a balanced review from Copilot December 2, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@DaanHoogland

Copy link
Copy Markdown
Contributor

I understand this PR depends on #14189, so I'll update it once the dependency is merged.

@dheeraj12347 if it depends on #14189, does it make sense to base it on top of that?

@dheeraj12347
dheeraj12347 force-pushed the fix/kvm-storage-pool-refcount-leak branch from b8ed8f2 to 7abbd3a Compare December 3, 2026 19:33
@dheeraj12347

Copy link
Copy Markdown
Contributor Author

I understand this PR depends on #14189, so I'll update it once the dependency is merged.

@dheeraj12347 if it depends on #14189, does it make sense to base it on top of that?

Hi @DaanHoogland ,

Yes, I've updated my branch on top of the latest commit of #14189 and pushed it. My fix is now a separate commit on top of those changes.

Once #14189 is merged, I'll rebase my PR onto the updated 4.22 branch so that only my changes remain in the diff.

Thanks for the suggestion!

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants