Skip to content

fix: Retry flags only on transport errors#724

Merged
marandaneto merged 2 commits into
mainfrom
fix/flags-transport-retry
Jul 2, 2026
Merged

fix: Retry flags only on transport errors#724
marandaneto merged 2 commits into
mainfrom
fix/flags-transport-retry

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

Align Python /flags/?v=2 retry behavior with the other SDK fixes: retry only bounded network/transport/timeout failures, and do not retry HTTP/API status responses like 408, 429, or 5xx.

This keeps transient network handling while avoiding repeated requests when PostHog has already returned an API response.

💚 How did you test it?

  • uv run --extra test --extra dev ruff check posthog/request.py posthog/client.py posthog/__init__.py posthog/test/test_request.py posthog/test/test_client.py
  • uv run --extra test --extra dev python .github/scripts/check_public_api.py
  • uv run --extra test pytest posthog/test/test_request.py posthog/test/test_client.py

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented with pi. The retry policy was changed from urllib3 status-code retries to explicit /flags transport retries, with default max retries set to 1, 0 disabling retries, and exponential backoff starting at 300ms.

@marandaneto marandaneto requested a review from a team as a code owner July 2, 2026 09:51
@marandaneto

Copy link
Copy Markdown
Member Author

#708 was closed because its a fork and i cant run the sdk harness as a fork

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-02 10:26:50 UTC
Duration: 177188ms

✅ All Tests Passed!

46/46 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 516ms
Format Validation.Event Has Uuid 1508ms
Format Validation.Event Has Lib Properties 1506ms
Format Validation.Distinct Id Is String 1507ms
Format Validation.Token Is Present 1507ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9518ms
Retry Behavior.Does Not Retry On 400 3505ms
Retry Behavior.Does Not Retry On 401 3509ms
Retry Behavior.Respects Retry After Header 9509ms
Retry Behavior.Implements Backoff 23522ms
Retry Behavior.Retries On 500 7514ms
Retry Behavior.Retries On 502 7513ms
Retry Behavior.Retries On 504 7511ms
Retry Behavior.Max Retries Respected 23517ms
Deduplication.Generates Unique Uuids 1509ms
Deduplication.Preserves Uuid On Retry 7513ms
Deduplication.Preserves Uuid And Timestamp On Retry 14521ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7507ms
Deduplication.No Duplicate Events In Batch 1504ms
Deduplication.Different Events Have Different Uuids 1507ms
Compression.Sends Gzip When Enabled 1507ms
Batch Format.Uses Proper Batch Structure 1506ms
Batch Format.Flush With No Events Sends Nothing 1005ms
Batch Format.Multiple Events Batched Together 1505ms
Error Handling.Does Not Retry On 403 3509ms
Error Handling.Does Not Retry On 413 3507ms
Error Handling.Retries On 408 7512ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1004ms
Request Payload.Flags Request Uses V2 Query Param 1006ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1006ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1006ms
Request Lifecycle.No Flags Request On Init Alone 502ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1010ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Retry Behavior.Retries Flags On 502 1005ms
Retry Behavior.Retries Flags On 504 1007ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1508ms

@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. posthog/test/test_request.py, line 507-549 (link)

    P2 Prefer @parameterized.expand over subTest loops

    Two tests (test_flags_retries_contract_http_status_errors and test_flags_does_not_retry_other_http_status_errors) iterate over status code lists with for … subTest rather than using @parameterized.expand, which is the team standard (see test_feature_flags_request_max_retries_is_forwarded added in this same PR for the correct pattern). Failures in subTest loops are harder to locate in CI output, and a future reader must scan the loop body to understand what inputs are covered. Both tests are good candidates for @parameterized.expand([(502,), (504,)]) and @parameterized.expand([(408,), (429,), (500,), (503,)]) respectively.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "fix: retry flags only on transport error..." | Re-trigger Greptile

Comment thread posthog/request.py Outdated
@marandaneto

Copy link
Copy Markdown
Member Author

@ioannisj blind approval pls because of #724 (comment)

@marandaneto

marandaneto commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

Addressed the Greptile feedback in fe937c6: converted the /flags HTTP status retry tests from subTest loops to @parameterized.expand, and also de-duplicated retry bookkeeping from the inline comment.

@marandaneto marandaneto merged commit 112c5fc into main Jul 2, 2026
36 checks passed
@marandaneto marandaneto deleted the fix/flags-transport-retry branch July 2, 2026 10:34
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.

2 participants