Skip to content

Add null checks when fetching culprits in builds with SCM#26261

Open
prathamalwayscomeslast wants to merge 7 commits intojenkinsci:masterfrom
prathamalwayscomeslast:culprit-npe-fix
Open

Add null checks when fetching culprits in builds with SCM#26261
prathamalwayscomeslast wants to merge 7 commits intojenkinsci:masterfrom
prathamalwayscomeslast:culprit-npe-fix

Conversation

@prathamalwayscomeslast
Copy link
Contributor

Fixes #26256

When culprits are misconfigured, likely due to older/corrupt build configs, it leads to cascading build failures. This change adds null checks ensuring builds run correctly.

Testing done

Manually tested the change and works as it should. Misconfigured the build.xml, removing the culprits tag, and successive builds run with no errors.

Screenshots (UI changes only)

Before

After

Proposed changelog entries

  • human-readable text

Proposed changelog category

/label bug

Proposed upgrade guidelines

N/A

Submitter checklist

  • The issue, if it exists, is well-described.
  • The changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developers, depending on the change) and are in the imperative mood (see examples). Fill in the Proposed upgrade guidelines section only if there are breaking changes or changes that may require extra steps from users during upgrade.
  • There is automated testing or an explanation as to why this change has no tests.
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadocs, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO"), if applicable.
  • UI changes do not introduce regressions when enforcing the current default rules of Content Security Policy Plugin. In particular, new or substantially changed JavaScript is not defined inline and does not call eval to ease future introduction of Content Security Policy (CSP) directives (see documentation).
  • For dependency updates, there are links to external changelogs and, if possible, full differentials.
  • For new APIs and extension points, there is a link to at least one consumer.

Desired reviewers

@MarkEWaite

Before the changes are marked as ready-for-merge:

Maintainer checklist

  • There are at least two (2) approvals for the pull request and no outstanding requests for change.
  • Conversations in the pull request are over, or it is explicit that a reviewer is not blocking the change.
  • Changelog entries in the pull request title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood.
  • Proper changelog labels are set so that the changelog can be generated automatically.
  • If the change needs additional upgrade steps from users, the upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the pull request title (see example).
  • If it would make sense to backport the change to LTS, be a Bug or Improvement, and either the issue or pull request must be labeled as lts-candidate to be considered.

- Added null check so corrupt/older configs on builds with SCM.

Signed-off-by: prathamalwayscomeslast <way4hell6@gmail.com>
@comment-ops-bot comment-ops-bot bot added the bug For changelog: Minor bug. Will be listed after features label Feb 4, 2026
@daniel-beck
Copy link
Member

Please add test coverage.

Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

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

Several changes are needed and more investigation.

  • Please resolve the spotbugs warnings
  • Please use git bisect to identify the commit that caused the regression. It is important that we understand what changed to cause the issue to be visible in this release when it was not visible in previous releases. This may be the correct fix, but until we know the commit that caused the regression, I'm not confident that this is the correct fix

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 addresses a NullPointerException that occurs when recording culprits at the end of FreeStyle builds using SCM. The issue was introduced when builds have misconfigured or corrupt culprit data, likely from older build configurations. The fix adds null checks and exception handling to ensure builds complete successfully even with corrupted culprit data.

Changes:

  • Adds null-safety checks for getCulpritIds() to handle null values explicitly
  • Wraps Set.copyOf() in a try-catch block to handle NullPointerExceptions from corrupted UnmodifiableCollection objects during XStream deserialization
  • Adds @SuppressFBWarnings annotation to document the intentional exception handling for SpotBugs

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

Comment on lines +107 to +109
} catch (NullPointerException e) {
culpritIds = Collections.emptySet();
}
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The NullPointerException is caught but silently swallowed without any logging. This makes it difficult to diagnose issues with corrupted build configurations. Based on the codebase convention seen in other files (e.g., hudson/util/Scrambler.java:56 logs "Corrupted data" at WARNING level), exceptions related to data corruption should be logged. Consider adding logging at WARNING or INFO level to help users identify when builds have corrupted culprit data, which would aid in troubleshooting and allow administrators to take corrective action on affected builds.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This catch block is removed now, so logging isn't needed

@prathamalwayscomeslast
Copy link
Contributor Author

identify the commit that caused the regression

I saw the history of RunWithSCM, it used Collections.unmodifiableSet() but changed to Set.copyOf() in commit 6d17999. This might be a cause of regression as Set.copyOf immediately calls isEmpty causing npe. But this commit happened during version 2.336 which is way before the mentioned version (2.528) in the issue that the user updated from. So i dont know if this is a regression or some issue on the user's end.

@prathamalwayscomeslast
Copy link
Contributor Author

Covered tests related to the changes and solved the spotbugs.

@MarkEWaite
Copy link
Contributor

Covered tests related to the changes and solved the spotbugs.

You didn't solve the spotbugs issues, didn't address the comment from GitHub Copilot, and didn't identify the commit that caused the issue.

When I run the command mvn -DskipTests verify, it reports:

[INFO] --- spotbugs:4.9.8.2:check (spotbugs) @ jenkins-core ---
[INFO] BugInstance size is 2
[INFO] Error size is 0
[INFO] Total bugs: 2
[ERROR] Medium: Do not catch NullPointerException like in new jenkins.scm.RunWithSCM$1(RunWithSCM, Set) [jenkins.scm.RunWithSCM$1] At RunWithSCM.java:[line 108] DCN_NULLPOINTER_EXCEPTION
[ERROR] Medium: Suppressing annotation DCN_NULLPOINTER_EXCEPTION on the field jenkins.scm.RunWithSCM$1.culpritIds is unnecessary [jenkins.scm.RunWithSCM$1] In RunWithSCM.java US_USELESS_SUPPRESSION_ON_FIELD

@prathamalwayscomeslast
Copy link
Contributor Author

Apologies for incomplete fixes. I removed NPE catch block causing spotbugs.

I ran git log jenkins-2.528..HEAD -- core/src/main/java/jenkins/scm/RunWithSCM.java. The only commits in that range are my own PR commits. The root cause of the regression seems to be commit 6d17999 which changed
Collections.unmodifiableSet() to Set.copyOf() which internally calls isEmpty() causing the NPE. It's a bug triggered by stale persisted data but not a regression introduced after 2.528

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

Labels

bug For changelog: Minor bug. Will be listed after features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException recording culprits at the end of FreeStyle builds, causing them to fail

4 participants