Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NPE error #252

Conversation

c3y1huang
Copy link
Contributor

Which issue(s) this PR fixes:

Issue longhorn/longhorn#9852

What this PR does / why we need it:

Fix NPE error.

Special notes for your reviewer:

None

Additional documentation or context

None

@c3y1huang c3y1huang self-assigned this Dec 3, 2024
Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The changes made in this pull request focus on the ReplicaAdd method within the Engine struct in pkg/spdk/engine.go. A nil check was added for the e.ReplicaStatusMap[dstReplicaName] to prevent nil pointer dereference errors. The error handling logic was also adjusted to ensure the mode is updated only if the replica status is not nil and not already set to types.ModeERR. Minor formatting adjustments and comments were included for clarity.

Changes

File Change Summary
pkg/spdk/engine.go Modified ReplicaAdd method to add nil check for e.ReplicaStatusMap[dstReplicaName] and adjusted error handling logic. Minor formatting and comments added.

Assessment against linked issues

Objective Addressed Explanation
Prevent faulted state during volume restore (9852)

Possibly related PRs

Suggested reviewers

  • shuo-wu
  • innobead
  • derekbit

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2652397 and 29e5dfb.

📒 Files selected for processing (1)
  • pkg/spdk/engine.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/spdk/engine.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
pkg/spdk/engine.go (1)

999-1002: LGTM! The nil check prevents NPE during replica addition failures.

The added nil check properly guards against null pointer dereference when accessing the replica's Mode field. This fix correctly handles cases where replica addition fails before initializing the status map entry.

Consider extracting the mode update logic into a helper method for better readability:

-if e.ReplicaStatusMap[dstReplicaName] != nil && e.ReplicaStatusMap[dstReplicaName].Mode != types.ModeERR {
-  e.log.WithError(err).Errorf("Engine failed to start replica %s rebuilding, will mark the rebuilding replica mode from %v to ERR", dstReplicaName, e.ReplicaStatusMap[dstReplicaName].Mode)
-  e.ReplicaStatusMap[dstReplicaName].Mode = types.ModeERR
-}
+if err := e.markReplicaModeAsError(dstReplicaName, "failed to start replica rebuilding"); err != nil {
+  e.log.WithError(err).Errorf("Failed to mark replica %s mode as ERR", dstReplicaName)
+}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between bfa1d21 and 2652397.

📒 Files selected for processing (1)
  • pkg/spdk/engine.go (1 hunks)
🔇 Additional comments (1)
pkg/spdk/engine.go (1)

999-1002: Verify similar patterns in the codebase.

While this fix addresses the immediate NPE issue, consider reviewing similar patterns where replica mode is accessed without nil checks.

Run the following script to find similar patterns:

✅ Verification successful

Let me analyze the code further to check for potential nil pointer issues.


Similar patterns are properly protected against nil pointer dereference

After analyzing the codebase, I found that other accesses to ReplicaStatusMap are properly protected:

  • Direct field accesses are preceded by nil checks in critical paths
  • Functions that operate on replica status have proper nil checks before accessing the map values
  • The pattern in the original fix is consistent with existing defensive programming practices in the codebase

Key examples of proper nil checks:

  • dstReplicaStatus := e.ReplicaStatusMap[dstReplicaName]; if dstReplicaStatus == nil {
  • replicaStatus := e.ReplicaStatusMap[replicaName]; if replicaStatus == nil {
  • if e.ReplicaStatusMap[dstReplicaName] != nil && e.ReplicaStatusMap[dstReplicaName].Mode != types.ModeERR {
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find similar patterns of accessing replica mode without nil checks
# Look for direct access to Mode field that might need nil checks

# Search for direct mode access patterns
rg -A 2 'ReplicaStatusMap\[[^]]+\]\.Mode'

# Search for potential nil dereference patterns
ast-grep --pattern 'if $map[$key].$field' 

Length of output: 2068


Script:

#!/bin/bash
# Search for all ReplicaStatusMap accesses to ensure we don't miss any patterns
rg -A 3 'ReplicaStatusMap\[' pkg/spdk/

# Also search for function signatures that return or handle ReplicaStatusMap entries
ast-grep --pattern 'func $_($_) (*ReplicaStatus, error)'

Length of output: 5562

Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 8.38%. Comparing base (537b20a) to head (29e5dfb).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pkg/spdk/engine.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #252      +/-   ##
========================================
- Coverage   8.42%   8.38%   -0.05%     
========================================
  Files         21      21              
  Lines       7158    7159       +1     
========================================
- Hits         603     600       -3     
- Misses      6475    6478       +3     
- Partials      80      81       +1     
Flag Coverage Δ
unittests 8.38% <0.00%> (-0.05%) ⬇️

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.

longhorn/longhorn-9852

Signed-off-by: Chin-Ya Huang <[email protected]>
@c3y1huang c3y1huang force-pushed the 9852-v2-volume-faulted-during-degraded-availability-restore branch from 2652397 to 29e5dfb Compare December 3, 2024 07:57
@c3y1huang c3y1huang requested review from derekbit, shuo-wu and a team December 3, 2024 08:21
Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

LGTM

@derekbit derekbit merged commit 46dd200 into longhorn:main Dec 3, 2024
8 of 10 checks passed
@c3y1huang c3y1huang deleted the 9852-v2-volume-faulted-during-degraded-availability-restore branch December 3, 2024 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants