Skip to content

Retry transient network and connection errors in sync and async clients #342

Open

Description

@aaguiarz

Checklist

  • I have looked into the README and have not found a suitable solution or answer.
  • I have looked into the documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have upgraded to the latest version of OpenFGA and the issue still persists.
  • I have searched the Slack community and have not found a suitable solution or answer.
  • I agree to the terms within the OpenFGA Code of Conduct.

Description

Unlike the Go, JS, .NET, and Java OpenFGA SDKs, openfga-sdk for Python currently only retries HTTP 429 (RateLimitExceededError) and 5xx (ServiceException, excluding 501) responses. Transient transport/network errors bypass RetryParams completely and fail immediately on the first attempt.

In production, this surfaces most noticeably on POST endpoints such as check and batch_check when an idle pooled HTTP/1.1 keep-alive connection is closed by an upstream load balancer or proxy right as a request is dispatched:

  • Sync client (urllib3): Raises urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')). Because urllib3's default Retry policy excludes POST from allowed_methods, stale keep-alive disconnects on GET are silently retried by urllib3, whereas POST /stores/{store_id}/check immediately raises.
  • Async client (aiohttp): aiohttp similarly restricts persistent-connection auto-retry to IDEMPOTENT_METHODS (excluding POST) and raises aiohttp.ServerDisconnectedError / aiohttp.ClientOSError.
  • OAuth2 token exchange (OAuth2Client): Both sync and async _obtain_token implementations only retry HTTP 429 and 5xx status codes, failing immediately on transient connection drops or timeouts to the token issuer.
  • Streaming endpoints (streamed_list_objects / execute_streamed_api_request): RESTClientObject.stream currently returns a lazy generator / async generator before opening the HTTP connection, meaning connection-phase failures happen during iteration outside ApiClient's retry loop (and async rest.py currently logs and swallows connection setup exceptions instead of propagating them).

Describe the ideal solution

Bring the Python SDK (both async and sync) to parity with the Go and JS SDKs by retrying transient network, disconnection, and timeout errors under RetryParams:

  1. Retry transient transport errors in ApiClient (openfga_sdk/api_client.py and openfga_sdk/sync/api_client.py):

    • Async (aiohttp): Catch transient transport errors such as aiohttp.ClientConnectionError (including ServerDisconnectedError, ClientConnectorError, ClientOSError) and asyncio.TimeoutError.
    • Sync (urllib3): Catch transient transport errors such as urllib3.exceptions.ProtocolError (wrapping http.client.RemoteDisconnected), urllib3.exceptions.NewConnectionError, urllib3.exceptions.ConnectTimeoutError, urllib3.exceptions.ReadTimeoutError, and socket/connection OSErrors.
    • Respect configuration.retry_params and per-request options["retry_params"] (max_retry, min_wait_in_ms, max_wait_in_sec), including max_retry=0 to disable retries.
    • Apply exponential backoff with jitter capped at max_wait_in_sec (no Retry-After header is present on transport errors).
    • Increment TelemetryAttributes.http_request_resend_count (http.request.resend_count) and record request telemetry metrics consistently across retry attempts.
    • Re-raise the original exception once max_retry is exhausted.
  2. Retry transient transport errors in OAuth2Client (openfga_sdk/oauth2.py and openfga_sdk/sync/oauth2.py):

    • Catch the same transient aiohttp / urllib3 transport errors during _obtain_token and retry up to max_retry using exponential backoff with jitter (min_wait_in_ms, max_wait_in_sec).
  3. Retry initial connection establishment for streaming requests (streamed_list_objects / execute_streamed_api_request):

    • Eagerly establish the HTTP connection and validate the initial HTTP response status before yielding chunks in openfga_sdk/rest.py and openfga_sdk/sync/rest.py (fixing async stream() so connection errors are raised rather than swallowed).
    • Retry transient network errors and 429/5xx (except 501) during connection setup via ApiClient's retry loop.
    • Never retry once the stream has been established and chunks have begun yielding, matching the Go and JS SDKs.
  4. Avoid hidden duplicate retries in urllib3 (openfga_sdk/sync/rest.py):

    • When configuration.retries is None, pass retries=False to urllib3.PoolManager / urllib3.ProxyManager so RetryParams is the single source of truth (ensuring RetryParams(max_retry=0) performs zero retries on GET as well as POST), while still honoring an explicit configuration.retries override if set by the caller.

Acceptance criteria

  • Both async (openfga_sdk) and sync (openfga_sdk.sync) ApiClient retry transient connection/disconnection/timeout errors (including ProtocolError(RemoteDisconnected) and ServerDisconnectedError on POST /check) up to max_retry with exponential backoff + jitter.
  • Both async and sync OAuth2Client retry transient network errors during token exchange up to max_retry.
  • streamed_list_objects / execute_streamed_api_request (sync and async) retry transient network errors and 429/5xx during initial connection establishment, and do not retry errors that occur mid-stream after chunks start arriving.
  • Per-call options={"retry_params": RetryParams(...)} overrides (including max_retry=0) are honored for network errors.
  • http.request.resend_count telemetry attribute and request metrics are recorded accurately for retried network errors.
  • Documentation (README.md) is updated to note that transient network errors are retried alongside 429 and 5xx responses.

Alternatives and current workarounds

For the synchronous client only, users can currently work around stale keep-alive RemoteDisconnected errors by configuring urllib3.Retry directly on ClientConfiguration:

import urllib3

fga_config.retries = urllib3.Retry(
    total=2,
    connect=2,
    read=1,
    status=0,
    allowed_methods=None,
)

However, this only applies to the synchronous urllib3 client, bypasses RetryParams and per-call overrides, wraps exhausted errors in MaxRetryError, and is invisible to OpenFGA's OpenTelemetry metrics.

References

  • Go SDK implementation:
  • JS SDK implementation:
    • API request network error retry (checkIfRetryableError / isNetworkError & attemptHttpRequest): common.ts
    • Streaming connection-phase retry (createStreamingRequestFunction): common.ts
    • OAuth2 token exchange retry via attemptHttpRequest (refreshAccessToken): credentials/credentials.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions