You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix incorrect error handling in the AllocateID function
PR Type
Bug fix
Description
• Fixed incorrect error handling in AllocateID function
• Added proper mutex unlock and error return logic
• Improved error logging for ID allocation failures
• Applied code formatting improvements to function signatures
Changes walkthrough 📝
Relevant files
Bug fix
hakeeper_client.go
Fix AllocateID error handling and formatting
pkg/logservice/hakeeper_client.go
• Fixed AllocateID function to properly handle errors and unlock mutex
• Added defer statement for mutex unlock to prevent deadlocks • Improved error logging with proper error return instead of continuing loop • Applied code formatting to multiple function signatures for better readability
• Fix ID allocation problems through improved error handling in AllocateID function
Non-compliant requirements:
• Direct resolution of "Unknown database tpch_1g" error is not evident in the code changes
• "internal error: reserved table ID 6" issue resolution is not clearly addressed
Requires further human verification:
• Verification that the AllocateID fix resolves the database creation and table ID reservation errors
• Testing in standalone regression environment to confirm the fix works end-to-end
The AllocateID function has a critical flaw where it returns firstID and err after successful allocation, but err might still contain the previous error from the retry loop. This could return a valid ID with a non-nil error, causing inconsistent behavior.
After successful ID allocation, the function should validate that firstID is not 0 before returning, similar to the validation done in the fast path. The removed validation logic for zero IDs is no longer present.
The defer c.mu.Unlock() statement is placed after the early return path, which means it won't execute when the mutex is already unlocked in the if block. This creates inconsistent mutex handling and potential race conditions.
c.mu.Lock()
+defer c.mu.Unlock()+
if c.mu.sharedAllocID.nextID != c.mu.sharedAllocID.lastID {
v := c.mu.sharedAllocID.nextID
c.mu.sharedAllocID.nextID++
- c.mu.Unlock()
if v == 0 {
logutil.Error("id should not be 0",
zap.Uint64("nextID", c.mu.sharedAllocID.nextID),
zap.Uint64("lastID", c.mu.sharedAllocID.lastID))
}
return v, nil
}
-defer c.mu.Unlock()-
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 9
__
Why: The suggestion correctly identifies a critical issue. The current PR code unlocks the mutex twice if the initial if condition is met, leading to a panic. The proposed change moves defer c.mu.Unlock() to right after c.mu.Lock() and removes the manual c.mu.Unlock(), which is the standard and correct way to handle mutexes in Go, preventing deadlocks and double-unlock panics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
What type of PR is this?
Which issue(s) this PR fixes:
issue #22005
What this PR does / why we need it:
PR Type
Bug fix
Description
• Fixed incorrect error handling in AllocateID function
• Added proper mutex unlock and error return logic
• Improved error logging for ID allocation failures
• Applied code formatting improvements to function signatures
Changes walkthrough 📝
hakeeper_client.go
Fix AllocateID error handling and formattingpkg/logservice/hakeeper_client.go
• Fixed AllocateID function to properly handle errors and unlock mutex
• Added defer statement for mutex unlock to prevent deadlocks
•
Improved error logging with proper error return instead of continuing
loop
• Applied code formatting to multiple function signatures for
better readability