Skip to content

fix(issue #531): Not able to log "retry_success", only if retry was triggered - #704

Closed
arunsoman wants to merge 1 commit into
jd:mainfrom
arunsoman:forge/fix-issue-531
Closed

fix(issue #531): Not able to log "retry_success", only if retry was triggered#704
arunsoman wants to merge 1 commit into
jd:mainfrom
arunsoman:forge/fix-issue-531

Conversation

@arunsoman

Copy link
Copy Markdown

Fixes #531

What

autonomous fix via atomic-forge fix — CIE (code graph over MCP) localized the bug, generated a failing regression test, and forge's repair loop fixed the source against it.

Issue

Not able to log "retry_success", only if retry was triggered

PROBLEM:
Trying to generate success message only if retry got triggered.
Able to generate the warning properly, but not success if retry happened
Using the following code:

import random
import logging
from tenacity import retry, wait_fixed, stop_after_attempt, before_sleep, RetryCallState, retry_if_exception_type

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def my_before_sleep(retry_state: RetryCallState):
    if retry_state.outcome.failed:
        # If it failed, don't do anything special here; before_sleep handles warnings.
        logging.warning(
        f"Retrying '{retry_state.fn.__name__}' for the {retry_state.attempt_number} time "
        f"after {retry_state.outcome.exception()}..."
        )
        return retry_state.outcome.result # Return the exception for tenacity to handle
    else:
        # THIS ELSE IS NOT WORKING, AS EXPECTED
        logging.info(
        f"'{retry_state.fn.__name__}' completed successfully after retries "
        f"(attempt {retry_state.attempt_number}). Result: {retry_state.outcome.result}"
        )

def my_after_execute(retry_state: RetryCallState):
    """
    Callback function executed after each attempt, regardless of success or failure.
    Checks if the function succeeded AND if retries previously occurred.
    """
    if not retry_state.outcome.failed:
        # IF THE ABOVE ELSE IS NOT SUPPOSED TO WORK, EVEN THIS IF CONDITION IS NOT WORKING.
        logging.info(
        f"'{retry_state.fn.__name__}' completed successfully after retries "
        f"(attempt {retry_state.attempt_number}). Result: {retry_state.outcome.result}"
        )
    return retry_state.outcome.result # Return the actual result for tenacity to propagate

@retry(
    wait=wait_fixed(1),
    stop=stop_after_attempt(3),
    retry=retry_if_exception_type(ValueError),
    before_sleep=my_before_sleep,
    after=my_after_execute,
    reraise=True
)
def might_fail_function(prob):
    # Simulate suc

## How it was verified
- regression test: `tests/test_forge_531.py` (CIE-generated; fails on the pre-fix code, passes on the fix)
- repair rounds: 1  failures: 1 -> 0
- repaired file(s): tenacity/__init__.py


---
🏛️ Generated end-to-end by [atomic-forge](https://github.com/arunsoman/atomic-forge) — an autonomous, test-driven issue→PR repair engine (`atomic-forge fix <issue-url>`).

@arunsoman

Copy link
Copy Markdown
Author

Superseded by #705 — reopened from our org's fork instead of a personal account. Same fix, same commit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not able to log "retry_success", only if retry was triggered

1 participant