Repository navigation
Description
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): Raisesurllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')). Becauseurllib3's defaultRetrypolicy excludesPOSTfromallowed_methods, stale keep-alive disconnects onGETare silently retried byurllib3, whereasPOST /stores/{store_id}/checkimmediately raises. - Async client (
aiohttp):aiohttpsimilarly restricts persistent-connection auto-retry toIDEMPOTENT_METHODS(excludingPOST) and raisesaiohttp.ServerDisconnectedError/aiohttp.ClientOSError. - OAuth2 token exchange (
OAuth2Client): Both sync and async_obtain_tokenimplementations only retry HTTP429and5xxstatus codes, failing immediately on transient connection drops or timeouts to the token issuer. - Streaming endpoints (
streamed_list_objects/execute_streamed_api_request):RESTClientObject.streamcurrently returns a lazy generator / async generator before opening the HTTP connection, meaning connection-phase failures happen during iteration outsideApiClient's retry loop (and asyncrest.pycurrently 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:
-
Retry transient transport errors in
ApiClient(openfga_sdk/api_client.pyandopenfga_sdk/sync/api_client.py):- Async (
aiohttp): Catch transient transport errors such asaiohttp.ClientConnectionError(includingServerDisconnectedError,ClientConnectorError,ClientOSError) andasyncio.TimeoutError. - Sync (
urllib3): Catch transient transport errors such asurllib3.exceptions.ProtocolError(wrappinghttp.client.RemoteDisconnected),urllib3.exceptions.NewConnectionError,urllib3.exceptions.ConnectTimeoutError,urllib3.exceptions.ReadTimeoutError, and socket/connectionOSErrors. - Respect
configuration.retry_paramsand per-requestoptions["retry_params"](max_retry,min_wait_in_ms,max_wait_in_sec), includingmax_retry=0to disable retries. - Apply exponential backoff with jitter capped at
max_wait_in_sec(noRetry-Afterheader 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_retryis exhausted.
- Async (
-
Retry transient transport errors in
OAuth2Client(openfga_sdk/oauth2.pyandopenfga_sdk/sync/oauth2.py):- Catch the same transient
aiohttp/urllib3transport errors during_obtain_tokenand retry up tomax_retryusing exponential backoff with jitter (min_wait_in_ms,max_wait_in_sec).
- Catch the same transient
-
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.pyandopenfga_sdk/sync/rest.py(fixing asyncstream()so connection errors are raised rather than swallowed). - Retry transient network errors and
429/5xx(except501) during connection setup viaApiClient's retry loop. - Never retry once the stream has been established and chunks have begun yielding, matching the Go and JS SDKs.
- Eagerly establish the HTTP connection and validate the initial HTTP response status before yielding chunks in
-
Avoid hidden duplicate retries in
urllib3(openfga_sdk/sync/rest.py):- When
configuration.retries is None, passretries=Falsetourllib3.PoolManager/urllib3.ProxyManagersoRetryParamsis the single source of truth (ensuringRetryParams(max_retry=0)performs zero retries onGETas well asPOST), while still honoring an explicitconfiguration.retriesoverride if set by the caller.
- When
Acceptance criteria
- Both async (
openfga_sdk) and sync (openfga_sdk.sync)ApiClientretry transient connection/disconnection/timeout errors (includingProtocolError(RemoteDisconnected)andServerDisconnectedErroronPOST /check) up tomax_retrywith exponential backoff + jitter. - Both async and sync
OAuth2Clientretry transient network errors during token exchange up tomax_retry. -
streamed_list_objects/execute_streamed_api_request(sync and async) retry transient network errors and429/5xxduring initial connection establishment, and do not retry errors that occur mid-stream after chunks start arriving. - Per-call
options={"retry_params": RetryParams(...)}overrides (includingmax_retry=0) are honored for network errors. -
http.request.resend_counttelemetry attribute and request metrics are recorded accurately for retried network errors. - Documentation (
README.md) is updated to note that transient network errors are retried alongside429and5xxresponses.
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:
- PRs: openfga/go-sdk#176 (retry on network errors &
Retry-After) and openfga/go-sdk#182 (retry on network errors during OAuth2 token exchange) - API request network error retry (
determineRetrydefault:branch):api_executor.go - Streaming connection-phase retry (
ExecuteStreaming):api_executor.go - OAuth2 token exchange network error retry (
doTokenRoundTrip):oauth2/internal/token.go
- PRs: openfga/go-sdk#176 (retry on network errors &
- 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
- API request network error retry (
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIntake
Activity