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: multiple temp directories on command executions #8726

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

harish551
Copy link
Contributor

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

Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes in the codebase introduce a new deferred function to ensure that the temporary application (tempApp) is closed properly and that the temporary directory is removed if it is not the default node home. The error handling for creating the temporary directory has been modified to panic on failure instead of reverting to the default node home, enhancing resource cleanup and error management.

Changes

Files Change Summary
cmd/osmosisd/cmd/root.go Introduced deferred function for closing tempApp and removing temporary directory; modified error handling for directory creation to panic on failure.
CHANGELOG.md Updated to include entries for additional queries in x/gauges and a fix for multiple temporary directories.

Assessment against linked issues

Objective Addressed Explanation
Prevent creation of multiple temp directories during command executions ( #8725)
Ensure that temporary directories are removed after execution ( #8725)
Avoid overwriting the default node home on temp directory creation failure ( #8725)

Possibly related PRs

Suggested labels

A:backport/v25.x

Suggested reviewers

  • PaddyMc

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Contributor

@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: 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.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 133d623 and b683bd2.

Files selected for processing (1)
  • cmd/osmosisd/cmd/root.go (2 hunks)

Comment on lines +359 to +366
defer func() {
if err := tempApp.Close(); err != nil {
panic(err)
}
if tempDir != osmosis.DefaultNodeHome {
os.RemoveAll(tempDir)
}
}()
Copy link
Contributor

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.

@PaddyMc PaddyMc added V:state/compatible/backport State machine compatible PR, should be backported A:backport/v26.x backport patches to v26.x branch labels Sep 24, 2024
@PaddyMc
Copy link
Contributor

PaddyMc commented Sep 24, 2024

Thanks for the PR, would you be able to add a changelog?

@harish551
Copy link
Contributor Author

Thanks for the PR, would you be able to add a changelog?

Updated changelog will that work ?
let me know if I need to add somewhere else

Copy link
Contributor

@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)
CHANGELOG.md (1)

Old sdkmath types still in use

The following files still use the old sdkmath types (DecCoin, DecCoins):

  • x/concentrated-liquidity/types/incentive_record.pb.go
  • x/concentrated-liquidity/model/tick_info.pb.go
  • x/concentrated-liquidity/client/queryproto/query.pb.go
  • osmoutils/accum/accum.pb.go
Analysis chain

Line range hint 98-99: API Break: Switch to sdkmath types

This is a significant change that affects many parts of the codebase. The switch to sdkmath types can improve consistency but requires careful migration. Pay attention to the note about Dec and Int initialization behavior changes.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any remaining uses of the old math types
echo "Searching for old math types usage:"
rg --type go 'github\.com/cosmos/cosmos-sdk/types\.(Dec|Int)' || echo "No old math types found."

Length of output: 2591

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b683bd2 and 47448e6.

Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments not posted (16)
CHANGELOG.md (16)

55-56: LGTM: Minor state export fix for cwpool gauges

This change appears to be a minor fix for state export related to cwpool gauges. It's good to see improvements in state export functionality.


57-57: LGTM: IAVL version bump

Upgrading IAVL to v0.19.7 is a good practice to keep dependencies up-to-date. This may include performance improvements or bug fixes.


Line range hint 61-62: New feature: SDK.Coins helpers added

The addition of Mul and Quo helpers for sdk.Coins in osmoutils is a useful feature that can simplify coin calculations in the codebase.


Line range hint 63-63: New feature: Mutative version of QuoRoundUp added

Adding a mutative version of QuoRoundUp can help improve performance in certain scenarios by reducing allocations.


Line range hint 64-64: New feature: Efficient BigDec truncations

The addition of mutative and efficient BigDec truncations with arbitrary decimals is a valuable performance improvement.


Line range hint 65-65: New feature: Query for number of initialized ticks in CL

This new query for the number of initialized ticks in Concentrated Liquidity (CL) can be useful for analytics and debugging purposes.


Line range hint 66-66: New feature: V2 SpotPrice CLI and GRPC query

The addition of a v2 SpotPrice query with 36 decimals in poolmanager is a significant improvement in precision for price queries.


Line range hint 70-70: API Break: PoolModuleI CalculateSpotPrice return type change

Changing the return type of CalculateSpotPrice to BigDec is an API breaking change. Ensure that all dependent code is updated accordingly.


Line range hint 71-71: API Break: Removal of redundant param in CreateGaugeRefKeys

Removing a redundant parameter from CreateGaugeRefKeys in incentives is a good cleanup, but it's an API breaking change. Make sure all callers are updated.


Line range hint 72-72: API Break: Removal of redundant ctx param in DeleteAllKeysFromPrefix

Removing the redundant ctx parameter from DeleteAllKeysFromPrefix in osmoutils is another API breaking change. Ensure all usage is updated.


Line range hint 76-80: LGTM: New features and performance improvements

These additions, including SDK.Coins helpers, mutative versions of mathematical operations, and efficient BigDec truncations, are all valuable improvements that can enhance performance and developer experience.


Line range hint 81-81: New feature: Query for number of initialized ticks in CL

This new query can be useful for analytics and debugging in the Concentrated Liquidity module.


Line range hint 85-87: Bug fixes: Improvements to CL math and swap operations

These bug fixes address important issues in the Concentrated Liquidity module, including reducing error blow-up in calculations and improving rounding behavior. These changes can lead to more accurate and stable operations.


Line range hint 88-88: Bug fix: Convert Int to BigDec in CL math

This change can help prevent potential precision loss or overflow issues in Concentrated Liquidity calculations.


Line range hint 92-97: API Breaks: Changes in CL module and poolmanager

These API breaks involve changes to function signatures and return types. While they may improve the overall design and functionality, ensure that all dependent code is updated accordingly. Special attention should be given to the SpotPrice API change, as it affects multiple modules.


Line range hint 1-5589: Overall CHANGELOG review

The CHANGELOG for v19.2.0 and v19.1.0 shows a focus on performance improvements, bug fixes, and API enhancements, particularly in the Concentrated Liquidity (CL) module and mathematical operations. The changes are well-documented, and breaking changes are clearly marked.

Key points:

  1. Several performance optimizations have been implemented, which should improve overall system efficiency.
  2. New queries and features have been added, enhancing functionality and developer tools.
  3. There are multiple API breaks, mainly in the CL module and mathematical operations. These changes seem to improve the design but require careful migration of dependent code.
  4. The switch to sdkmath types is a significant change that affects many parts of the codebase and requires special attention during migration.

Recommendations:

  1. Ensure comprehensive testing of all affected modules, especially the CL module and areas using mathematical operations.
  2. Provide detailed migration guides for developers to address the API breaks and the switch to sdkmath types.
  3. Consider adding more context or links to relevant documentation for some of the more complex changes.

Overall, the changes appear to be positive improvements to the system, but careful attention to breaking changes is necessary during the upgrade process.

@PaddyMc
Copy link
Contributor

PaddyMc commented Sep 24, 2024

No that's perfect, ty

@PaddyMc PaddyMc merged commit 4ce0dbf into osmosis-labs:main Sep 24, 2024
1 check passed
mergify bot pushed a commit that referenced this pull request Sep 24, 2024
* delete temp dir after it's usage

* update changelog

(cherry picked from commit 4ce0dbf)

# Conflicts:
#	CHANGELOG.md
PaddyMc added a commit that referenced this pull request Sep 26, 2024
…#8729)

* fix: multiple temp directories on command executions (#8726)

* delete temp dir after it's usage

* update changelog

(cherry picked from commit 4ce0dbf)

# Conflicts:
#	CHANGELOG.md

* chore: update changelog

---------

Co-authored-by: Marri Harish <[email protected]>
Co-authored-by: ghost <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v26.x backport patches to v26.x branch V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: node is creating multiple temp directories command executions
2 participants