: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hubspot): retry token refresh while portal migration is in progress (#64985)
Tom Owers9fc81307d587
diff --git a/posthog/temporal/data_imports/sources/hubspot/auth.py b/posthog/temporal/data_imports/sources/hubspot/auth.pyindex f49a3a7c3087..d4d433049ec2 100644--- a/posthog/temporal/data_imports/sources/hubspot/auth.py+++ b/posthog/temporal/data_imports/sources/hubspot/auth.py@@ -16,7 +16,7 @@ _FALLBACK_WAIT = wait_exponential_jitter(initial=1, max=30)class HubspotRetryableError(Exception):- """Transient HubSpot API failure (429 rate limit or 5xx) that should be retried with backoff."""+ """Transient HubSpot API failure (429 rate limit, 5xx, or portal migration) that should be retried with backoff."""def __init__(self, message: str, retry_after: float | None = None) -> None:super().__init__(message)@@ -81,6 +81,11 @@ def hubspot_refresh_access_token(refresh_token: str, source_id: str | None = Nonif res.status_code == 429 or res.status_code >= 500:retry_after = _parse_retry_after(res) if res.status_code == 429 else Noneraise HubspotRetryableError(err_message, retry_after=retry_after)+ # HubSpot briefly returns this (on a non-5xx) while a portal is migrated between data+ # centers; the portal becomes reachable again once the migration finishes, so back off+ # and retry rather than failing the sync (and disabling the schema) on a transient state.+ if "migration in progress" in err_message.lower():+ raise HubspotRetryableError(err_message)raise Exception(err_message)access_token = res.json()["access_token"]diff --git a/posthog/temporal/data_imports/sources/hubspot/test/test_hubspot_auth.py b/posthog/temporal/data_imports/sources/hubspot/test/test_hubspot_auth.pyindex 048807ccc405..70fc0fde4110 100644--- a/posthog/temporal/data_imports/sources/hubspot/test/test_hubspot_auth.py+++ b/posthog/temporal/data_imports/sources/hubspot/test/test_hubspot_auth.py@@ -69,6 +69,25 @@ def test_non_transient_status_raises_plain_exception(status: int, message: str)assert message in str(exc_info.value)[email protected]("status", [400, 403, 423])+def test_portal_migration_in_progress_is_retryable(status: int) -> None:+ # A portal mid-migration is a transient HubSpot state on a non-5xx status; it must back off+ # and retry instead of failing the sync and disabling the schema.+ message = "Migration in progress and the portal is not available for access."+ session = MagicMock()+ session.post.side_effect = [+ _make_response(status, {"message": message}),+ _make_response(status, {"message": message}),+ _make_response(200, {"access_token": "new-token"}),+ ]+ with patch(+ "posthog.temporal.data_imports.sources.hubspot.auth.make_tracked_session",+ return_value=session,+ ):+ assert hubspot_refresh_access_token("refresh-token") == "new-token"+ assert session.post.call_count == 3++def test_transient_status_with_non_json_body_still_retryable() -> None:response = MagicMock()response.status_code = 429