Skip to content

Conversation

@haitwang-cloud
Copy link
Contributor

What type of PR is this?

/kind flake

What this PR does / why we need it:

This PR adds defensive nil checks throughout the pkg/util/leaderelection/leaderelection.go file to prevent potential runtime panics. The changes improve code robustness by handling cases where callback functions or lease object fields may be nil.

The original code had several potential panic scenarios:

  1. Nil callback functions: When LeaderCallbacks are created without providing OnStartedLeading or OnStoppedLeading functions, calling them directly would cause a panic. This is a real scenario as seen in test code: NewLeaderManager("dev", "kube-system", "hami-scheduler", LeaderCallbacks{})

  2. Nil lease pointer: The isHolderOf() method could receive a nil lease pointer without proper validation

  3. Nil lease fields: The isLeaseValid() method could access nil observedLease or LeaseDurationSeconds fields causing nil pointer dereference

Changes made:

  • Added nil checks for OnStartedLeading and OnStoppedLeading callbacks in onAdd(), onUpdate(), and onDelete() methods
  • Added nil check for lease pointer in isHolderOf() method
  • Added nil checks for observedLease and LeaseDurationSeconds in isLeaseValid() method

Which issue(s) this PR fixes:

Fixes potential runtime panics in leader election mechanism

Special notes for your reviewer:

  • These changes are purely defensive programming improvements with no functional changes to the business logic
  • The callback nil checks are necessary because the API design allows empty LeaderCallbacks{} to be passed (as seen in test code)
  • All changes have minimal performance overhead (single pointer comparisons)
  • Existing tests continue to pass and now safely handle edge cases

Does this PR introduce a user-facing change?:

Fix potential panics in leader election by adding nil checks for callbacks and lease fields

@hami-robot
Copy link
Contributor

hami-robot bot commented Jan 20, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: haitwang-cloud
Once this PR has been reviewed and has the lgtm label, please assign wawa0210 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@github-actions github-actions bot added the kind/bug Something isn't working label Jan 20, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @haitwang-cloud, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness of the leader election mechanism by introducing several defensive nil checks across key functions. These changes proactively address potential runtime panics that could arise from uninitialized callback functions or nil lease object fields, thereby improving the system's stability and reliability without altering its core functional behavior.

Highlights

  • Callback Nil Checks: Implemented nil checks for OnStartedLeading and OnStoppedLeading callback functions within onAdd, onUpdate, and onDelete methods to prevent panics when callbacks are not provided, as seen in test scenarios.
  • Lease Pointer Validation: Added a nil check for the lease pointer in the isHolderOf method to ensure safe access to its fields, addressing a potential nil pointer dereference.
  • Lease Field Validation: Introduced nil checks for observedLease and LeaseDurationSeconds within the isLeaseValid method to prevent nil pointer dereferences when these fields might be uninitialized.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@hami-robot hami-robot bot added the size/S label Jan 20, 2026
- Add nil checks for callback functions (OnStartedLeading/OnStoppedLeading)
- Add nil checks for lease pointer in isHolderOf method
- Add nil checks for observedLease and LeaseDurationSeconds in isLeaseValid method
- Prevents runtime panics when callbacks are not provided or lease fields are nil

This change improves robustness by adding defensive nil checks throughout
the leaderelection code, particularly for callback functions that may not
always be provided and for lease object fields that could be nil.

Signed-off-by: Tim <[email protected]>
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses potential nil pointer panics in the leader election logic by adding necessary checks for callbacks and lease objects. The changes are defensive and improve the robustness of the code. I have one suggestion to improve consistency in how the nil checks for callbacks are implemented.

@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 51.18% <100.00%> (+0.03%) ⬆️

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

Files with missing lines Coverage Δ
pkg/util/leaderelection/leaderelection.go 88.60% <100.00%> (+1.10%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ields

Add comprehensive test coverage for nil pointer safety in the leader election
manager. Tests verify that the system handles nil callbacks gracefully without
panicking, and properly validates nil lease fields.

Changes include:
- Test nil callbacks (OnStartedLeading, OnStoppedLeading) don't cause panics
- Test partial callback configurations work correctly
- Test nil lease and lease field validation (HolderIdentity, LeaseDurationSeconds)
- Test IsLeader and isLeaseValid behavior with nil observedLease

This improves robustness by ensuring the leader election system degrades
gracefully when callbacks are not provided or lease data is incomplete.

Signed-off-by: Tim <[email protected]>
@archlitchi
Copy link
Member

have you encountered an issue related to it?

@haitwang-cloud
Copy link
Contributor Author

Hey@archlitchi, not yet. I recently noticed the new feature release that introduces leader election, reviewed the relevant code, and inserted a safeguard to prevent the panic.

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.

2 participants