: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(proxy): stop a duplicate proxy delete returning a 500 (#95888)
posthog[bot]a779744aceec
diff --git a/frontend/src/scenes/settings/environment/ManagedReverseProxy.tsx b/frontend/src/scenes/settings/environment/ManagedReverseProxy.tsxindex e955afccfa5d..f7e32fd5813a 100644--- a/frontend/src/scenes/settings/environment/ManagedReverseProxy.tsx+++ b/frontend/src/scenes/settings/environment/ManagedReverseProxy.tsx@@ -1,5 +1,5 @@import clsx from 'clsx'-import { useActions, useValues } from 'kea'+import { useActions, useAsyncActions, useValues } from 'kea'import { Form } from 'kea-forms'import { useMemo } from 'react'@@ -56,8 +56,10 @@ export function ManagedReverseProxy(): JSX.Element {diagnoseLoadingIds,expandedRecordIds,} = useValues(proxyLogic)- const { acknowledgeCloudflareOptIn, deleteRecord, retryRecord, diagnose, setRecordExpanded, showForm } =- useActions(proxyLogic)+ const { acknowledgeCloudflareOptIn, retryRecord, diagnose, setRecordExpanded, showForm } = useActions(proxyLogic)+ // Awaitable so the confirmation dialog can hold its Delete button in a loading state+ // until the request settles.+ const { deleteRecord } = useAsyncActions(proxyLogic)const { preflight } = useValues(preflightLogic)const cloudflareProxyEnabled = preflight?.instance_preferences?.cloudflare_proxy_enabled@@ -191,6 +193,7 @@ export function ManagedReverseProxy(): JSX.Element {secondaryButton: {children: 'Cancel',},+ shouldAwaitSubmit: true,})},},diff --git a/frontend/src/scenes/settings/environment/proxyLogic.test.ts b/frontend/src/scenes/settings/environment/proxyLogic.test.tsindex be281f46a9ce..33fa1e99a89d 100644--- a/frontend/src/scenes/settings/environment/proxyLogic.test.ts+++ b/frontend/src/scenes/settings/environment/proxyLogic.test.ts@@ -178,3 +178,29 @@ describe('proxyLogic — root redirect', () => {logic.unmount()})})++describe('proxyLogic — delete record', () => {+ it('reloads the records when the delete request fails', async () => {+ const record = mockProxyRecord()+ useMocks({+ get: {+ [`/api/organizations/${MOCK_ORGANIZATION_ID}/proxy_records/`]: proxyRecordsResponse([record]),+ },+ delete: {+ [`/api/organizations/${MOCK_ORGANIZATION_ID}/proxy_records/${record.id}/`]: () => [500, {}],+ },+ })+ initKeaTests()+ organizationLogic.mount()++ const logic = proxyLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ await expectLogic(logic, () => {+ logic.actions.deleteRecord(record.id)+ }).toDispatchActions(['deleteRecordFailure', 'loadRecords'])++ logic.unmount()+ })+})diff --git a/frontend/src/scenes/settings/environment/proxyLogic.ts b/frontend/src/scenes/settings/environment/proxyLogic.tsindex c80aa0259dc0..cf8295b6518d 100644--- a/frontend/src/scenes/settings/environment/proxyLogic.ts+++ b/frontend/src/scenes/settings/environment/proxyLogic.ts@@ -496,7 +496,7 @@ export const proxyLogic = kea<proxyLogicType>([return [response, ...values.proxyRecords]},deleteRecord: async (id: ProxyRecord['id']) => {- void proxyRecordsDestroy(values.currentOrganizationId, id)+ await proxyRecordsDestroy(values.currentOrganizationId, id)const newRecords = [...values.proxyRecords].map((r) => ({...r,status: r.id === id ? 'deleting' : r.status,diff --git a/posthog/api/proxy_record.py b/posthog/api/proxy_record.pyindex e0922d2f46fb..1e1efebd3de9 100644--- a/posthog/api/proxy_record.py+++ b/posthog/api/proxy_record.py@@ -16,6 +16,7 @@ from rest_framework.decorators import actionfrom rest_framework.exceptions import NotFoundfrom rest_framework.response import Responsefrom rest_framework.viewsets import ModelViewSet+from temporalio.common import WorkflowIDConflictPolicyfrom posthog.api.proxy_record_diagnostics import diagnose as diagnose_proxy_recordfrom posthog.api.routing import TeamAndOrgViewSetMixin@@ -542,6 +543,15 @@ class ProxyRecordViewset(TeamAndOrgViewSetMixin, ModelViewSet):record.delete()else:previous_status = record.status+ # The workflow id is fixed per record, so a repeat delete collides with the+ # deletion already running. Join that run only when the record was already+ # deleting. Any other status means the last run is failing, and joining it+ # would leave the record deleting forever once it closes.+ delete_conflict_policy = (+ WorkflowIDConflictPolicy.USE_EXISTING+ if previous_status == ProxyRecord.Status.DELETING+ else WorkflowIDConflictPolicy.FAIL+ )record.status = ProxyRecord.Status.DELETINGrecord.save()@@ -561,6 +571,7 @@ class ProxyRecordViewset(TeamAndOrgViewSetMixin, ModelViewSet):inputs,id=workflow_id,task_queue=settings.GENERAL_PURPOSE_TASK_QUEUE,+ id_conflict_policy=delete_conflict_policy,))except Exception as e:diff --git a/posthog/api/test/test_proxy_record.py b/posthog/api/test/test_proxy_record.pyindex dd26a42c19ef..a7cc58104cef 100644--- a/posthog/api/test/test_proxy_record.py+++ b/posthog/api/test/test_proxy_record.py@@ -8,6 +8,7 @@ from django.test import SimpleTestCasefrom parameterized import parameterizedfrom rest_framework import status+from temporalio.common import WorkflowIDConflictPolicyfrom posthog.api.proxy_record import ProxyRecordUpdateSerializerfrom posthog.models import ProxyRecord@@ -600,15 +601,16 @@ class TestProxyRecordAPI(APIBaseTest):@parameterized.expand([- ("valid", ProxyRecord.Status.VALID),- ("issuing", ProxyRecord.Status.ISSUING),- ("warning", ProxyRecord.Status.WARNING),+ ("valid", ProxyRecord.Status.VALID, WorkflowIDConflictPolicy.FAIL),+ ("issuing", ProxyRecord.Status.ISSUING, WorkflowIDConflictPolicy.FAIL),+ ("warning", ProxyRecord.Status.WARNING, WorkflowIDConflictPolicy.FAIL),+ ("deleting", ProxyRecord.Status.DELETING, WorkflowIDConflictPolicy.USE_EXISTING),])@patch("posthog.api.proxy_record.sync_connect")@patch("posthoganalytics.capture")def test_destroy_active_proxy_starts_deletion_workflow(- self, _name, initial_status, mock_capture, mock_sync_connect+ self, _name, initial_status, expected_conflict_policy, mock_capture, mock_sync_connect):mock_temporal = AsyncMock()mock_sync_connect.return_value = mock_temporal@@ -629,6 +631,7 @@ class TestProxyRecordAPI(APIBaseTest):record.refresh_from_db()assert record.status == ProxyRecord.Status.DELETINGmock_temporal.start_workflow.assert_called_once()+ assert mock_temporal.start_workflow.call_args.kwargs["id_conflict_policy"] == expected_conflict_policy@patch("posthog.api.proxy_record.sync_connect")def test_destroy_returns_500_and_reverts_status_on_temporal_failure(self, mock_sync_connect):