: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(temporal): skip cancellation exceptions in error tracking interceptor (#67239)
posthog[bot]a935abbc43f0
diff --git a/posthog/temporal/common/posthog_client.py b/posthog/temporal/common/posthog_client.pyindex c258d9f24dce..8603a2f97937 100644--- a/posthog/temporal/common/posthog_client.py+++ b/posthog/temporal/common/posthog_client.py@@ -65,6 +65,10 @@ class _PostHogClientActivityInboundInterceptor(ActivityInboundInterceptor):try:return await super().execute_activity(input)except Exception as e:+ # Cancellations (worker drain, activity timeout, workflow cancellation) are expected+ # control flow, not defects — re-raise without reporting them to error tracking.+ if temporalio.exceptions.is_cancelled_exception(e):+ raiseactivity_info = activity.info()capture_kwargs = {"properties": {@@ -97,6 +101,8 @@ class _PostHogClientWorkflowInterceptor(WorkflowInboundInterceptor):except Exception as e:if isinstance(e, temporalio.exceptions.ActivityError):raise # Already captured at the activity level+ if temporalio.exceptions.is_cancelled_exception(e):+ raise # Expected cancellation (worker drain, timeout, cancel), not a defecttry:workflow_info = workflow.info()capture_kwargs = {diff --git a/posthog/temporal/tests/common/test_error_tracking.py b/posthog/temporal/tests/common/test_error_tracking.pyindex 00b28501c6f0..fe444bb3495b 100644--- a/posthog/temporal/tests/common/test_error_tracking.py+++ b/posthog/temporal/tests/common/test_error_tracking.py@@ -11,7 +11,7 @@ from unittest.mock import patchfrom temporalio import activity, workflowfrom temporalio.client import Client, WorkflowFailureErrorfrom temporalio.common import RetryPolicy-from temporalio.exceptions import ApplicationError+from temporalio.exceptions import ApplicationError, CancelledErrorfrom temporalio.worker import UnsandboxedWorkflowRunner, Workerfrom posthog.temporal.common.posthog_client import PostHogClientInterceptor@@ -79,6 +79,24 @@ async def failing_activity_with_properties_to_log(inputs: OptionallyFailingInputraise ValueError("Activity failed!")+async def cancelled_activity(inputs: OptionallyFailingInputs) -> None:+ raise CancelledError()+++class CancelledActivityWorkflow:+ @workflow.run+ async def run(self, inputs: OptionallyFailingInputs) -> None:+ await workflow.execute_activity(+ cancelled_activity,+ inputs,+ start_to_close_timeout=dt.timedelta(minutes=1),+ heartbeat_timeout=dt.timedelta(seconds=5),+ retry_policy=RetryPolicy(maximum_attempts=1),+ )++@workflow.defnclass DirectlyFailingWorkflow:@workflow.run@@ -160,6 +178,34 @@ async def test_exception_capture(fail: bool, capture_additional_properties: boolmock_ph_capture.assert_not_called()+async def test_cancellation_is_not_captured(temporal_client: Client):+ """A cancelled activity (worker drain, timeout, cancel) is expected control flow, not a defect,+ so the interceptor must re-raise without reporting it to error tracking."""+ task_queue = "TEST-TASK-QUEUE"+ workflow_id = str(uuid.uuid4())++ with patch("posthog.temporal.common.posthog_client.capture_exception") as mock_ph_capture:+ async with Worker(+ temporal_client,+ task_queue=task_queue,+ workflows=[CancelledActivityWorkflow],+ activities=[cancelled_activity],+ interceptors=[PostHogClientInterceptor()],+ workflow_runner=UnsandboxedWorkflowRunner(),+ ):+ with pytest.raises(WorkflowFailureError):+ await temporal_client.execute_workflow(+ "CancelledActivityWorkflow",+ OptionallyFailingInputs(fail=True),+ id=workflow_id,+ task_queue=task_queue,+ retry_policy=RetryPolicy(maximum_attempts=1),+ )++ mock_ph_capture.assert_not_called()++@pytest.mark.asyncioasync def test_workflow_only_error_is_captured(temporal_client: Client):task_queue = "TEST-TASK-QUEUE"