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

Suppress ClientConnectionResetError when returning logs #5358

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

sairon
Copy link
Member

@sairon sairon commented Oct 17, 2024

Proposed change

When client requests (or more often follows) some busy logs and closes the connection while the StreamWriter tries to write into it, an exception is raised. This should be harmless, and unless there's another way to handle this gracefully (I'm not aware of), then it should be safe to ignore the exception in this context.

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

    • Enhanced error handling for log streaming, improving robustness against client connection resets.
  • Bug Fixes

    • Improved handling of connection reset errors during log data fetching, ensuring smoother operation.

When client requests (or more often follows) some busy logs and closes
the connection while the StreamWriter tries to write into it, an
exception is raised. This should be harmless, and unless there's another
way to handle this gracefully (I'm not aware of), then it should be safe
to ignore the exception in this context.
Copy link

coderabbitai bot commented Oct 17, 2024

📝 Walkthrough

Walkthrough

The changes in the supervisor/api/host.py file focus on enhancing error handling related to client connection issues. An import statement for ClientConnectionResetError has been added, and a context manager using suppress(ClientConnectionResetError) has been implemented in the advanced_logs_handler method to ignore specific exceptions during log streaming. The handling of ConnectionResetError remains unchanged, where it raises an APIError for connection resets while fetching data. The overall structure and logic of the API methods are preserved.

Changes

File Change Summary
supervisor/api/host.py Added import for ClientConnectionResetError. Implemented context manager to suppress this error in advanced_logs_handler. Retained handling of ConnectionResetError to raise APIError.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant Logger

    Client->>API: Request logs
    API->>Logger: Stream logs
    alt Client connection reset
        Logger->>API: ClientConnectionResetError
        API->>API: Suppress ClientConnectionResetError
    else Fetching data
        API->>Logger: Fetch data from systemd-journald
        alt Connection reset occurs
            Logger->>API: ConnectionResetError
            API->>API: Raise APIError
        end
    end
    API->>Client: Send logs
Loading

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

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 and nitpick comments (1)
supervisor/api/host.py (1)

263-266: LGTM: Error handling improved as intended.

The changes effectively suppress the ClientConnectionResetError as described in the PR objectives. The comment explains the rationale clearly, and the suppress context manager is used correctly.

However, to improve code readability and maintainability, consider extracting the error suppression logic into a separate helper method. This would allow for easier testing and potential reuse if needed elsewhere.

Here's a suggested refactor:

def _write_log_line_suppressing_client_reset(self, response, line):
    with suppress(ClientConnectionResetError):
        return await response.write(line.encode("utf-8") + b"\n")

# Then in the method:
await self._write_log_line_suppressing_client_reset(response, line)

This refactoring would make the main method cleaner and the error handling logic more modular.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 96df335 and aa93edd.

📒 Files selected for processing (1)
  • supervisor/api/host.py (2 hunks)
🧰 Additional context used
🔇 Additional comments (2)
supervisor/api/host.py (2)

7-7: LGTM: Import statement added correctly.

The import of ClientConnectionResetError from aiohttp is necessary for the new error handling in the advanced_logs_handler method. It's placed appropriately with other imports from the same library.


Line range hint 1-266: Consider adding tests for the new error handling.

While the changes look good and align with the PR objectives, I noticed that no new tests were added. Given that this change involves error handling, it would be beneficial to add unit tests to verify the behavior of the advanced_logs_handler method with the new error suppression.

Here's a script to check for test files related to this module:

This script will help identify if there are existing test files where you could add new tests for this functionality.

@agners agners merged commit 6f196c9 into main Oct 21, 2024
22 checks passed
@agners agners deleted the suppress-client-conn-reset-logs branch October 21, 2024 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants