: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(devex): keep zxcvbn and webauthn off the django.setup() path (#109739)
Julian Bez8f1b84cce250
diff --git a/docs/internal/django-startup-time.md b/docs/internal/django-startup-time.mdindex 5d025079085e..aa304dbf6a17 100644--- a/docs/internal/django-startup-time.md+++ b/docs/internal/django-startup-time.md@@ -171,6 +171,7 @@ Every removal was a `ready()` chain or a model file dragging a subsystem in at m- `products/signals/backend/receivers.py` imported `scout_harness.suggestions` → `prompt` → `products.tasks.backend.facade.api` → `facade.contracts` (61 pydantic dataclasses) to wire one `post_delete` receiver. Deferred to the receiver body.- `ee` `ready()` imported all of `ee/vercel/integration.py` to wire four receivers. That module imports `ee.api.authentication`, which holds `@api_view` functions, and DRF's decorator resolves `DEFAULT_SCHEMA_CLASS` at decoration time → `posthog.api.documentation` → `drf_spectacular.plumbing` → `rest_framework.test` → `django.test` → `jinja2`. The receivers now live in `ee/vercel/receivers.py` (a light module, as the skill asks) and import the integration when they fire.- `posthog/helpers/impersonation.py` (reached from the activity-log signal handlers) imported `posthog.auth`, which pulls `zxcvbn` and `webauthn`. Deferred. The guard then caught that `WebauthnCredential` only registered through that import — it is now imported from `posthog/models/__init__.py`.+ `posthog/event_usage.py` is a second door: model files import it at module scope, so its `posthog.auth` import is call-time too.- `posthog/apps.py` imported `posthog.async_migrations.setup` (which imports every async migration) even when `SKIP_ASYNC_MIGRATIONS_SETUP` is on. Import moved under the branch that runs it. That module also used `infi.clickhouse_orm.utils.import_submodules`, and the `infi` package `__init__` imports `pkg_resources` (~40ms); replaced with a local `pkgutil` helper. Deferring it exposed a latent cycle that setup-first import order had hidden: `async_migrations/utils.py` imported the dependency map from `setup.py` at module scope, `setup.py` imports every migration module, and three migrations import `utils`. Entering at `utils` first (the celery task module, the API module, the tests) now failed with a partially initialised module, so `utils` reads the map at call time.- `boto3`/`botocore` reached setup through three doors: `products.workflows.backend.providers` (eager aggregator `__init__`, hit by the email and twilio integration models), `posthog/storage/object_storage.py`, and `posthog/models/js_snippet_versioning.py`, plus an `except (BotoCoreError, ClientError)` in `posthog/storage/hypercache.py`. All build clients or classify exceptions at call time now. Web workers pay the boto3 import on their first request that touches object storage.diff --git a/posthog/event_usage.py b/posthog/event_usage.pyindex 8c637131bd40..a956e4e2ca8d 100644--- a/posthog/event_usage.py+++ b/posthog/event_usage.py@@ -12,7 +12,6 @@ from django.contrib.auth.models import AnonymousUserimport posthoganalyticsfrom opentelemetry import trace-from posthog.auth import SessionAuthenticationfrom posthog.clickhouse.query_tagging import get_query_tag_valuefrom posthog.constants import POSTHOG_INTERNAL_EMAIL_SUFFIXfrom posthog.helpers.oauth_pending_connection import PendingOAuthConnection@@ -521,6 +520,10 @@ def get_event_source(request) -> EventSource:# DRF sets successful_authenticator during view dispatch; before that# (e.g. in middleware), fall back to checking the Django session cookie# which is available after Django's AuthenticationMiddleware runs.+ # Call-time import: model files import this module during django.setup(), and posthog.auth+ # pulls zxcvbn and webauthn, which no background process needs.+ from posthog.auth import SessionAuthentication # noqa: PLC0415 — keeps the heavy dep off the import path+if isinstance(getattr(request, "successful_authenticator", None), SessionAuthentication):return EventSource.WEBif getattr(getattr(request, "session", None), "session_key", None) is not None:diff --git a/posthog/test/repo_invariants/setup_import_baseline.txt b/posthog/test/repo_invariants/setup_import_baseline.txtindex fbb37eea627b..4d3eece7e47f 100644--- a/posthog/test/repo_invariants/setup_import_baseline.txt+++ b/posthog/test/repo_invariants/setup_import_baseline.txt@@ -29,6 +29,4 @@ psycopgredisrichtrio-webauthn-zxcvbn# First-party modules with a known, structural import cost:diff --git a/posthog/test/repo_invariants/test_startup_import_budget.py b/posthog/test/repo_invariants/test_startup_import_budget.pyindex 4e8d61526827..5f12fda15708 100644--- a/posthog/test/repo_invariants/test_startup_import_budget.py+++ b/posthog/test/repo_invariants/test_startup_import_budget.py@@ -49,6 +49,8 @@ FORBIDDEN_AT_SETUP = ["ee.vercel.integration", # reaches ee.api.authentication (@api_view -> DRF schema class) — receivers live in ee.vercel.receivers and import it at call time"posthog.api.documentation", # drf_spectacular schema hooks — request-time only"django.test", # test client — was dragged in by drf_spectacular.plumbing via rest_framework.test+ "zxcvbn", # password strength, needed only by posthog.auth (deferred in posthog.helpers.impersonation and posthog.event_usage)+ "webauthn", # passkeys, same door as zxcvbn (posthog.auth)"posthog.async_migrations.setup", # imports every async migration — only when SKIP_ASYNC_MIGRATIONS_SETUP is off"infi.clickhouse_orm", # ClickHouse ORM — migration commands only; its package __init__ imports pkg_resources"pkg_resources", # setuptools shim (~40ms) — only reached via infi.clickhouse_orm