Skip to content

🛡️ Sentinel: Stop retrying on 4xx client errors#188

Closed
abhimehro wants to merge 1 commit intomainfrom
sentinel-retry-logic-9233204614472082297
Closed

🛡️ Sentinel: Stop retrying on 4xx client errors#188
abhimehro wants to merge 1 commit intomainfrom
sentinel-retry-logic-9233204614472082297

Conversation

@abhimehro
Copy link
Owner

Stopped retrying on 4xx errors (except 429) to prevent API spamming and improve error handling efficiency.


PR created automatically by Jules for task 9233204614472082297 started by @abhimehro

Previously, `_retry_request` would retry on any `httpx.HTTPError`, including 4xx client errors (e.g., 400 Bad Request, 401 Unauthorized). This is inefficient and can lead to API spamming or bans.

This change modifies `_retry_request` to:
- Check if the exception is an `httpx.HTTPStatusError`.
- If the status code is 4xx (and not 429 Too Many Requests), raise the exception immediately instead of retrying.
- Log the response content (sanitized) for debugging before raising.

This improves security by preventing potential DoS-like behavior against the API and failing fast on invalid inputs or auth failures.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 11, 2026 10:44
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@trunk-io
Copy link

trunk-io bot commented Feb 11, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@github-actions
Copy link

👋 Development Partner is reviewing this PR. Will provide feedback shortly.

@gemini-code-assist
Copy link

Summary of Changes

Hello @abhimehro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the application's robustness by modifying the retry logic for HTTP requests. It introduces a specific condition to prevent automatic retries for most 4xx client errors, ensuring that issues originating from incorrect client requests are not repeatedly attempted, thereby optimizing resource usage and preventing unintended API interactions.

Highlights

  • Error Handling: Implemented a mechanism to stop retrying HTTP requests when a 4xx client error (excluding 429 Too Many Requests) is encountered.
  • API Interaction: Prevents potential API spamming by immediately failing on non-retryable client errors.
  • Efficiency: Improves error handling efficiency by avoiding unnecessary retries for permanent client-side issues.
Changelog
  • main.py
    • Modified the _retry_request function to prevent retries on 4xx HTTP client errors, excluding 429 (Too Many Requests).
Activity
  • PR created automatically by Jules for task 9233204614472082297 started by @abhimehro.
  • Initial commit introduces logic to halt retries for 4xx HTTP errors, except for 429.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable change to stop retrying on 4xx client errors, with the exception of 429 for rate limiting. This is a solid approach to prevent spamming the API with invalid requests and improves error handling efficiency. The implementation is correct. I've added one suggestion to simplify the code by removing a redundant check, which will improve readability.

Comment on lines +607 to +609
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
raise

Choose a reason for hiding this comment

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

medium

This check for e.response is redundant. The isinstance(e, httpx.HTTPStatusError) check on line 604 already guarantees that e.response exists and is not None. You can simplify this block by removing the conditional check around the logging statement.

Suggested change
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
raise
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
raise

status = e.response.status_code
if 400 <= status < 500 and status != 429:
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the shared HTTP request retry helper to avoid retrying on non-rate-limit client errors, reducing unnecessary repeated requests when failures are permanent (e.g., auth/validation issues).

Changes:

  • Add a fast-fail path in _retry_request() for HTTP 4xx responses (except 429), immediately re-raising instead of retrying.
  • Log sanitized response content on these non-retriable 4xx failures for debugging consistency with the final-attempt logging.

Comment on lines +603 to +609
# Security: Don't retry on client errors (4xx) unless it's rate limiting (429)
if isinstance(e, httpx.HTTPStatusError):
status = e.response.status_code
if 400 <= status < 500 and status != 429:
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
raise
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

This change introduces new retry behavior (do not retry 4xx except 429), but there doesn't appear to be a test asserting the retry count/behavior for (a) a non-429 4xx (should call the request func once) and (b) 429 (should still retry). Adding focused unit tests around _retry_request for these status codes will prevent regressions.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +605 to +608
status = e.response.status_code
if 400 <= status < 500 and status != 429:
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

In the HTTPStatusError branch, e.response is accessed to read status_code before the hasattr(e, "response") / e.response is not None guard. That guard is therefore ineffective (and redundant for HTTPStatusError, which always carries a response from raise_for_status()). Consider removing the guard here, or moving the response existence check before reading status_code (e.g., via getattr(e, "response", None)).

Suggested change
status = e.response.status_code
if 400 <= status < 500 and status != 429:
if hasattr(e, "response") and e.response is not None:
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
# Use getattr to avoid AttributeError if a HTTPStatusError ever lacks a response
response_obj = getattr(e, "response", None)
status = response_obj.status_code if response_obj is not None else None
if status is not None and 400 <= status < 500 and status != 429:
if response_obj is not None:
log.debug(
f"Response content: {sanitize_for_log(response_obj.text)}"
)
# Don't retry non-rate-limited client errors

Copilot uses AI. Check for mistakes.
@abhimehro
Copy link
Owner Author

Closing: superseded by #191 (4xx retry behavior fixed).

@abhimehro abhimehro closed this Feb 13, 2026
@abhimehro abhimehro deleted the sentinel-retry-logic-9233204614472082297 branch February 13, 2026 01:59
@google-labs-jules
Copy link

Closing: superseded by #191 (4xx retry behavior fixed).

Understood. I will stop working on this task as it has been superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants