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 addon in wrong state after restore #5111

Merged
merged 2 commits into from
Jun 4, 2024
Merged

Conversation

mdegat01
Copy link
Contributor

@mdegat01 mdegat01 commented May 28, 2024

Proposed change

Full restore currently stops the docker events listener before it begins as part of shutdown. It is not restarted afterwards leaving the addons in completely unknown states beyond that point. This PR fixes that issue, the docker monitor is no longer stopped.

It also fixes a minor possible issue where addons were not loaded prior to being restarted during a restore due to an exception. The first is the real cause of these "unknown state after restore" issues though.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints of add-on configuration are added/changed:

Summary by CodeRabbit

  • Bug Fixes

    • Ensured add-ons are properly loaded before running by moving the check to a finally block.
  • Improvements

    • Improved system shutdown process by removing unnecessary stopping of docker monitoring.
  • Tests

    • Added tests to verify addon state monitoring after full and partial restore operations.

@mdegat01 mdegat01 added the bugfix A bug fix label May 28, 2024
@mdegat01 mdegat01 force-pushed the addon-not-started-restore branch from b5a399a to 9ee7593 Compare June 3, 2024 19:40
@mdegat01 mdegat01 marked this pull request as ready for review June 3, 2024 20:52
Copy link

coderabbitai bot commented Jun 3, 2024

Walkthrough

Walkthrough

The updates involve ensuring the add-on is loaded before running it in the STARTED state, removing the stopping of Docker monitoring during shutdown, and adding tests to verify add-on state monitoring after full and partial restores.

Changes

File Change Summary
supervisor/addons/addon.py Moved the code block checking if the add-on is loaded to a finally block in _restore_data to ensure it runs before proceeding if in the STARTED state.
supervisor/core.py Removed the stopping of Docker monitoring in the shutdown method of the Core class.
tests/backups/test_manager.py Added new async test functions to verify add-on state monitoring post full and partial restore operations.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant CoreSys
    participant Addon
    participant Docker

    User->>CoreSys: Initiate shutdown
    CoreSys->>Docker: Stop Docker monitoring (removed)
    CoreSys->>User: Shutdown complete

    User->>CoreSys: Initiate restore
    CoreSys->>Addon: Restore data
    Addon->>Addon: Ensure add-on is loaded
    Addon->>CoreSys: Add-on loaded
    CoreSys->>User: Restore complete

    User->>CoreSys: Run add-on (STARTED state)
    CoreSys->>Addon: Check if loaded (moved to finally block)
    Addon->>User: Add-on running
Loading

Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between e57de4a and 9ee7593.

Files selected for processing (3)
  • supervisor/addons/addon.py (1 hunks)
  • supervisor/core.py (1 hunks)
  • tests/backups/test_manager.py (1 hunks)
Additional context used
Path-based instructions (1)
supervisor/core.py (4)

Pattern */**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.

  • Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
  • In step-by-step instructions, front the location phrase in the instructional sentence.
  • In step-by-step instructions, front the 'goal' in the instructional sentence.
  • In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
  • do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'

Pattern */**(html|markdown|md): - Use bold to mark UI strings.

  • If "" are used to mark UI strings, replace them by bold.

Pattern */**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"


Pattern */**(html|markdown|md): - Use sentence-style capitalization also in headings.

Additional comments not posted (3)
supervisor/addons/addon.py (1)

1346-1346: Change approved: Ensuring add-ons are loaded in the finally block enhances reliability during the restore process.

Verification successful

The _restore_data method in supervisor/addons/addon.py correctly places the addon.is_loaded check in the finally block. This ensures that the add-on's loaded state is verified regardless of any exceptions that might occur during the restore process. This change enhances the reliability of the restore process.

  • The finally block ensures that the add-on is loaded and started if necessary, which is crucial for maintaining the expected state of the add-on.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the addon loading check in the `finally` block does not introduce any side effects or errors during the restore process.

# Test: Search for any exceptions or errors logged related to addon loading during restore.
rg --type python $'addon.is_loaded' -A 5 -B 5

Length of output: 75



Script:

#!/bin/bash
# Description: Verify that the addon loading check in the `finally` block does not introduce any side effects or errors during the restore process.

