: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(exports): stop importing playwright at failure_handler module level (#69938)
Paul D'Ambraa505346b5c9a
diff --git a/products/exports/backend/tasks/failure_handler.py b/products/exports/backend/tasks/failure_handler.pyindex b558cd4d8ff1..a4d8382b3efa 100644--- a/products/exports/backend/tasks/failure_handler.py+++ b/products/exports/backend/tasks/failure_handler.py@@ -1,10 +1,10 @@+import sysfrom ssl import SSLErrorfrom django.db import OperationalErrorfrom billiard.exceptions import SoftTimeLimitExceededfrom clickhouse_driver.errors import SocketTimeoutError-from playwright.sync_api import TimeoutError as PlaywrightTimeoutErrorfrom rest_framework.exceptions import ValidationErrorfrom urllib3.exceptions import MaxRetryError, ProtocolError, ReadTimeoutError@@ -126,7 +126,6 @@ USER_QUERY_ERRORS = (TIMEOUT_ERRORS = (SoftTimeLimitExceeded,TimeoutError,- PlaywrightTimeoutError,ExportCancelled,)@@ -135,15 +134,32 @@ USER_QUERY_ERROR_NAMES = frozenset(cls.__name__ for cls in USER_QUERY_ERRORS)SYSTEM_ERROR_NAMES = frozenset(cls.__name__ for cls in EXCEPTIONS_TO_RETRY)# "TimeoutException" kept literally: historical ExportedAsset rows from the retired selenium# render path stored that exception name and must still classify as timeouts.+# playwright's TimeoutError.__name__ is also "TimeoutError" (aliased on import), so it's already+# covered here without needing the class itself.TIMEOUT_ERROR_NAMES = frozenset(cls.__name__ for cls in TIMEOUT_ERRORS) | {"TimeoutException"}+def _is_playwright_timeout(exception: BaseException) -> bool:+ # playwright is a heavy import (browser automation), only needed by the actual image-export+ # path. isinstance() against it can't be a real match unless something has already imported+ # playwright.sync_api (raising one requires it), so checking sys.modules first avoids paying+ # the import cost on every failure-classification call. getattr with a default covers the+ # window where another thread has started (but not finished) that first import: sys.modules+ # holds a partially initialized module then, which may not expose TimeoutError yet — and an+ # exception raised by playwright itself cannot predate its own module finishing import.+ playwright_sync_api = sys.modules.get("playwright.sync_api")+ if playwright_sync_api is None:+ return False+ timeout_error = getattr(playwright_sync_api, "TimeoutError", None)+ return timeout_error is not None and isinstance(exception, timeout_error)++def classify_failure_type(exception: Exception | str) -> str:# Live exceptions are classified by actual type, not name: the name sets are derived from# these same tuples, so isinstance has identical coverage while avoiding false positives from# unrelated classes that merely share a name (django/pydantic ValidationError, builtin SyntaxError).if isinstance(exception, Exception):- if isinstance(exception, TIMEOUT_ERRORS):+ if isinstance(exception, TIMEOUT_ERRORS) or _is_playwright_timeout(exception):return FAILURE_TYPE_TIMEOUT_GENERATIONif isinstance(exception, USER_QUERY_ERRORS):return FAILURE_TYPE_USER