Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): tolerate fractional Retry-After headers in warehouse sync HTTP retries (#74113)

Tom Owers30ad54b3c348
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/http/transport.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/http/transport.py
index cb3e33da56c8..dbb2805fac39 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/http/transport.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/http/transport.py
@@ -14,6 +14,7 @@ want the metering — they only need to mount the tracked adapter:
from __future__ import annotations
+import math
import time
from collections.abc import Mapping
from typing import Any
@@ -21,21 +22,44 @@ from typing import Any
import requests
from requests import PreparedRequest, Response
from requests.adapters import HTTPAdapter
+from urllib3.exceptions import InvalidHeader
from urllib3.util.retry import Retry
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http.observer import record_request
class BoundedRetry(Retry):
- """`Retry` that caps the honoured `Retry-After` sleep.
-
- A server can send a `Retry-After` far larger than any sane wait — an absurd
- integer or a date decades out. urllib3 passes it straight to `time.sleep`,
- which raises `OverflowError` once it exceeds what a C `PyTime_t` can hold,
- killing the sync. Bounding it to the backoff ceiling keeps a hostile value
- from overflowing (and from parking a worker for hours).
+ """`Retry` that hardens `Retry-After` handling against hostile or sloppy servers.
+
+ Two failure modes are covered:
+
+ - A server can send a `Retry-After` far larger than any sane wait — an absurd
+ integer or a date decades out. urllib3 passes it straight to `time.sleep`,
+ which raises `OverflowError` once it exceeds what a C `PyTime_t` can hold,
+ killing the sync. `get_retry_after` bounds it to the backoff ceiling, keeping
+ a hostile value from overflowing (and from parking a worker for hours).
+ - RFC 9110 requires `Retry-After` to be an integer delay-seconds or an
+ HTTP-date, and urllib3's default parsing raises `InvalidHeader` on anything
+ else. Some upstream APIs send fractional seconds (e.g. "0.129") instead, which
+ otherwise turns a should-be-transient rate limit into a hard failure.
+ `parse_retry_after` tolerates fractional values and falls back to no delay.
"""
+ def parse_retry_after(self, retry_after: str) -> float:
+ try:
+ return super().parse_retry_after(retry_after)
+ except InvalidHeader:
+ try:
+ seconds = float(retry_after)
+ except ValueError:
+ return 0.0
+ # `float()` accepts "NaN"/"Infinity"; a non-finite delay would slip past
+ # `min()`/`max()` (which don't order NaN) and reach `time.sleep`, raising
+ # ValueError. Treat non-finite values as "no delay".
+ if not math.isfinite(seconds):
+ return 0.0
+ return max(seconds, 0.0)
+
def get_retry_after(self, response: Any) -> float | None:
retry_after = super().get_retry_after(response)
if retry_after is None:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_http_transport.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_http_transport.py
index 786e3a374073..881efc4e418e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_http_transport.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_http_transport.py
@@ -8,6 +8,7 @@ from urllib3.util.retry import Retry
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http.transport import (
DEFAULT_RETRY,
+ BoundedRetry,
TrackedHTTPAdapter,
_NoRedirectSession,
make_tracked_adapter,
@@ -110,6 +111,29 @@ def test_make_tracked_session_merges_headers():
assert session.headers["User-Agent"] == "posthog/test"
+ "header_value,expected",
+ [
+ ("5", 5),
+ ("0.129", 0.129),
+ (" 1.5 ", 1.5),
+ ("not-a-number", 0.0),
+ # Non-finite values `float()` accepts but `time.sleep` rejects — must
+ # collapse to no delay rather than reach the sleep call.
+ ("NaN", 0.0),
+ ("Infinity", 0.0),
+ ("-Infinity", 0.0),
+ ],
+)
+def test_lenient_retry_after_parses_fractional_seconds_without_raising(header_value, expected):
+ # Some upstream APIs send fractional-second Retry-After values (e.g. "0.129"),
+ # which isn't RFC 9110-compliant and makes urllib3's default parsing raise
+ # InvalidHeader, crashing the sync instead of backing off.
+ retry = BoundedRetry(total=3)
+
+ assert retry.parse_retry_after(header_value) == expected
+
+
def test_make_tracked_adapter_with_none_retry_uses_default():
"""`retry=None` is the explicit "use default" sentinel — not "disable retries"."""
adapter = make_tracked_adapter(retry=None)