# Test: Search for any exceptions or errors logged related to addon loading during restore.
rg 'addon.is_loaded' -A 5 -B 5

Length of output: 30



Script:

#!/bin/bash
# Description: Verify the implementation of the `_restore_data` method to ensure the `addon.is_loaded` check is correctly placed in the `finally` block.

# Test: Search for the `_restore_data` method in the codebase and review its implementation.
rg 'def _restore_data' -A 20

Length of output: 3036



Script:

#!/bin/bash
# Description: Verify the complete implementation of the `_restore_data` method in `supervisor/addons/addon.py` to ensure the `addon.is_loaded` check is correctly placed in the `finally` block.

# Test: Search for the complete implementation of the `_restore_data` method in `supervisor/addons/addon.py`.
rg 'def _restore_data' -A 50 supervisor/addons/addon.py

Length of output: 2175

tests/backups/test_manager.py (2)

1755-1773: The test function test_monitoring_after_full_restore correctly sets up the environment and asserts the expected behavior after a full restore. It ensures that Docker monitoring is not stopped, aligning with the PR's objectives.


1775-1789: The test function test_monitoring_after_partial_restore correctly sets up the environment and asserts the expected behavior after a partial restore. It ensures that Docker monitoring is not stopped, aligning with the PR's objectives.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@mdegat01 mdegat01 requested review from agners and frenck June 3, 2024 20:52
Copy link

@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 comments (1)
supervisor/core.py (1)

[!TIP]
Codebase Verification

The reference to sys_docker.unload() is part of a list of coroutines that are being stopped during the shutdown process. This indicates that Docker monitoring is still being unloaded during shutdown, which contradicts the intended change.

  • supervisor/core.py: The sys_docker.unload() call is still present in the shutdown process.
Analysis chain

Line range hint 318-318: Change approved: Docker monitoring is no longer unloaded during shutdown.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that Docker monitoring remains active during the shutdown process and does not negatively affect other operations.

# Test: Search for any other references to `sys_docker.unload` to ensure it's not called elsewhere during shutdown.
rg --type python $'sys_docker.unload'

Length of output: 67



Script:

#!/bin/bash
# Description: Verify that Docker monitoring remains active during the shutdown process and does not negatively affect other operations.

# Test: Search for any other references to `sys_docker.unload` to ensure it's not called elsewhere during shutdown.
rg 'sys_docker.unload'

Length of output: 95



Script:

#!/bin/bash
# Description: Extract the relevant lines around the reference to `sys_docker.unload` in `supervisor/core.py` to understand its context.

# Extract lines around the reference to `sys_docker.unload`
rg 'sys_docker.unload' -A 5 -B 5 supervisor/core.py

Length of output: 475

Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

Hm, I see there is another sys_docker.unload() in stop(), which should take care in the actual shutdown case. Maybe shutdown() as a function name on the (Supervisor-)Core class is a bit misleading 😅

Nice find, LGTM!

@agners agners merged commit c4452a8 into main Jun 4, 2024
19 checks passed
@agners agners deleted the addon-not-started-restore branch June 4, 2024 14:17
@agners
Copy link
Member

agners commented Jun 4, 2024

The restore progress actually timed out on the add-on startups:

2024-06-04 16:23:57.102 WARNING (MainThread) [supervisor.addons.addon] Timeout while waiting for addon Terminal & SSH to start, took more than 120 seconds
2024-06-04 16:23:57.944 WARNING (MainThread) [supervisor.addons.addon] Timeout while waiting for addon OpenThread Border Router to start, took more than 120 seconds

While they were actually already running:

# docker ps
CONTAINER ID   IMAGE                                                                      COMMAND               CREATED          STATUS          PORTS                                   NAMES
2dacce55c61a   homeassistant/amd64-addon-otbr:2.6.0                                       "/init"               7 minutes ago    Up 7 minutes                                            addon_core_openthread_border_router
f919ae671c09   homeassistant/amd64-addon-ssh:9.14.0                                       "/init"               7 minutes ago    Up 7 minutes                                            addon_core_ssh
...

Which make sense, since the Docker event monitor wasn't running at this stage.

@agners
Copy link
Member

agners commented Jun 4, 2024

These timeouts did also appear in this issue report, so this most likely fixes #4814.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants