8000 Add blockbuster option to API by mdegat01 · Pull Request #5746 · home-assistant/supervisor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add blockbuster option to API #5746

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

Merged
merged 2 commits into from
Mar 25, 2025
Merged

Add blockbuster option to API #5746

merged 2 commits into from
Mar 25, 2025

Conversation

mdegat01
Copy link
Contributor
@mdegat01 mdegat01 commented Mar 12, 2025

Proposed change

Add an option to enable/disable blockbuster via Supervisor API for use on dev systems to track down any additional blocking I/O in the event loop.

The reason the option is not binary is because on_at_startup is a bit dangerous. Blockbuster will cause any attempts to do blocking I/O in the event loop to raise. During startup this will result in a supervisor crash loop that will require some manual effort on the CLI to fix.

on on the other hand is always safe. It may result in individual tasks failing with error logs but Supervisor will keep running. And in the worst case a crash and restart of Supervisor will disable it, you won't get a crash loop.

Basically I want to allow someone to run it at startup if they want but I want them to really mean it and be prepared to deal with catastrophic errors 😄

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:
  • Link to client library 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 or add-on configuration are added/changed:

Summary by CodeRabbit

  • New Features
    • Introduced a blocking I/O detection feature that lets users control its behavior via configuration and API options. Users can now set detection to be enabled immediately, enabled at startup, or disabled.
  • Bug Fixes
    • Improved the handling of blocking I/O detection settings in the API, ensuring accurate responses based on user input.
  • Tests
    • Added tests to validate the new blocking I/O detection configurations, ensuring that changes correctly reflect in system responses and error handling.

@mdegat01 mdegat01 added missing-documentation Added to pull requests that needs a docs, but none is linked new-feature A new feature need-cli Added to pull requests that need cli changes but none is linked need-client-library Added to pull requests that need client library changes but none is linked labels Mar 12, 2025
@mdegat01 mdegat01 requested a review from agners March 12, 2025 18:24
@mdegat01 mdegat01 force-pushed the add-blockbuster-option branch from 0d04fbd to 733ed3e Compare March 12, 2025 18:25
Copy link
Contributor
coderabbitai bot commented Mar 12, 2025
📝 Walkthrough

Walkthrough

This pull request integrates a blocking I/O detection feature into the supervisor. The changes update the startup routine to conditionally activate the blocking detector, add API support for managing the detection state using a new enum and attribute, and extend configuration management with getter/setter and schema validation. A new utility module encapsulates the logic for activating, deactivating, and checking the blocking I/O detection. In addition, corresponding test cases and fixture modifications are introduced to verify the correct behavior.

Changes

File(s) Change Summary
supervisor/__main__.py Reorganized import statements; added a conditional check in run_os_startup_check_cleanup to call activate_blockbuster() if blocking I/O detection is enabled.
supervisor/api/const.py
supervisor/api/supervisor.py
Added a new enum class DetectBlockingIO for state management and introduced the attribute ATTR_DETECT_BLOCKING_IO in the API schema. The options method now handles values (ON, ON_AT_STARTUP, OFF) by invoking activate_blockbuster() or deactivate_blockbuster(), and reporting the current state via blockbuster_enabled().
supervisor/config.py
supervisor/const.py
supervisor/validate.py
Introduced a new configuration attribute for blocking I/O detection. A corresponding property (getter and setter) is added to CoreConfig, a new constant ATTR_DETECT_BLOCKING_IO is defined, and the validation schema is updated to include this boolean option with a default of False.
supervisor/utils/blockbuster.py New module that implements the BlockBuster functionality. It provides functions to activate (activate_blockbuster()), deactivate (deactivate_blockbuster()), and check (blockbuster_enabled()) the blocking I/O detection, using an LRU-cached helper _get_blockbuster() to manage the instance.
tests/api/test_supervisor.py
tests/conftest.py
Added a new test function to verify the supervisor’s handling of the blocking I/O detection settings, including error behavior, and updated the blockbuster fixture to conditionally return a BlockBuster instance or None based on the request parameter.

Sequence Diagram(s)

sequenceDiagram
    participant SM as Supervisor Main
    participant CC as CoreConfig
    participant BB as Blockbuster Module
    SM ->> CC: Read detect_blocking_io configuration
    CC -->> SM: Return boolean value
    alt Detection is enabled
        SM ->> BB: Call activate_blockbuster()
    end
Loading
sequenceDiagram
    participant Client as API Client
    participant API as Supervisor API
    participant Config as CoreConfig
    participant BB as Blockbuster Module
    Client ->> API: Send options request with ATTR_DETECT_BLOCKING_IO value
    API ->> API: Validate and interpret the option value
    alt Value is ON or ON_AT_STARTUP
        API ->> BB: Call activate_blockbuster() [if ON]
        API ->> Config: Update configuration (enable detection)
    else Value is OFF
        API ->> BB: Call deactivate_blockbuster()
        API ->> Config: Update configuration (disable detection)
    end
    API -->> Client: Return response with updated configuration
Loading

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 733ed3e and 099d76b.

📒 Files selected for processing (9)
  • supervisor/__main__.py (2 hunks)
  • supervisor/api/const.py (1 hunks)
  • supervisor/api/supervisor.py (5 hunks)
  • supervisor/config.py (2 hunks)
  • supervisor/const.py (1 hunks)
  • supervisor/utils/blockbuster.py (1 hunks)
  • supervisor/validate.py (2 hunks)
  • tests/api/test_supervisor.py (2 hunks)
  • tests/conftest.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (7)
  • supervisor/const.py
  • supervisor/api/const.py
  • supervisor/config.py
  • supervisor/validate.py
  • tests/conftest.py
  • supervisor/main.py
  • supervisor/utils/blockbuster.py
🧰 Additional context used
🧬 Code Definitions (2)
tests/api/test_supervisor.py (3)
tests/conftest.py (2) (2)
  • blockbuster (68:81)
  • coresys (324:404)
supervisor/coresys.py (1) (1)
  • config (169:171)
supervisor/config.py (2) (2)
  • detect_blocking_io (147:149)
  • detect_blocking_io (152:154)
supervisor/api/supervisor.py (3)
supervisor/utils/blockbuster.py (3) (3 8000 )
  • activate_blockbuster (26:29)
  • blockbuster_enabled (17:23)
  • deactivate_blockbuster (32:35)
supervisor/config.py (2) (2)
  • detect_blocking_io (147:149)
  • detect_blocking_io (152:154)
supervisor/api/const.py (1) (1)
  • DetectBlockingIO (85:90)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Build armv7 supervisor
  • GitHub Check: Build armhf supervisor
  • GitHub Check: Build aarch64 supervisor
  • GitHub Check: Run tests Python 3.13.2
  • GitHub Check: Check pylint
🔇 Additional comments (15)
tests/api/test_supervisor.py (9)

4-4: Import addition is appropriate.

The time module is correctly imported as it's used for the time.sleep(0) calls in the new test function.


8-8: Necessary import for testing blocking detection.

The BlockingError exception from blockbuster is appropriately imported to verify that blocking I/O operations raise the expected exception when detection is enabled.


254-258: Well-structured parameterized test.

The test correctly uses parameterization to cover both enabling detection temporarily (on) and at startup (on_at_startup), while using the "no_blockbuster" fixture parameter to avoid interference from the test framework's own blockbuster detection.


259-265: Test setup is clear and the initial sleep verifies detection is off.

The first time.sleep(0) call verifies that blocking detection isn't active at the start of the test, which establishes a good baseline for the test.


266-275: API interaction is thoroughly tested.

The test correctly verifies:

  1. That the API responds with status 200 when enabling detection
  2. That the supervisor info endpoint reflects the updated detection state

276-277: Important configuration distinction is verified.

This assertion correctly verifies that enabling detection with on only affects the current run, while on_at_startup persists the setting to the configuration.


279-281: Detection verification is thorough.

Using pytest.raises(BlockingError) is an excellent way to verify that blocking I/O detection is actually working, not just configured.


282-291: Disabling detection is properly tested.

The test confirms that:

  1. The API can disable detection after enabling it
  2. The supervisor info reflects this change
  3. The configuration is updated correctly

293-294: Verification of disabled detection is complete.

The final time.sleep(0) call confirms that blocking detection is truly disabled, as it no longer raises an exception.

supervisor/api/supervisor.py (6)

23-23: Appropriate const import.

The ATTR_DETECT_BLOCKING_IO constant is correctly imported from the ..const module.


51-55: Utility imports are well-organized.

The blockbuster utility functions are imported in a clean, grouped manner, correctly following the project's import style.


59-59: Appropriate enum import.

The DetectBlockingIO enum is correctly imported from the .const module.


78-78: Schema definition is properly extended.

The ATTR_DETECT_BLOCKING_IO is correctly added to the options schema with appropriate conversion to the DetectBlockingIO enum type.


111-111: Info endpoint correctly includes blocking detection state.

The current state of blocking I/O detection is correctly included in the supervisor info response using the blockbuster_enabled() function.


171-181: Blocking detection option handling is comprehensive.

The implementation correctly handles all three states of the DetectBlockingIO enum:

  1. ON_AT_STARTUP: Enables detection at startup (persistent) and activates it for the current session
  2. ON: Activates detection for the current session only
  3. OFF: Disables detection at startup and deactivates it for the current session

This matches the PR objectives which note that enabling at startup could cause supervisor crash loops, so it's properly treated as a distinct option.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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 or @coderabbitai title 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.

@mdegat01 mdegat01 force-pushed the add-blockbuster-option branch from 733ed3e to 099d76b Compare March 18, 2025 18:21
@agners agners merged commit 80f7f07 into main Mar 25, 2025
22 checks passed
@agners agners deleted the add-blockbuster-option branch March 25, 2025 08:40
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed missing-documentation Added to pull requests that needs a docs, but none is linked need-cli Added to pull requests that need cli changes but none is linked need-client-library Added to pull requests that need client library changes but none is linked new-feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0