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(flags): stop reporting handled scheduled-change misses to error tracking (#68209)

posthog[bot]2d64214cae4f
diff --git a/posthog/tasks/process_scheduled_changes.py b/posthog/tasks/process_scheduled_changes.py
index 700a67fdaa81..f5ef371e056c 100644
--- a/posthog/tasks/process_scheduled_changes.py
+++ b/posthog/tasks/process_scheduled_changes.py
@@ -194,10 +194,15 @@ def process_scheduled_changes() -> None:
scheduled_change.save()
continue
+ orphaned_target = False
try:
# Execute the change on the model instance
model = models[scheduled_change.model_name]
- instance = model.objects.get(id=scheduled_change.record_id, team_id=scheduled_change.team_id)
+ try:
+ instance = model.objects.get(id=scheduled_change.record_id, team_id=scheduled_change.team_id)
+ except ObjectDoesNotExist:
+ orphaned_target = True
+ raise
# Approval-aware dispatch: a scheduled change whose payload flips a policy-gated
# field carries a bound ChangeRequest created at scheduling time. We only apply
@@ -372,7 +377,27 @@ def process_scheduled_changes() -> None:
# For recoverable errors under retry limit, leave executed_at=NULL to allow retries
scheduled_change.save()
- capture_exception(e)
+
+ # orphaned_target covers any target row missing for this record_id/team_id —
+ # most commonly deleted after the change was scheduled, but also a record_id
+ # that never existed for this team. Either way it's expected drift, already
+ # handled via the row's failure_reason above, so reporting it to error tracking
+ # is pure noise. Other unrecoverable errors — invalid payload, unsupported
+ # operation, mismatched variant data, or a missing bound ChangeRequest —
+ # indicate either a broken payload or a data integrity issue, and should stay
+ # visible in error tracking.
+ if orphaned_target:
+ logger.info(
+ "Scheduled change skipped: target record not found",
+ scheduled_change_id=scheduled_change.id,
+ model_name=scheduled_change.model_name,
+ record_id=scheduled_change.record_id,
+ team_id=scheduled_change.team_id,
+ error=str(e),
+ error_type=e.__class__.__name__,
+ )
+ else:
+ capture_exception(e)
except OperationalError:
# Failed to obtain the lock
pass
diff --git a/posthog/tasks/test/test_process_scheduled_changes.py b/posthog/tasks/test/test_process_scheduled_changes.py
index 5004d6bef5c1..02b5bcf0f14c 100644
--- a/posthog/tasks/test/test_process_scheduled_changes.py
+++ b/posthog/tasks/test/test_process_scheduled_changes.py
@@ -4,6 +4,7 @@ from typing import Any, cast
from freezegun import freeze_time
from posthog.test.base import APIBaseTest, QueryMatchingTest, snapshot_postgres_queries
+from unittest.mock import patch
from parameterized import parameterized
@@ -387,8 +388,6 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
def test_recoverable_error_allows_retry(self) -> None:
"""Test that recoverable errors don't set executed_at, allowing retries"""
- from unittest.mock import patch
-
feature_flag = FeatureFlag.objects.create(
name="Test Flag",
key="test-recoverable-error",
@@ -408,7 +407,10 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
)
# Mock the dispatcher to raise a recoverable error (OperationalError)
- with patch.object(FeatureFlag, "scheduled_changes_dispatcher") as mock_dispatcher:
+ with (
+ patch.object(FeatureFlag, "scheduled_changes_dispatcher") as mock_dispatcher,
+ patch("posthog.tasks.process_scheduled_changes.capture_exception") as mock_capture,
+ ):
from django.db import OperationalError
mock_dispatcher.side_effect = OperationalError("Connection timeout")
@@ -416,6 +418,9 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
# Process the scheduled change - should fail but not set executed_at
process_scheduled_changes()
+ # Recoverable errors are genuinely unexpected and must still reach error tracking
+ mock_capture.assert_called_once()
+
# Refresh the scheduled change from database
updated_scheduled_change = ScheduledChange.objects.get(id=scheduled_change.id)
@@ -435,8 +440,41 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
self.assertEqual(failure_data["retry_count"], 1)
self.assertEqual(failure_data["error_classification"], "recoverable")
- def test_unrecoverable_error_prevents_retry(self) -> None:
- """Test that unrecoverable errors set executed_at, preventing retries"""
+ @parameterized.expand(
+ [
+ # A payload missing required keys, rejected up front by the dispatcher. This payload
+ # was broken from the moment it was created, so it should still reach error tracking.
+ ("invalid_payload", False, {"invalid": "payload"}, "Invalid payload", True),
+ # ObjectDoesNotExist via model.objects.get(): the scenario the PR description is
+ # actually about, a scheduled change whose target flag was deleted after scheduling.
+ # This is expected drift, so it should not be reported to error tracking.
+ ("deleted_flag", True, {"operation": "update_status", "value": True}, "does not exist", False),
+ # A bare ValueError classified unrecoverable via isinstance alone (no matching
+ # error-message indicator). Like invalid_payload, the payload was broken at creation
+ # time (rollout percentages are self-contained in the payload), so it should still
+ # reach error tracking.
+ (
+ "invalid_variant_rollout",
+ False,
+ {
+ "operation": "update_variants",
+ "value": {"variants": [{"key": "control", "name": "Control", "rollout_percentage": 50}]},
+ },
+ "Invalid variant rollout percentages",
+ True,
+ ),
+ ]
+ )
+ def test_unrecoverable_error_prevents_retry(
+ self,
+ _name: str,
+ delete_flag_before_processing: bool,
+ payload: dict[str, Any],
+ expected_error_substring: str,
+ should_capture: bool,
+ ) -> None:
+ """Test that unrecoverable errors set executed_at, preventing retries, and that only the
+ orphaned-target case is suppressed from error tracking"""
feature_flag = FeatureFlag.objects.create(
name="Test Flag",
key="test-unrecoverable-error",
@@ -446,18 +484,37 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
created_by=self.user,
)
- # Create a scheduled change with invalid payload (unrecoverable error)
+ # Create the scheduled change while the flag still exists, so record_id points at a
+ # row that existed at creation time (mirrors the real orphaned-change case).
scheduled_change = ScheduledChange.objects.create(
team=self.team,
record_id=feature_flag.id,
model_name="FeatureFlag",
- payload={"invalid": "payload"}, # This will cause ValidationError
+ payload=payload,
scheduled_at=(datetime.now(UTC) - timedelta(seconds=30)),
created_by=self.user,
)
+ if delete_flag_before_processing:
+ feature_flag.delete()
+
# Process the scheduled change - should fail and set executed_at
- process_scheduled_changes()
+ with (
+ patch("posthog.tasks.process_scheduled_changes.capture_exception") as mock_capture,
+ patch("posthog.tasks.process_scheduled_changes.logger") as mock_logger,
+ ):
+ process_scheduled_changes()
+
+ if should_capture:
+ # A broken payload is a defect worth surfacing, not expected drift.
+ mock_capture.assert_called_once()
+ mock_logger.info.assert_not_called()
+ else:
+ # An orphaned target is expected and already handled, so it must not be
+ # reported to error tracking (it only adds noise), but it must still leave
+ # a log trail so the skip isn't silent.
+ mock_capture.assert_not_called()
+ mock_logger.info.assert_called_once()
# Refresh the scheduled change from database
updated_scheduled_change = ScheduledChange.objects.get(id=scheduled_change.id)
@@ -468,19 +525,16 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
self.assertEqual(updated_scheduled_change.failure_count, 1)
# Parse failure reason to verify it contains error details and retry info
- self.assertIsNotNone(updated_scheduled_change.failure_reason)
failure_reason = updated_scheduled_change.failure_reason
assert failure_reason is not None # For mypy type narrowing
failure_data = json.loads(failure_reason)
- self.assertEqual(failure_data["error"], "Invalid payload")
+ self.assertIn(expected_error_substring, failure_data["error"])
self.assertFalse(failure_data["will_retry"]) # Should indicate won't retry
self.assertEqual(failure_data["retry_count"], 1)
self.assertEqual(failure_data["error_classification"], "unrecoverable")
def test_max_retries_exceeded(self) -> None:
"""Test that changes exceeding max retries preserve actual error info"""
- from unittest.mock import patch
-
from posthog.tasks.process_scheduled_changes import MAX_RETRY_ATTEMPTS
feature_flag = FeatureFlag.objects.create(
@@ -504,7 +558,10 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
)
# Mock the dispatcher to raise a recoverable error that will hit the limit
- with patch.object(FeatureFlag, "scheduled_changes_dispatcher") as mock_dispatcher:
+ with (
+ patch.object(FeatureFlag, "scheduled_changes_dispatcher") as mock_dispatcher,
+ patch("posthog.tasks.process_scheduled_changes.capture_exception") as mock_capture,
+ ):
from django.db import OperationalError
mock_dispatcher.side_effect = OperationalError("Database connection lost")
@@ -512,6 +569,10 @@ class TestProcessScheduledChanges(APIBaseTest, QueryMatchingTest):
# Process the scheduled change - should hit max retry limit
process_scheduled_changes()
+ # Retry-exhausted errors are still genuinely unexpected (recoverable), so they
+ # must keep reaching error tracking even on the final, non-retrying attempt
+ mock_capture.assert_called_once()
+
# Refresh the scheduled change from database
updated_scheduled_change = ScheduledChange.objects.get(id=scheduled_change.id)