: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(wizard): add server-side killswitch for onboarding wizard SSE stream (#61508)
Fernando Gomesf2167f5071b2
diff --git a/products/wizard/backend/presentation/views.py b/products/wizard/backend/presentation/views.pyindex 4b2b31933229..f27aec628fb0 100644--- a/products/wizard/backend/presentation/views.py+++ b/products/wizard/backend/presentation/views.py@@ -10,9 +10,11 @@ from collections.abc import AsyncIteratorfrom typing import Anyfrom django.conf import settings-from django.http import StreamingHttpResponse+from django.http import HttpResponse, StreamingHttpResponse+from django.http.response import HttpResponseBaseimport structlog+import posthoganalyticsfrom asgiref.sync import sync_to_asyncfrom drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schemafrom rest_framework import status, viewsets@@ -95,6 +97,22 @@ SSE_HEARTBEAT_INTERVAL_SECONDS = 15.0SSE_POLL_TIMEOUT_SECONDS = 1.0SSE_MAX_DURATION_SECONDS = 30 * 60+WIZARD_SYNC_KILLSWITCH_FLAG = "onboarding-wizard-sync-killswitch"+++def _wizard_sync_killswitch_enabled(distinct_id: str) -> bool:+ # Local-only eval: no per-request network/decide call on this hot endpoint.+ # Flag definitions are served via HyperCache (posthog/apps.py). Fail-open:+ # if the flag can't be evaluated locally, the stream behaves normally.+ return bool(+ posthoganalytics.feature_enabled(+ WIZARD_SYNC_KILLSWITCH_FLAG,+ distinct_id,+ only_evaluate_locally=True,+ send_feature_flag_events=False,+ )+ )+class WizardSessionViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):scope_object = "wizard_session"@@ -236,7 +254,18 @@ class WizardSessionViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):},)@action(detail=False, methods=["get"], url_path="stream", renderer_classes=[EventStreamRenderer])- def stream(self, request: Request, *args: Any, **kwargs: Any) -> StreamingHttpResponse:+ def stream(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponseBase:+ # Killswitch first, before any other work: a 204 tells EventSource to stop+ # reconnecting, severing the self-reconnect loop for already-open tabs.+ user = getattr(request, "user", None)+ distinct_id = (+ str(user.distinct_id)+ if user is not None and not user.is_anonymous and getattr(user, "distinct_id", None)+ else f"team:{self.team_id}"+ )+ if _wizard_sync_killswitch_enabled(distinct_id):+ return HttpResponse(status=204)+workflow_id = request.query_params.get("workflow_id")skill_id = request.query_params.get("skill_id") or Noneif not workflow_id:diff --git a/products/wizard/backend/tests/test_presentation.py b/products/wizard/backend/tests/test_presentation.pyindex a9b1d5881895..713308dfc62e 100644--- a/products/wizard/backend/tests/test_presentation.py+++ b/products/wizard/backend/tests/test_presentation.py@@ -1,4 +1,5 @@from posthog.test.base import APIBaseTest+from unittest.mock import patchfrom parameterized import parameterizedfrom rest_framework import status@@ -6,6 +7,7 @@ from rest_framework import statusfrom posthog.models import Organizationfrom products.wizard.backend.models import WizardSession+from products.wizard.backend.presentation.views import _wizard_sync_killswitch_enabledclass TestWizardSessionViewSet(APIBaseTest):@@ -198,3 +200,18 @@ class TestWizardSessionViewSet(APIBaseTest):# only required query param; skill_id is optional for pattern subscribe).response = self.client.get(f"{self._url()}stream/")self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)++ @patch("products.wizard.backend.presentation.views.posthoganalytics.feature_enabled", return_value=True)+ def test_stream_killswitch_returns_204(self, _mock_feature_enabled):+ # When the killswitch flag is on, the endpoint short-circuits with a 204+ # before any stream work — a 204 tells EventSource to stop reconnecting.+ response = self.client.get(f"{self._url()}stream/?workflow_id=onboarding")+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)++ @parameterized.expand([("on", True, True), ("off", False, False), ("unresolved", None, False)])+ def test_wizard_sync_killswitch_helper(self, _label, flag_value, expected):+ with patch(+ "products.wizard.backend.presentation.views.posthoganalytics.feature_enabled",+ return_value=flag_value,+ ):+ self.assertEqual(_wizard_sync_killswitch_enabled("distinct-id"), expected)