Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 10, 2025

The retry logic in the streaming backoff implementation had three critical flaws that caused incorrect behavior during rate limiting scenarios:

Issues Fixed

1. Logger attempt count stuck at 1
The attempt number in log messages was calculated using options.max_attempts - max_attempts + 1, but since options.max_attempts always referenced the original value while max_attempts decreased, the calculation remained constant at 1 for all retry attempts.

2. Incorrect exponential backoff delays
The exponential delay calculation used base_delay * (2 ** (options.max_attempts - max_attempts)), which resulted in non-exponential delays like 0.13s → 0.29s → 0.20s instead of the expected exponential progression.

3. Logger names growing with recursive calls
Each recursive retry call created a new RetryOptions with logger.getChild("@teams/retry"), causing nested logger names like parent.@teams/retry.@teams/retry.@teams/retry.

Solution

  • Added attempt_number parameter to RetryOptions to properly track the current attempt number
  • Fixed exponential delay calculation to use base_delay * (2 ** (attempt_number - 1)) for true exponential backoff (0.5s → 1.0s → 2.0s → 4.0s)
  • Added _internal_logger parameter to pass the existing child logger between recursive calls, preventing nested logger names
  • Enhanced test coverage with comprehensive tests validating all three fixes

Before/After Example

# Before: Incorrect delays and attempt numbers
# Delaying 0.13s before retry (attempt 1)...
# Delaying 0.29s before retry (attempt 1)...
# Delaying 0.20s before retry (attempt 1)...

# After: Correct exponential backoff and attempt tracking
# Delaying 0.50s before retry (attempt 1)...
# Delaying 1.00s before retry (attempt 2)...  
# Delaying 2.00s before retry (attempt 3)...

This ensures proper rate limit handling in streaming scenarios where Microsoft Teams API calls need reliable exponential backoff.

Fixes #145.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor Author

Copilot AI commented Sep 10, 2025

@heyitsaamir 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] [py] Streaming backoff logic is flawed Fix streaming backoff logic flaws in retry module Sep 11, 2025
Copilot AI requested a review from heyitsaamir September 11, 2025 00:08
Copilot finished work on behalf of heyitsaamir September 11, 2025 00:08
@heyitsaamir heyitsaamir marked this pull request as ready for review September 11, 2025 00:10
Copilot AI review requested due to automatic review settings September 11, 2025 00:10
Copy link
Contributor

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 fixes critical flaws in the streaming backoff retry logic that caused incorrect attempt counting, non-exponential delays, and nested logger names during recursive retry calls.

  • Fixed attempt number calculation to properly increment from 1 instead of staying constant
  • Corrected exponential backoff formula to use attempt_number - 1 for true exponential progression
  • Prevented logger name nesting by passing existing child logger through recursive calls

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/apps/src/microsoft/teams/apps/utils/retry.py Added attempt_number and _internal_logger parameters, fixed exponential delay calculation and logger passing
packages/apps/tests/test_retry.py Added comprehensive test suite covering attempt counting, exponential backoff, and logger behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@MehakBindra MehakBindra self-requested a review September 11, 2025 20:29
Copy link
Contributor

@MehakBindra MehakBindra left a comment

Choose a reason for hiding this comment

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

Looks good, thanks Aamir !

@MehakBindra MehakBindra merged commit b003046 into main Sep 11, 2025
4 checks passed
@MehakBindra MehakBindra deleted the copilot/fix-145 branch September 11, 2025 20:30
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.

[py] Streaming backoff logic is flawed

3 participants