-
Notifications
You must be signed in to change notification settings - Fork 581
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: multiple temp directories on command executions #8726
base: main
Are you sure you want to change the base?
fix: multiple temp directories on command executions #8726
Conversation
WalkthroughThe changes in the codebase introduce a new deferred function to ensure that the temporary application ( Changes
Assessment against linked issues
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
CodeRabbit Configuration File (
|
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: 1
Outside diff range and nitpick comments (1)
cmd/osmosisd/cmd/root.go (1)
Line range hint
489-496
: Approve changes with a suggestion for cleanup timing.The modifications to the
tempDir
function improve isolation and error handling. However, the cleanup logic can be improved.Consider moving the
defer os.RemoveAll(dir)
to the calling function. This ensures that the temporary directory is cleaned up after it has been used, rather than immediately after creation. Here's a suggested modification:func tempDir() string { dir, err := os.MkdirTemp("", "osmosisd") if err != nil { panic(fmt.Sprintf("failed creating temp directory: %s", err.Error())) } - defer os.RemoveAll(dir) return dir }
Then, in the calling function (likely
NewRootCmd
), add:tempDir := tempDir() defer os.RemoveAll(tempDir)This ensures that the temporary directory is removed after it has been used by the application.
defer func() { | ||
if err := tempApp.Close(); err != nil { | ||
panic(err) | ||
} | ||
if tempDir != osmosis.DefaultNodeHome { | ||
os.RemoveAll(tempDir) | ||
} | ||
}() |
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.
Approve changes with a suggestion for improved error handling.
The addition of the deferred cleanup function is a good practice for resource management. It ensures that the temporary app is properly closed and the temporary directory is removed when necessary.
Consider improving the error handling in the deferred function. Instead of panicking, log the error. This allows the application to continue running even if there's an issue during cleanup. Here's a suggested modification:
defer func() {
if err := tempApp.Close(); err != nil {
- panic(err)
+ logger.Error("failed to close temp app", "error", err)
}
if tempDir != osmosis.DefaultNodeHome {
- os.RemoveAll(tempDir)
+ if err := os.RemoveAll(tempDir); err != nil {
+ logger.Error("failed to remove temp directory", "error", err)
+ }
}
}()
This change ensures that any errors during cleanup are logged but don't cause the application to panic, which could be disruptive in a production environment.
Committable suggestion was skipped due to low confidence.
Closes: #8725
What is the purpose of the change
This pull request fix issue with temp directories and stops creating multiple temp directories
also gives panic if node is unable to create a temp directory instead of returning node default home directory which leads to erasing of entire node data