Skip to content

Retry-After delay-seconds are not honored #341

Open

Description

@SoulPancake

Description

When a 429 or 5xx response carries Retry-After as delay-seconds, the SDK does not wait that long. _parse_retry_after_header tries the value as an HTTP date before trying int(), and dateutil parses integer strings as dates ("5" becomes the 5th of the current month, "60" becomes the year 2060). The result is outside the 1 to 1800 second range, so the SDK falls back to exponential backoff. Only values dateutil rejects, such as "31", reach int() and are honored.

The sync client has a second problem on GET, PUT and DELETE: urllib3's default Retry(3) handles Retry-After itself before the SDK sees the response. Retry-After: not-a-date raises urllib3.exceptions.InvalidHeader instead of falling back to backoff, and Retry-After: 1801 sleeps 30 minutes, bypassing the SDK's 1800 second cap.

Expectation

Delay-seconds between 1 and 1800 are honored as given, HTTP dates are converted to a delay, and anything else falls back to exponential backoff, on both clients.

Reproduction

Local server answers 429 with Retry-After: 5, then 200:

import asyncio
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread

from openfga_sdk import ClientConfiguration, OpenFgaClient

hits = 0


class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        global hits
        hits += 1
        if hits == 1:
            self.send_response(429)
            self.send_header("Retry-After", "5")
            self.end_headers()
            return
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(b'{"stores": [], "continuation_token": ""}')

    def log_message(self, *args):
        pass


server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
Thread(target=server.serve_forever, daemon=True).start()


async def main():
    config = ClientConfiguration(api_url=f"http://127.0.0.1:{server.server_port}")
    async with OpenFgaClient(config) as client:
        start = time.monotonic()
        await client.list_stores()
        print(f"requests={hits} waited={time.monotonic() - start:.2f}s")


asyncio.run(main())
  • On main: requests=2 waited=0.17s
  • Expected: requests=2 waited=5.01s

Sync client (openfga_sdk.sync.OpenFgaClient) against the same server with Retry-After: not-a-date:

urllib3.exceptions.InvalidHeader: Invalid Retry-After header: not-a-date

OpenFGA SDK version

0.10.6

OpenFGA version

n/a, reproduced against a local HTTP server

Fix in #340.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions