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

Support for a circuit breaker to avoid recursive calls to atmos commands (infinite loop) #906

Merged
merged 4 commits into from
Jan 5, 2025

Conversation

Listener430
Copy link
Collaborator

@Listener430 Listener430 commented Jan 3, 2025

what

This PR contains the code that counts the number of recursive calls, and in case it exceeds the threshold, terminates the processes (parent + all children).

Add-on to atmos.yaml

atmos_yaml_addon

The circuit breaker in use

atmos_loop_termination

why

Avoid infinite loops

references

Summary by CodeRabbit

  • New Features

    • Added a circuit breaker mechanism to prevent infinite shell command loops.
    • Implemented maximum shell depth limit of 10 levels.
    • Introduced a new command loop for testing infinite loops in the CLI.
    • Enhanced environment variable tracking for nested shell executions.
  • Tests

    • Added a new test case to verify circuit breaker functionality.
    • Ensured CLI handles infinite loop scenarios gracefully.

@Listener430 Listener430 added the enhancement New feature or request label Jan 3, 2025
@Listener430 Listener430 requested a review from osterman January 3, 2025 16:14
@Listener430 Listener430 self-assigned this Jan 3, 2025
@Listener430 Listener430 requested a review from a team as a code owner January 3, 2025 16:14
Copy link
Contributor

coderabbitai bot commented Jan 3, 2025

📝 Walkthrough

Walkthrough

The pull request introduces a circuit breaker mechanism to prevent infinite loops in Atmos CLI custom commands. A new constant MaxShellDepth is defined to limit nested shell command execution to 10 levels. A new function getNextShellLevel() manages the ATMOS_SHLVL environment variable, ensuring that recursive command calls do not exceed the maximum depth. Test cases and a sample command are added to validate this new functionality.

Changes

File Change Summary
internal/exec/shell_utils.go Added MaxShellDepth constant, created getNextShellLevel() function, modified shell execution functions to track and limit shell depth
examples/tests/atmos.yaml Added loop command to test infinite loop scenario
tests/test_cases.yaml Added new test case "atmos circuit-breaker" to verify depth limitation

Assessment against linked issues

Objective Addressed Explanation
Detect cycles in CLI custom commands [#765, DEV-2688]
Implement recursive depth limit [DEV-2688]

Possibly related issues

  • DEV-2688: Directly addressed by implementing a recursive depth limit for Atmos commands.

Possibly related PRs

Suggested labels

major

Suggested reviewers

  • hans-d
  • osterman
  • Gowiem

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f6649c and 9551822.

📒 Files selected for processing (3)
  • examples/tests/atmos.yaml (1 hunks)
  • internal/exec/shell_utils.go (4 hunks)
  • tests/test_cases.yaml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • examples/tests/atmos.yaml
  • internal/exec/shell_utils.go
  • tests/test_cases.yaml

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

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: 0

🧹 Nitpick comments (3)
internal/exec/shell_utils.go (2)

27-46: Great approach for handling shell levels.
The logic cleanly enforces the depth limit. However, consider reusing getNextShellLevel() in execTerraformShellCommand to avoid duplicating the environment variable parsing.


58-63: Solid environment variable update.
Updating ATMOS_SHLVL here is consistent with the new approach. Also unify with the Terraform shell logic for consistency.

examples/demo-custom-command/atmos.yaml (1)

73-79: Neat demonstration of recursion.
The new “loop” command clearly shows how to trigger and test the circuit breaker. Consider adding a quick note in documentation to warn about potential infinite loops.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2763512 and 968c332.

📒 Files selected for processing (2)
  • examples/demo-custom-command/atmos.yaml (1 hunks)
  • internal/exec/shell_utils.go (4 hunks)
🔇 Additional comments (3)
internal/exec/shell_utils.go (3)

24-26: Nicely done with the constant definition.
A dedicated constant clarifies the maximum allowed depth.


116-121: Straightforward usage.
Happy to see consistency in retrieving the shell level.


143-148: Consistent shell level handling.
Using getNextShellLevel() keeps the recursion check uniform across methods.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 3, 2025
@osterman osterman added the minor New features that do not break anything label Jan 3, 2025
Copy link

mergify bot commented Jan 3, 2025

💥 This pull request now has conflicts. Could you fix it @Listener430? 🙏

@mergify mergify bot added the conflict This PR has conflicts label Jan 3, 2025
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

🧹 Nitpick comments (1)
examples/tests/atmos.yaml (1)

99-105: Improve command documentation and fix typo.

The command's documentation could be more informative, and there's a typo in the description.

Apply this diff to enhance the documentation:

   # test for infinite loop
   - name: loop
-    description: This command tests curcuit breaker for infinite loop
+    description: |
+      This command tests the circuit breaker mechanism by creating an infinite loop.
+      WARNING: This command intentionally creates an infinite loop that will be
+      terminated by the circuit breaker when shell depth exceeds 10.
     steps:
       - "echo Hello world!"
       - atmos loop
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 968c332 and 8845ddd.

📒 Files selected for processing (2)
  • examples/tests/atmos.yaml (1 hunks)
  • tests/test_cases.yaml (1 hunks)

tests/test_cases.yaml Outdated Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 3, 2025
Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Listener430

@aknysh aknysh merged commit 77258ba into main Jan 5, 2025
44 checks passed
@aknysh aknysh deleted the DEV-2688 branch January 5, 2025 20:24
Copy link

github-actions bot commented Jan 5, 2025

These changes were released in v1.136.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request minor New features that do not break anything
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants