Skip to content

Comments

Fix NPE in NASBackupProvider when no running KVM host is available#12680

Open
jmsperu wants to merge 1 commit intoapache:mainfrom
jmsperu:fix/nas-backup-null-host-npe
Open

Fix NPE in NASBackupProvider when no running KVM host is available#12680
jmsperu wants to merge 1 commit intoapache:mainfrom
jmsperu:fix/nas-backup-null-host-npe

Conversation

@jmsperu
Copy link

@jmsperu jmsperu commented Feb 21, 2026

Description

NASBackupProvider.syncBackupStorageStats() crashes with a NullPointerException when ResourceManager.findOneRandomRunningHostByHypervisor() returns null. This happens when no KVM host in the zone has status=Up at the moment the BackupSyncTask runs (e.g., during management server startup, brief agent disconnections, or host state transitions).

The NPE kills the entire BackupSyncTask background job every sync interval (default 300s), flooding the management server log with stack traces and preventing backup storage stats from being updated.

The same pattern exists in deleteBackup() where host can be null when the VM is removed and no running KVM host is available.

Stack Trace

ERROR [o.a.c.b.B.BackupSyncTask] Error trying to run backup-sync background task due to:
[Cannot invoke "com.cloud.host.Host.getId()" because "host" is null].
java.lang.NullPointerException: Cannot invoke "com.cloud.host.Host.getId()" because "host" is null
    at org.apache.cloudstack.backup.NASBackupProvider.syncBackupStorageStats(NASBackupProvider.java:544)
    at org.apache.cloudstack.backup.BackupManagerImpl$BackupSyncTask.runInContext(BackupManagerImpl.java:1947)

Changes

  • syncBackupStorageStats(): Add early return with warning log when no running KVM host is found
  • deleteBackup(): Add null check with descriptive CloudRuntimeException

How Has This Been Tested

Tested on a production CloudStack 4.22.0.0 deployment with 3 KVM hosts and NAS (NFS) backup provider. Before the fix, the NPE fired every 5 minutes after management server restart. After applying the patch, the BackupSyncTask runs cleanly — logging a warning during startup and resuming normally once hosts reconnect.

Types of changes

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

Screenshot / Video (if applicable)

Before (every 5 minutes):

ERROR [o.a.c.b.B.BackupSyncTask] Error trying to run backup-sync background task due to:
[Cannot invoke "com.cloud.host.Host.getId()" because "host" is null]

After:

WARN  [o.a.c.b.NASBackupProvider] Unable to find a running KVM host in zone 1 to sync backup storage stats

ResourceManager.findOneRandomRunningHostByHypervisor() can return null
when no KVM host in the zone has status=Up (e.g. during management
server startup, brief agent disconnections, or host state transitions).

NASBackupProvider.syncBackupStorageStats() and deleteBackup() call
host.getId() without a null check, causing a NullPointerException that
crashes the entire BackupSyncTask background job every sync interval.

This adds null checks in both methods:
- syncBackupStorageStats: log a warning and return early
- deleteBackup: throw CloudRuntimeException with a descriptive message
@boring-cyborg
Copy link

boring-cyborg bot commented Feb 21, 2026

Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
Here are some useful points:

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a NullPointerException in NASBackupProvider that occurs when no running KVM host is available in a zone. The NPE was crashing the BackupSyncTask background job every sync interval and preventing backup storage stats from being updated.

Changes:

  • Added null check in syncBackupStorageStats() with early return and warning log when no running KVM host is found
  • Added null check in deleteBackup() with descriptive exception when no running KVM host is available

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +570 to +572
if (CollectionUtils.isEmpty(repositories)) {
return;
}
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

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

The empty repositories check is placed after the method starts processing but before the host lookup. If repositories are empty, the host lookup at line 573 becomes unnecessary. Consider moving this check before the host lookup, or better yet, to the beginning of the method to avoid any unnecessary operations when there are no repositories to process.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 17.92%. Comparing base (30e6c22) to head (80435b4).

Files with missing lines Patch % Lines
...rg/apache/cloudstack/backup/NASBackupProvider.java 0.00% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #12680      +/-   ##
============================================
- Coverage     17.92%   17.92%   -0.01%     
  Complexity    16154    16154              
============================================
  Files          5939     5939              
  Lines        533181   533188       +7     
  Branches      65237    65240       +3     
============================================
+ Hits          95585    95586       +1     
- Misses       426856   426860       +4     
- Partials      10740    10742       +2     
Flag Coverage Δ
uitests 3.67% <ø> (ø)
unittests 19.03% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@DaanHoogland
Copy link
Contributor

@jmsperu , would you want this included in the next 22 release (in which case you might to want to base it of the 4.22 branch)

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.

NullPointerException in NASBackupProvider.syncBackupStorageStats when no KVM host is in "Up" state

2 participants