-
Notifications
You must be signed in to change notification settings - Fork 304
fix: Replace defer with explicit Close() calls in export/import loops #1109
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -126,7 +126,6 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return 0, nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
defer exporter.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node, err := exporter.Next() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if errors.Is(err, iavl.ErrorExportDone) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -137,6 +136,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export = append(export, node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.AddNode(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exporter.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.AddDurationSince(start) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmt.Printf("%-13v: %v\n", name, stats.String()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
totalStats.Add(stats) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -173,7 +173,6 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
defer importer.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, node := range exports[name] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
err = importer.Add(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -185,6 +184,7 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
importer.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Potential resource leak on early error return. Similar to the export function, this explicit Close() call could be skipped if errors occur in the processing loop (lines 176-182) or during commit (line 183). Consider refactoring to a helper function: +func importStore(name string, version int64, nodes []*iavl.ExportNode) (Stats, error) {
+ tempdir, err := os.MkdirTemp("", name)
+ if err != nil {
+ return Stats{}, err
+ }
+ defer os.RemoveAll(tempdir)
+
+ start := time.Now()
+ stats := Stats{}
+
+ newDB, err := idbm.NewGoLevelDB(name, tempdir)
+ if err != nil {
+ return stats, err
+ }
+
+ newTree := iavl.NewMutableTree(newDB, 0, false, iavl.NewNopLogger())
+ importer, err := newTree.Import(version)
+ if err != nil {
+ return stats, err
+ }
+ defer importer.Close()
+
+ for _, node := range nodes {
+ err = importer.Add(node)
+ if err != nil {
+ return stats, err
+ }
+ stats.AddNode(node)
+ }
+
+ err = importer.Commit()
+ if err != nil {
+ return stats, err
+ }
+
+ stats.AddDurationSince(start)
+ return stats, nil
+} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stats.AddDurationSince(start) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fmt.Printf("%-12v: %v\n", name, stats.String()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
totalStats.Add(stats) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
🛠️ Refactor suggestion
Potential resource leak on early error return.
While moving from defer to explicit Close() addresses the accumulation issue in loops, the current placement could lead to resource leaks if errors occur in the processing loop (lines 129-138).
Consider refactoring the per-store logic into a helper function where defer can be used safely:
🤖 Prompt for AI Agents