Skip to content

fix: Retry flags only on transport errors#707

Closed
marandaneto wants to merge 1 commit into
mainfrom
pi-flags-network-retry-20260628
Closed

fix: Retry flags only on transport errors#707
marandaneto wants to merge 1 commit into
mainfrom
pi-flags-network-retry-20260628

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 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 self-assigned this Jun 28, 2026
@greptile-apps

greptile-apps Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread posthog/client.py
Comment on lines +906 to 919
flag_request_options: Dict[str, Any] = {}
if (
self.feature_flags_request_max_retries
!= DEFAULT_FEATURE_FLAGS_REQUEST_MAX_RETRIES
):
flag_request_options["max_retries"] = self.feature_flags_request_max_retries

resp_data = flags(
self.api_key,
self.host,
timeout=self.feature_flags_request_timeout_seconds,
**flag_request_options,
**request_data,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 The flag_request_options dict and its conditional guard are superfluous. flags() already defaults to DEFAULT_FEATURE_FLAGS_REQUEST_MAX_RETRIES, so passing the stored value unconditionally produces identical behaviour for every configuration — but is simpler and more obviously correct. The extra dict and branch also make the default code path invisible to tests: patch_flags.call_args.kwargs["max_retries"] would raise KeyError when the default is in use, forcing the test to cover only the non-default path.

Suggested change
flag_request_options: Dict[str, Any] = {}
if (
self.feature_flags_request_max_retries
!= DEFAULT_FEATURE_FLAGS_REQUEST_MAX_RETRIES
):
flag_request_options["max_retries"] = self.feature_flags_request_max_retries
resp_data = flags(
self.api_key,
self.host,
timeout=self.feature_flags_request_timeout_seconds,
**flag_request_options,
**request_data,
)
resp_data = flags(
self.api_key,
self.host,
timeout=self.feature_flags_request_timeout_seconds,
max_retries=self.feature_flags_request_max_retries,
**request_data,
)

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!

Comment on lines +987 to +1000
@mock.patch("posthog.client.flags")
def test_feature_flags_request_max_retries_is_forwarded_when_configured(
self, patch_flags
):
patch_flags.return_value = {"featureFlags": {}, "featureFlagPayloads": {}}
client = Client(
FAKE_TEST_API_KEY,
feature_flags_request_max_retries=0,
personal_api_key=FAKE_TEST_API_KEY,
)

client.get_all_flags("distinct_id")

self.assertEqual(patch_flags.call_args.kwargs["max_retries"], 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Non-parameterised test for the max_retries forwarding. The custom instructions prefer parameterised tests, and there are at least three distinct cases worth covering: the default value (1), zero (disable), and a non-default positive value (e.g. 3). A single case also doesn't verify that the default is forwarded at all, since the current conditional in client.py omits max_retries from the call when its value equals the default.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

@marandaneto

Copy link
Copy Markdown
Member Author

Closing in favor of a fork-backed branch because repository rules require signed commits on origin feature branches and the follow-up public API snapshot update could not be pushed directly. Replacement PR coming next.

@github-actions

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-28 14:23:19 UTC
Duration: 530088ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10008ms
Format Validation.Event Has Lib Properties 10006ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18018ms
Retry Behavior.Does Not Retry On 400 12004ms
Retry Behavior.Does Not Retry On 401 10006ms
Retry Behavior.Respects Retry After Header 16015ms
Retry Behavior.Implements Backoff 30017ms
Retry Behavior.Retries On 500 13010ms
Retry Behavior.Retries On 502 16010ms
Retry Behavior.Retries On 504 16011ms
Retry Behavior.Max Retries Respected 30014ms
Deduplication.Generates Unique Uuids 7006ms
Deduplication.Preserves Uuid On Retry 16014ms
Deduplication.Preserves Uuid And Timestamp On Retry 23020ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16005ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5004ms
Batch Format.Multiple Events Batched Together 10006ms
Error Handling.Does Not Retry On 403 12009ms
Error Handling.Does Not Retry On 413 10006ms
Error Handling.Retries On 408 14013ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9502ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10006ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10006ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10006ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10007ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9510ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10510ms

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.

1 participant