-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix: NPE error #252
Conversation
WalkthroughThe changes made in this pull request focus on the Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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
📒 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
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
longhorn/longhorn-9852 Signed-off-by: Chin-Ya Huang <[email protected]>
2652397
to
29e5dfb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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