Skip to content

: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.

fix(data-imports): stop tracking benign worker-shutdown errors (#73126)

Tom Owers49fdf83bc64f
diff --git a/posthog/temporal/common/posthog_client.py b/posthog/temporal/common/posthog_client.py
index 1e6e9d7ecac3..c0daef9d01a7 100644
--- a/posthog/temporal/common/posthog_client.py
+++ b/posthog/temporal/common/posthog_client.py
@@ -18,6 +18,7 @@ from posthog.egress.transport.transport import EgressBudgetExhausted
from posthog.exceptions_capture import ambient_exception_properties
from posthog.temporal.common.interceptor import ALL_TASK_QUEUES
from posthog.temporal.common.logger import get_write_only_logger
+from posthog.temporal.common.shutdown import WorkerShuttingDownError
logger = get_write_only_logger()
@@ -67,11 +68,14 @@ class _PostHogClientActivityInboundInterceptor(ActivityInboundInterceptor):
try:
return await super().execute_activity(input)
except Exception as e:
- # Cancellations (worker drain, activity timeout, workflow cancellation) and our own
- # egress-budget backpressure (a deliberate "defer and retry later" signal that our
- # rate limiter already records via record_outbound_decision) are expected control flow,
- # not defects — re-raise without reporting them to error tracking.
- if temporalio.exceptions.is_cancelled_exception(e) or isinstance(e, EgressBudgetExhausted):
+ # Cancellations (worker drain, activity timeout, workflow cancellation), a cooperative
+ # worker shutdown (raised mid-activity during a deploy, always retried on a fresh
+ # worker), and our own egress-budget backpressure (a deliberate "defer and retry later"
+ # signal that our rate limiter already records via record_outbound_decision) are expected
+ # control flow, not defects — re-raise without reporting them to error tracking.
+ if temporalio.exceptions.is_cancelled_exception(e) or isinstance(
+ e, EgressBudgetExhausted | WorkerShuttingDownError
+ ):
raise
activity_info = activity.info()
capture_kwargs = {
diff --git a/posthog/temporal/tests/common/test_error_tracking.py b/posthog/temporal/tests/common/test_error_tracking.py
index 3684693b8fe0..ac56e12aab7e 100644
--- a/posthog/temporal/tests/common/test_error_tracking.py
+++ b/posthog/temporal/tests/common/test_error_tracking.py
@@ -17,6 +17,7 @@ from temporalio.worker import UnsandboxedWorkflowRunner, Worker
from posthog.egress.github.transport import GitHubEgressBudgetExhausted
from posthog.exceptions_capture import bind_exception_context
from posthog.temporal.common.posthog_client import PostHogClientInterceptor
+from posthog.temporal.common.shutdown import WorkerShuttingDownError
@dataclass
@@ -144,6 +145,24 @@ class EgressBackpressureActivityWorkflow:
)
+async def worker_shutting_down_activity(inputs: OptionallyFailingInputs) -> None:
+ raise WorkerShuttingDownError.from_activity_context()
+
+
+class WorkerShuttingDownActivityWorkflow:
+ @workflow.run
+ async def run(self, inputs: OptionallyFailingInputs) -> None:
+ await workflow.execute_activity(
+ worker_shutting_down_activity,
+ inputs,
+ start_to_close_timeout=dt.timedelta(minutes=1),
+ heartbeat_timeout=dt.timedelta(seconds=5),
+ retry_policy=RetryPolicy(maximum_attempts=1),
+ )
+
+
@workflow.defn
class DirectlyFailingWorkflow:
@workflow.run
@@ -315,6 +334,35 @@ async def test_egress_backpressure_is_not_captured(temporal_client: Client):
mock_ph_capture.assert_not_called()
+async def test_worker_shutting_down_is_not_captured(temporal_client: Client):
+ """A cooperative worker shutdown raised mid-activity (during a deploy) is expected control flow
+ that is always retried on a fresh worker, so the interceptor must re-raise it 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=[WorkerShuttingDownActivityWorkflow],
+ activities=[worker_shutting_down_activity],
+ interceptors=[PostHogClientInterceptor()],
+ workflow_runner=UnsandboxedWorkflowRunner(),
+ ):
+ with pytest.raises(WorkflowFailureError):
+ await temporal_client.execute_workflow(
+ "WorkerShuttingDownActivityWorkflow",
+ OptionallyFailingInputs(fail=True),
+ id=workflow_id,
+ task_queue=task_queue,
+ retry_policy=RetryPolicy(maximum_attempts=1),
+ )
+
+ mock_ph_capture.assert_not_called()
+
+
@pytest.mark.asyncio
async def test_workflow_only_error_is_captured(temporal_client: Client):
task_queue = "TEST-TASK-QUEUE"